Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Two dimensional arrays - HELP!

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Location
    Oslo
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

     
    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  3. The Following User Says Thank You to Norm For This Useful Post:

    ecco (September 19th, 2010)

  4. #3
    Junior Member
    Join Date
    Sep 2010
    Location
    Oslo
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Two dimensional arrays - HELP!

    Quote Originally Posted by Norm View Post
    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;

     field[rowNum] = rowNum;

    .. 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).

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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];

  6. The Following User Says Thank You to Norm For This Useful Post:

    ecco (September 19th, 2010)

  7. #5
    Junior Member
    Join Date
    Sep 2010
    Location
    Oslo
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Two dimensional arrays - HELP!

    Made it work. Thanks, Norm!

Similar Threads

  1. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM
  2. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM
  3. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM
  4. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM
  5. How to show 2D array using combobox and radiobutton?
    By Broken in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2009, 06:01 AM