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

Thread: Looping / OutOfBounds code problem

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Looping / OutOfBounds code problem

    Here is my code:
    public class Player{
    	public static void main(String [] args) {
    		int myBriefcase;
    		int myCaseIndex;
     
    		int[] cases = {1 , 2 , 5 , 10 , 25 , 50 , 75,
    		 100 , 200 , 300 , 400 ,500,750, 1000 ,
    		 5000 , 10000 , 25000 , 50000 , 75000 , 100000 ,
    		 200000 , 300000 , 400000 , 500000 , 750000 , 1000000 };
     
    		System.out.println("Choose:");
    		myCaseIndex = SavitchIn.readLineInt();
    		myBriefcase = cases[myCaseIndex];
    		System.out.println("You chose briefcase number" + myCaseIndex);
     
    		int i=26;
    		int remove;
    		while ( i != 1){
    			System.out.println("Remove what");
    			remove = SavitchIn.readLineInt();
     
    			if ( remove > 25 )
    			System.out.println("Out of Bounds");
     
    			else if ( cases[remove] == 0 || cases[remove] == cases[myCaseIndex])
    			System.out.println("It has been chosed already.");
     
    			else
    			cases[remove] = 0;
     
    			i--;
    			}
     
    		System.out.println("Inside briefcase is" + cases[myCaseIndex]);
     
    		}
     
    	}

    I am a total beginner in JAVA and it would be great if someone can help me correcting the flaw of my code.

    My first problem is that when the user inputs a larger value in myCaseIndex than 26 it says ArrayIndexOutofBounds. I tried fixing it with an if statement but still I cant seem to get it working.

    My second problem is that this program allows the user to remove case in just 25 times, if you repeat the number lets say you inputed 21, then you inputed 21 again, then 21 again, it is already counted as three times. So how do I fix that? I tried putting an i++ if he/she inputed the number again but it does not compile.

    Oh and another question... Is it possible to make the start of the index of Array as '1' not '0'? If you observe my code it is from a gameshow, and in the gameplay of that show the Cases(Array) starts at '1'. Is it possible to start at '1' or not?

    Hope someone can help me. Sorry if my english sucks, it's not my mother tongue, hope you understand my grammar.


  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: Looping / OutOfBounds code problem

    when the user inputs a larger value in myCaseIndex than 26
    Use an if statement to test the value of the user's input before using that input as an index.
    If the value is too big, print a message and get the user's input again. This will need to be in a loop that will continue looping until the user gives a good input.

    . Is it possible to make the start of the index of Array as '1' not '0'?
    No, array indexes always start at 0

    program allows the user to remove case in just 25 times, if you repeat the number lets say you inputed 21, then you inputed 21 again, then 21 again, it is already counted as three times.
    Can you show what you mean by executing the program.

    Please execute your program to show what the problem is and copy the console output here.
    Add comments to the output describing what is wrong with the programs output.

    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.

Similar Threads

  1. problem in my code
    By dhruvguys in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2011, 05:18 AM
  2. What is the Problem With This Code?
    By olufemi1712 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 18th, 2011, 09:05 AM
  3. ArrayList to String/outOfBounds
    By Scotty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2011, 04:41 PM
  4. Outofbounds error
    By tendulkarfan4life in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 11:20 AM
  5. Problem with the code
    By noFear in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 9th, 2010, 10:28 AM