0

I couldn't find the bug. I used nextLine() after reading nextInt() but still next() is not reading the word("UPDATE"). What am I missing?

Input to read:

    2
    4 5
    UPDATE 2 2 2 4
    QUERY 1 1 1 3 3 3
    UPDATE 1 1 1 23
    QUERY 2 2 2 4 4 4
    QUERY 1 1 1 3 3 3
    2 4
    UPDATE 2 2 2 1
    QUERY 1 1 1 1 1 1
    QUERY 1 1 1 2 2 2
    QUERY 2 2 2 2 2 2

My code:

   Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int i=0 ; i<t ; i++){
            int n = sc.nextInt();
            int m = sc.nextInt();
            sc. nextLine ();
            System.out.println(n+ " " + m);
            for(int j=0 ; j<m ; j++){
                String Q = sc.next(); // tried (String)sc.next(); also

                if(Q == "UPDATE"){
                    int x = sc.nextInt();
                    int y = sc.nextInt();
                    int z = sc.nextInt();
                    int val = sc.nextInt();
                    sc.nextLine();
                    System.out.println(x+ " " + y + " "+ z + " "+ val);
                }
                else if(Q == "QUERY"){
                    int x1 = sc.nextInt()-1;
                    int y1 = sc.nextInt()-1;
                    int z1 = sc.nextInt()-1;

                    int x2 = sc.nextInt();
                    int y2 = sc.nextInt();
                    int z2 = sc.nextInt();
                    sc.nextLine();

                  System.out.println(x1+ " " + y1 + " "+ z1 + " "+ x2+ " " + y2 + " "+ z2);
                }


            }
        }
Jonas
  • 121,568
  • 97
  • 310
  • 388
Charan
  • 1,051
  • 3
  • 12
  • 32
  • 2
    You need to replace `Q == "UPDATE"` with `Q.equals("UPDATE")`, and the same thing for all other string comparisons. – Anders Jul 27 '15 at 12:20

2 Answers2

2

String comparisons in java are made with .equals

replace you code for

if(Q.equals("UPDATE")){
Krismorte
  • 642
  • 7
  • 24
1

you should use equals to compare the strings here.

if(Q.equals("UPDATE")){

if(Q.equals("QUERY")){
Ace
  • 700
  • 7
  • 37