I'm new to java programming (about 1 week) and i've made up this simple program. The objective of the program is to make simple calculations but the problem is trying to input the operator. Here is the code:
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int num1,num2,result; String opr; System.out.println("Input 1st number:"); num1 = scanner.nextInt(); System.out.println("Now the operator:"); opr = scanner.nextLine(); if(opr.equals("+")) { System.out.println("Input 2nd number:"); num2 = scanner.nextInt(); result = num1 + num2; System.out.println("Result: " + result); }else if(opr.equals("-")) { System.out.println("Input 2nd number:"); num2 = scanner.nextInt(); result = num1 - num2; System.out.println("Result: " + result); }else if(opr.equals("*")) { System.out.println("Input 2nd number:"); num2 = scanner.nextInt(); result = num1 * num2; System.out.println("Result: " + result); }else if(opr.equals("/")) { System.out.println("Input 2nd number:"); num2 = scanner.nextInt(); result = num1 / num2; System.out.println("Result: " + result); }else{ System.out.println("Please use a normal operator."); } } }
What happens when i run the code is that it skips over scanning the opr variable and runs the else command. How can i fix this so i can input the operator?
Just a simple begginer question and it would be awesome if you could help me out.