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):
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
ecco