-1

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();


    }

}
Fildor
  • 14,510
  • 4
  • 35
  • 67
leoOrion
  • 1,833
  • 2
  • 26
  • 52

1 Answers1

1

Simple Solution: you should simply call

instSet=in.next() + in.nextLine();

instead of

in.nextLine()

next() returns very first token from standard input. And nextLine() returns next line after this token.

Another solution: nextLine() after every nextInt(), like as below

    System.out.println("Enter the Initial XLocation:");
    x=in.nextInt();
    in.nextLine();
    System.out.println("Enter the Initial YLocation:");
    y=in.nextInt();
    in.nextLine();
    System.out.println("Enter the Instructions\t");
    instSet=in.nextLine();

If you use Scanner#next###() (except for nextLine), and you reach an end of line token as when the user presses enter, if you don't handle the end of line token, it will prevent the Scanner object from working appropriately. So, nextInt() is not reading the and that is being read by nextLine()

Hope it clarifies

Rahul Winner
  • 430
  • 3
  • 16