When getting the input in the main()
function.... the input for the String object instSet
' gets skipped.... can someone explain why this happens and how to rectify...for now i am only enclosing my public class. if required ill add the other classes later...
import java.util.Scanner;
public class MarsRover {
private String inst_set;
private Plateau p;
private Cell currentLocation;
public enum Direction{N,S,E,W}
Direction dir;
public MarsRover(Plateau p1, int x, int y, Direction dir, String instSet) {
p=p1;
inst_set=instSet;
this.dir=dir;
currentLocation=new Cell(x, y);
}
public void executeInst() {
int i = 0;
System.out.println(inst_set.length());
while (i < inst_set.length()) {
//System.out.println("Check");
char inst = inst_set.charAt(i++); //get each inst seperately as a character
if (inst=='M') {
Cell temp=p.getNeighbour(dir, currentLocation);
if (temp != null)
currentLocation=temp;
else
continue;
}
if(inst=='L'||inst=='R'){
dir=setDirection(dir,inst);
}
}
}
private Direction setDirection(Direction dir, char inst) {
if(inst=='L') {
switch (dir) {
case N:
dir = Direction.W;
break;
case W:
dir = Direction.S;
break;
case S:
dir = Direction.E;
break;
case E:
dir = Direction.N;
break;
}
}
else if(inst=='R'){
switch (dir) {
case N:
dir = Direction.E;
break;
case W:
dir = Direction.N;
break;
case S:
dir = Direction.W;
break;
case E:
dir = Direction.S;
break;
}
}
return dir;
}
public static void main(String... a){
Plateau p1=new Plateau();
Scanner in = new Scanner(System.in);
int x,y;
String instSet;
System.out.println("Enter the Initial XLocation:");
x=in.nextInt();
System.out.println("Enter the Initial YLocation:");
y=in.nextInt();
System.out.println("Enter the Instructions");
instSet=in.nextLine();
String dir;
System.out.println("Enter the direction the rover is Headed");
dir=in.nextLine();
MarsRover mr1=new MarsRover(p1,x,y,Direction.valueOf(dir),instSet);
mr1.executeInst();
}
}