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:
_ _ _ _ _ _
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.
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--;
}
}
