Two dimensional arrays - HELP!
Hi!I'm ecco, and I'm pretty new at these forums (obviously), and I need some help with one of my Java-programs.
My program is based on commands through user input (1-7), for buying oil-fields (in Utiopia), showing lists, overviews etc (the rest is not really that important). Command 1 asks the user to give the companies name, and what fields he wants to buy, and depending on whether the fields are bought or not already, I want to save this data into 2 different two-dimensional arrays (one for the company-name, one for fields(rows-columns).
Row-input should only be between the numbers 0 and 10, while columns-input need to be between 0 and 15. The program compiles and runs just fine, but my problem lies at my column-input;
Whenever i set the column-number to be 10 or more, i get an ArrayIndexOutOfBoundsExceptionError, and I believe my problem lies in my for-loops, though, I can't seem to find it. I've been trying to get this to work for hours! Here's my code (read between the 'HERE' and 'to HERE'-commentaries, line 75-85, marked in bold):
Code :
import easyIO.*;
class oilAdmin {
public static void main(String[] args) {
System.out.println("*** Utiopias oil-administration ***");
Oil oil = new Oil();
oil.commandLoop();
System.out.println("--- Ending program! ---");
}
}
class Oil {
In key = new In();
Out screen = new Out();
// In-objects
In inName = new In();
In row = new In();
In col = new In();
// Two-dimensional arrays
String [][] owner = new String[10][15];
int [][] field = new int[10][15];
// Command -> method
void commandLoop() {
int command = -1;
while (command != 0) {
String menu = "1. Buy field \n" +
"2. List fields \n" +
"3. Overview w/statistics \n" +
"4. Update oil-fields \n" +
"5. Find row with MAX oil \n" +
"6. Quit \n";
System.out.println(menu);
System.out.print("Give command: ");
command = key.inInt();
switch (command) {
case 1: buyField(); break;
case 2: showList(); break;
case 3: showOverview(); break;
case 4: updateOil(); break;
case 5: maxOil(); break;
case 6: exit(); break;
}
}
}
void buyField() {
Oil oil = new Oil();
// Name of buyer
System.out.print("Name of buyer: ");
String name = inName.inWord();
// Fields
System.out.print("Oil-field (1) required (ROW, 0-10): ");
int rowNum = row.inInt();
System.out.print("Oil-field (2) required (COL, 0-15): ");
int colNum = col.inInt();
[B]// HERE
if ( rowNum <= 10 && rowNum >= 0 && colNum <= 15 && colNum >= 0 && owner[colNum][rowNum] == null && owner[rowNum][colNum] == null) {
for (int i=0; i<field.length; i++) {
for (int j=0; j<field[i].length; j++) {
field[colNum][rowNum] = rowNum;
field[rowNum][colNum] = colNum;
owner[rowNum][colNum] = name;
owner[colNum][rowNum] = name;
}
}
}
// to HERE.[/B]
else {
System.out.println("Invalid input. Try again!");
oil.commandLoop();
}
System.out.println("Field bought! " + owner[colNum][rowNum] + " now have full rights at: " + field[colNum][rowNum] + "-" + field[rowNum][colNum]);
}
void showList() {
}
void showOverview() {
}
void updateOil() {
}
void maxOil() {
}
void exit() {
System.exit(1);
}
}
I'm open to all suggestions. Thanks in advance :D
ecco
Re: Two dimensional arrays - HELP!
Your code isn't consistent in its use of indexes:
field[colNum][rowNum] = rowNum; // col then row
field[rowNum][colNum] = colNum; // row then col
Can you explain what these lines of code are supposed to do?
Re: Two dimensional arrays - HELP!
Quote:
Originally Posted by
Norm
Your code isn't consistent in its use of indexes:
field[colNum][rowNum] = rowNum; // col then row
field[rowNum][colNum] = colNum; // row then col
Can you explain what these lines of code are supposed to do?
Yeah, thing is I want the row-values to be in the first dimension of the array, and the column-values to be in the second. Obviously, this is where my problem is, but writing;
.. inside the first of the two for-loops, gives me a type-error (found:int, required:int[]). I might not have explained it perfectly good enough, so I'll try again with an example;
Let's say our user tells us his company name is JavaOil, and JavaOil wants to buy the oil in fields of 9(rows)-12(columns). This is where that part you're mentioning comes in; i want the value of rowNum to go into the first dimension of the field-array, at the position equalled to rowNum, and the value of colNum to go into the second dimension of the same array, at the position equalled to colNum - from there, I can connect this data to the owner, JavaOil, which should be placed in the owner-array at its places equalled to the values of rows and columns in my field-array.
Hope that made things more clear of what I'm trying to do. Long story short i want the user input of rows and columns to go into the position thats equal to the key in the array(in its correct dimension).
Re: Two dimensional arrays - HELP!
If you are going to use the same indexes in both dimensions of the array, you should define the two dimensions of the array to be the same size:
int [][] field = new int[15][15];
Re: Two dimensional arrays - HELP!
Made it work. Thanks, Norm!