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 9 of 9

Thread: Airline having with array index out of bounds exception

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Airline having with array index out of bounds exception

    class test{//class
    public static void main(String[]args)
    {
    String booking [][]= new String [30] [6] ;//two dimensional array
    System.out.println("Enter the seat column you want");//column entry
    char column=Keyboard.readChar();
    System.out.println("Enter the seat row you want");//row entry
    int row=Keyboard.readInt();
    System.out.println("Enter your name");//name entry
    String name=Keyboard.readString();

    if(column=='a' || column =='A')
    {
    column='0';
    }
    else if(column=='b' || column=='B')
    {
    column='1';
    }
    else if(column=='c' || column=='C')
    {
    column='2';
    }
    else if(column=='d' || column=='D')
    {
    column='3';
    }
    else if(column=='e' || column=='E')
    {
    column='4';
    }
    else if(column=='f' || column=='F')
    {
    column='5';
    }
    System.out.println( name + " your seat is column " + column + " seat " +row);

    booking[column][row] = name;//put name into chosen index

    for(int i = 0; i < booking.length; i++)
    {
    for(int x = 0; x < booking[0].length; x++)
    {
    System.out.print(booking[i][x]+" ");
    }
    System.out.println();
    }
    }
    }




    /************************************************** ************
    could anyone help me having trouble with it
    cant get my in put to go in to the index

    eooro.png


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

    Default Re: Airline having with array index out of bounds exception

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    dubs4sam (March 22nd, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Airline having with array index out of bounds exception

    Enter the seat column you want
    C
    Enter the seat row you want
    22
    Enter your name
    Paul
    Paul your seat is column 2 seat 22

    This is the error i get

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50
    at test.main(test.java:38)
    Press any key to continue . . .

    This is the error i get


    class test{//class
    	public static void main(String[]args)
    	{
    String  booking [][]= new  String [30] [6] ;//two dimensional array
    System.out.println("Enter the seat column you want");//column entry
    			char column=Keyboard.readChar();
    System.out.println("Enter the seat row you want");//row entry
    			int row=Keyboard.readInt();
    System.out.println("Enter your name");//name entry
    			String name=Keyboard.readString();
     
    		if(column=='a' || column =='A')
    			{
    			   column='0';
    			}
    		else if(column=='b' || column=='B')
    			{
    				column='1';
    			}
    		else if(column=='c' || column=='C')
    		   {
    				column='2';
    			}
    		else if(column=='d' || column=='D')
    			{
    				column='3';
    			}
    		else if(column=='e' || column=='E')
    			{
    				column='4';
    			}
    		else if(column=='f' || column=='F')
    			{
    				column='5';
    			}
    		System.out.println(  name + " your seat  is  column "  +  column + " seat "  +row);
     
    booking[column][row] = name;//put name into chosen index
     
    			for(int i = 0; i < booking.length; i++)
    				{
    					for(int x = 0; x < booking[0].length; x++)
    				{
    					System.out.print(booking[i][x]+" ");
    				}
    				System.out.println();
    		}
    	}
    }

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

    Default Re: Airline having with array index out of bounds exception

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50
    at test.main(test.java:38)
    At line 38 the code uses an array index with a value of 50 in an array that does not have 51 elements.
    Look at line 38 and check why the index used on that line is past the end of the array.

    What code is at line 38?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Airline having with array index out of bounds exception

    Code at line 38



    booking[column][row] = name;//put name into chosen index

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

    Default Re: Airline having with array index out of bounds exception

    What are the values of the variables: column and row?

    Why is column defined as char and not as an int? The char: '2' has an int value of 50.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Airline having with array index out of bounds exception

    String  booking [][]= new  String [30] [6] ;//two dimensional array
    System.out.println("Enter the seat column you want");//column entry
    			char column=Keyboard.readChar();
    System.out.println("Enter the seat row you want");//row entry
    			int row=Keyboard.readInt();
    System.out.println("Enter your name");//name entry
    			String name=Keyboard.readString();
    column and row are values that are entered by the user
    column is defined as char because the user must selected a either A,B,C,D,E,F,
    AND THEN select a seat number associated with that row any number 1 to 30

    --- Update ---

    then the name should fill in to the appropriate position in the array booking to the row and column the user entered

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

    Default Re: Airline having with array index out of bounds exception

    The char: 'A', 'B' can not be used as indexes into an array that expects int index values from 0.
    You can convert char to 0 based int values:
    To convert the char 'A' to a 0 subtract 'A' from it. For example 'B' - 'A' = 1

    Your usage of column and row are reversed from normal.
    Usually it is array[row][column]
    If you don't understand my answer, don't ignore it, ask a question.

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

    dubs4sam (March 22nd, 2014)

  11. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Airline having with array index out of bounds exception

    THanks for the help much appreciated lad

Similar Threads

  1. Help needed for string index out of bounds exception
    By BFF in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 11th, 2013, 10:48 PM
  2. Array Index Out of bounds?
    By blazeking in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2012, 01:44 AM
  3. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  4. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  5. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM