This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/2/25

Traversing an array

JasonZhu JasonZhu

2014/2/25

#
    public void main()
    {
        System.out.println();
        int position = 0;
        ArrayList<Integer>nums = new ArrayList<Integer>();
        System.out.println("Snakes and Ladders.");
        System.out.println("Enter number list followed by any letter when finished: (ex. 3 3 -1 -1 2 y)");
        Scanner input = new Scanner(System.in);
        while(input.hasNextInt()){
            nums.add(input.nextInt());      
        }
        position+=nums.get(0);
        while(position!=0||position<nums.size()||position>0){
            if(position==0){
                System.out.println("Loop");
            }else if(position>nums.size()){
                System.out.println("Right");
            }else if(position<0){
                System.out.println("Left");
            }
            position+=nums.get(position);
        }
    }
I get an out of bounds exception for the array based on my inputs. Is there a way to avoid this while returning either left, right, or loop?
bourne bourne

2014/2/25

#
I think your ArrayList nums is empty and you try accessing its first element.
while(input.hasNextInt()){
Your while loop returns false at start since the user hasn't inputted anything yet.
JasonZhu JasonZhu

2014/2/25

#
It works to store the inputted values into the array. The problem I'm having is stopping the while loop that traverses the array before an error appears. Ex. Input: 3 3 -1 -1 2 d Output: right (but it throws me an array index exception) Ex2. Input: 4 2 3 5 -10 7 2 5 13 14 Output: left (but it throws me an array index exception like before) I thought that by having the while loop, it'd check the conditions before trying to access that non-existent cell in the arraylist... That was of course when I had
14.        if(position==0){  
15.            System.out.println("Loop");  
16.        }else if(position>nums.size()){  
17.            System.out.println("Right");  
18.        }else if(position<0){  
19.            System.out.println("Left");  
20.        } 
outside of the while loop.
bourne bourne

2014/2/25

#
if(position==0){ System.out.println("Loop"); }else if(position>nums.size()){ System.out.println("Right"); }else if(position<0){ System.out.println("Left"); } position+=nums.get(position);
So you are checking if the position variable is within bounds. And then if it is out of bounds (left or right), it then tries to access the element at that position (last line of code in above quote)
JasonZhu JasonZhu

2014/2/25

#
No, you travel through the array based off the value of the cell in the array list. Ex. 0 1 2 3 4 <--- cell numbers 3 3 -1 -1 2 <--- user input you start at cell 0 and you travel 3 right, you land on cell 3, you move back 1 left, then 1 left again, and eventually you reach the end of the array from the right side, that's the output is right. The problem I'm having is the array exception when my code goes through that while block.
bourne bourne

2014/2/25

#
If it prints out "Right" or "Left", it means the variable position is not a valid position within the ArrayList correct? So when those happen,
position+=nums.get(position);
should not be executed, because an indexoutofbounds will occur
JasonZhu JasonZhu

2014/2/25

#
exactly, how do I prevent that? is there a way to kick myself out of the while loop? so that:
    public void main()
    {
        System.out.println();
        int position = 0;
        ArrayList<Integer>nums = new ArrayList<Integer>();
        System.out.println("Snakes and Ladders.");
        System.out.println("Enter number list followed by any letter when finished: (ex. 3 3 -1 -1 2 y)");
        Scanner input = new Scanner(System.in);
        while(input.hasNextInt()){
            nums.add(input.nextInt());      
        }
        position+=nums.get(0);
        while(position!=0||position<nums.size()||position>0){
            position+=nums.get(position);
        }
        if(position==0){
            System.out.println("Loop");
        }else if(position>nums.size()){
            System.out.println("Right");
        }else if(position<0){
            System.out.println("Left");
        }        
    }
it would enter the if and else if statement that will print out an output?
bourne bourne

2014/2/25

#
Change your or's (||) to and's (&&)
JasonZhu JasonZhu

2014/2/25

#
Wow, that fixed it, thank you so much! Why is it that I need &&s though, isn't it one or the other condition, so I'd have to use ||s?
bourne bourne

2014/2/25

#
Or's keep your loop going while any one of the conditions are true. And's keep your loop going only while all the conditions are true
JasonZhu JasonZhu

2014/2/25

#
Also, is there a way to make it so that I wouldn't have to place a non-int ( a letter) to mark the end of user input?
JasonZhu JasonZhu

2014/2/25

#
bourne wrote...
Or's keep your loop going while any one of the conditions are true. And's keep your loop going only while all the conditions are true
OHHHH, it's backwards compared to if statement's ||s and &&s that I'm used to, thanks for the info!
bourne bourne

2014/2/25

#
I'm sure there is a way. I wouldn't read each int in one at a time though. I would read one line in, and parse it for ints.
JasonZhu JasonZhu

2014/2/25

#
Thank you for everything, bourne.
You need to login to post a reply.