I’m new to java, the nextLine(); doesn’t work once and it does all of the other times. I’m confused 🙁 I use eclipse in case you need to know.
package tuna;
import java.util.Scanner;
public class Calculater {
public static void main (String args []) {
Scanner uno = new Scanner(System.in);
System.out.println("How many differant numbers do you want to use? (up to four, minimum two)");
double two = uno.nextDouble();
if (two == 2){
System.out.println("2");
System.out.println("Enter first number: ");
double fnum = uno.nextDouble();
System.out.println(fnum);
System.out.println("Enter second number: ");
double snum = uno.nextDouble();
System.out.println(snum);
System.out.println("Enter number operation (Say plus, minus, divide or times. No capitials please)");
String op = uno.nextLine();
. above the uno.nextLine doesn’t work. Why?
if (op.equals("plus")){
System.out.println(fnum + snum);
}
}
double three;
double four;
}
}
EDIT: What I mean by ‘it doesn’t work’ is that after “Enter number operation (Say plus, minus, divide or times. No capitials please)” is printed in the output, I can’t type anything in. When you run it says how many numbers do you want? I type 2 and enter. Then it says enter first number. I say 1 and enter. Then it says enter second number. I say 1 and enter. Then it says what number operation. It doesn’t let you type even though there is a nextLine(); in there. If you add another nextLine(); in there you can type, but if you type plus, nothing happens, where it should print the first number add the second number.
If you input:
Then
uno.nextDouble();consumes3.5and leavesThen
uno.nextDouble();consumes<enter>2and leavesNext
uno.nextLine();just consumes<enter>. You need to calluno.nextLine();again in order to consume the remainingdivide<enter>.