2

Possible Duplicate:
java.util.Scanner : why my nextLine() in my code not promt?

import java.util.Scanner;

class apples {
public static void main(String args[]){
        Scanner npwn = new Scanner(System.in);
        int first,second,answer;
        String op;
        System.out.print("Insert first number: ");
        first = npwn.nextInt();
        System.out.print("Insert Operation ('+', '-', '*', '/')");
        op = npwn.nextLine();
        if (op.equals("+")) {
            System.out.print("Insert next number: ");
            second = npwn.nextInt();
            answer = first + second;
        } 
        else { System.out.print("Insert next number: ");
        second = npwn.nextInt(); 
        answer = first - second;

        }
        System.out.println(answer);
}
}

Where it says "op:nextLine();"; it doesn't pause and wait for the "nextLine" persay. It just skips and prints "Insert next number: ". Why is this?

Community
  • 1
  • 1
nPwn
  • 13
  • 5
  • 2
    I found another question. I had to use "next()" instead of "nexLine()". Here's the link: http://stackoverflow.com/questions/6289765/java-util-scanner-why-my-nextline-in-my-code-not-promt?rq=1 – nPwn Jun 24 '12 at 07:50
  • You only need to declare `answer` before usage. Don't do premature declaration. – user unknown Jun 24 '12 at 08:25

0 Answers0