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

2014/4/28

Recognizing underscores from integers from input

1
2
JasonZhu JasonZhu

2014/4/28

#
I am writing a program where the user inputs a road and my programs outputs the orientation of the road. For instance: Input: The real input consists of no spaces between the underscores, I'm only placing those in their to make it more notable that those are separate underscores. _ _ _ 2 _ _ 0 _ Output: _ _ _ _ _ 20_ The numbers represent the amount of underscores they travel for the output. Cars will never overrun a car in front of it and cars that go off the road from the right side are ignored and removed and the previous spot where the car was will be empty. For example: _ _ _ _ 2 _ Will result in: _ _ _ _ _ _
    public void stuckInTraffic()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Input the number of cases:");
        int cases = in.nextInt();
        while(cases>0){
            System.out.println("Input road orientation.");
            Scanner in1 = new Scanner(System.in);
            String input = in1.nextLine();
            Scanner in2 = new Scanner(input);
            ArrayList<Integer> road = new ArrayList<Integer>();       
            while(in2.hasNext("_")||in2.hasNextInt()){
                if(in2.next().equals("_")){
                    road.add(-1);
                }else{
                    road.add(in2.nextInt());
                }
            }
            for(int i = 0;i<road.size();i++){
                int move = 0;
                if(road.get(i)!=-1){
                    move = road.get(i);
                }
                road.set(i,-1);
                i+=move;
                road.set(i,move);
            }
            for(int i = 0;i<road.size();i++){
                if(road.get(i)==-1){
                    System.out.print("_");
                }else{
                    System.out.print(road.get(i));
                }
            }
            cases--;
        }
    }
I have the following code which does not yet account for the cars that exit the right side of the road, but even without those type of scenarios, I'm receiving no output. Help appreciated.
danpost danpost

2014/4/28

#
I could be wrong on this -- but, it would seem that the 'next' in line 13 is taking the 'nextInt' away from line 16.
JasonZhu JasonZhu

2014/4/28

#
I will give that a try. Thank you for your suggestion.
JasonZhu JasonZhu

2014/4/28

#
I tried reversing the order of those two statements, but my output on my window is still as follows: Input the number of cases: 2 Input road orientation. __2__ Input road orientation.
danpost danpost

2014/4/28

#
I do not think you understood me. I was suggesting that maybe you should save 'next' in a local variable and determine then if it is an underscore or an int. My thought was that if the 'next' in the 'if' condition was not an underscore than IT was the int and the later 'nextInt' was not getting that int, but the following element.
JasonZhu JasonZhu

2014/4/28

#
I have a good feeling the problem resides in that area. I'm not that confident that I'm using the right methods from the if statements.
JasonZhu JasonZhu

2014/4/28

#
I tried what you said.
    public void stuckInTraffic()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Input the number of cases:");
        int cases = in.nextInt();
        while(cases>0){
            System.out.println("Input road orientation.");
            Scanner in1 = new Scanner(System.in);
            String input = in1.nextLine();
            Scanner in2 = new Scanner(input);
            ArrayList<Integer> road = new ArrayList<Integer>();       
            while(in2.hasNext("_")||in2.hasNextInt()){
                String next = in2.next();
                if(next.equals("_")){
                    road.add(-1);
                }else{
                    road.add(Integer.parseInt(next));
                }
                //                 if(in2.hasNextInt()){
                //                     road.add(in2.nextInt());
                //                 }else
                //                 if(in2.next().equals("_")){
                //                     road.add(-1);
                //                 }
            }
            for(int i = 0;i<road.size();i++){
                int move = 0;
                if(road.get(i)!=-1||road.get(i)!=0){
                    move = road.get(i);
                    if((road.get(i)+move)>road.size()){
                        road.set(i,-1);
                    }else{
                        road.set(i,-1);
                        i+=move;
                        road.set(i,move);
                    }
                }
            }
            for(int i = 0;i<road.size();i++){
                if(road.get(i)==-1){
                    System.out.print("_");
                }else{
                    System.out.print(road.get(i));
                }
            }
            cases--;
        }
    }
Same output. :/
JasonZhu JasonZhu

2014/4/28

#
I'm starting to think next() is giving me the entire line rather than just one character. I need a way to treat each character of input individually.
JasonZhu JasonZhu

2014/4/28

#
Looks like I'll have to try something with charAt, I'll give it a try.
danpost danpost

2014/4/28

#
Why not try something like this:
if(in2.hasNext("_")){
    next("_");
    road.add(-1);
}else{
    road.add(in2.nextInt());
}
danpost danpost

2014/4/28

#
I edited the above.
JasonZhu JasonZhu

2014/4/28

#
I gave that a try, I'm getting the same output.
danpost danpost

2014/4/28

#
Did you try the edited version? (or the original)
danpost danpost

2014/4/28

#
I guess both versions were wrong -- should have been:
if(in2.hasNext("_")){
    in2.next("_");
    road.add(-1);
}else{
    road.add(in2.nextInt());
}
(was missing the 'in2.' on line 2)
danpost danpost

2014/4/28

#
What about line 28??? One of the two conditions will always be true! You need '&&' there.
There are more replies on the next page.
1
2