This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
I'm making a simple calculator, but I've run into a problem where a string input is skipped.
Here is my code:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Calculator\n");
while (true) {
System.out.print("Number: ");
float num1 = input.nextFloat();
System.out.print("Operator: ");
String optr = input.nextLine();
System.out.print("Number: ");
float num2 = input.nextFloat();
System.out.println(num1 + num2);
}
}
}
I have tried doing \n but I don't know what else I should really do as I don't know what the problem is.
I know input is never closed, but I have tried closing it. It has no affect and I don't want to add it right now as I am making the base system.
Instead of using...
System.out.print("Operator: ");
String optr = input.nextLine();
...use this...
System.out.print("Operator: ");
String optr = input.next();
The method nextLine() is a bit finnicky. Long story short, if you use nextLine(), beware that it doesn't play well with next(), nextInt(), nextFloat(), etc.
If you want the long story, here is a more detailed explanation -- https://stackoverflow.com/a/13102066/10118965
Related
This question already has answers here:
why cant we use <, <=, >,>= relational operators on String?
(4 answers)
Closed 1 year ago.
I have no idea why this doesn't work. Please help, I am new to java
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("what is your first number ");
String firstNum = scanner.nextLine();
System.out.println("what is your second number ");
String secondNum = scanner.nextLine();
System.out.println("what is your Third number ");
String thirdNum = scanner.nextLine();
if (firstNum > secondNum) {
System.out.println("the firs number is bigger than the firs number");
Scanner.nextLine() is used to input string . Instead use scanner.nextInt() to input number and store in an integer variable.
This is an illegal comparison. You cannot compare a String to a String. You should use Scanner.nextInt(). If you want to use a string to store a numerical value than you can use Integer.parseInt() to convert string into int.
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I'm trying to learn Java through Hackerrank and the challenge that I'm working on currently takes an int, double, and string and prints them on separate lines in reverse order, but I haven't been able to get the string to print.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
double y=sc.nextDouble();
String s=sc.nextLine();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
}
}
The input is:
42
3.1415
Welcome to Hackerrank Java tutorials!
And output is:
String:
Double: 3.1415
Int: 42
I don't know Java at all, but from the code I've seen online, I can't tell why this is wrong.
Change the first part of the code to this:
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
double y = sc.nextDouble();
sc.nextLine(); // Discard rest of current line
String s = sc.nextLine();
The way java.util.Scanner splits the input into numbers or lines is a bit weird.
The sc.nextLine(); should instead be sc.next();
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 7 years ago.
I tried to get inputs via scanner and in the past, I use enter to get to the next set of inputs.
For ex.
Input 1 <enter>
Input 2 <enter>
However this time, it only accepts in the same line , taking spaces as delimiter.
Scanner in = new Scanner(System.in);
int a,b,n,t;
String input_line;
String inputs[]= new String[3];
t = in.nextInt();
in.reset(); //Tried resetting Scanner to see if this works
input_line = in.nextLine();
inputs = input_line.split(" ");
for(String s:inputs)
System.out.println(s);
For instance, I expect to take the variable t in first line and then move on to the second line for input_line scanning. But if I hit enter after entering t, the program ends.
What am I missing here?
(Merging with another question was suggested but , let me explain, the Scanner does not skip any inputs).
Without any testing I would think you would need something like this
Scanner in = new Scanner(System.in);
int a,b,n,t;
String input_line;
String[] input_numbers = new String[3];
t = in.nextInt();
in.nextLine();
input_line = in.nextLine();
while(!input_line.equals("")){
input_numbers = input_line.split(" ");
// do what you want with numbers here for instance parse to make each string variable into int or create new scanner to do so
input_line = in.nextLine();
}
}
Ive got a problem.I 'm new to Java,I've started today:D) ..I've programmed before so I know it little bit,but I am new to Java. Here is my code: `
public class Tutorial {
public static void main(String[] args) {
double num1,num2;
String operacia;
Scanner in=new Scanner (System.in);
System.out.println("Write 2 numbers");
num1=in.nextDouble();
num2=in.nextDouble();
System.out.println("Choose the operation");
operacia=in.nextLine();
if (operacia.equals("+")){
System.out.println("Your result is "+(num1+num2)) ;
}
else if (operacia.equals("-")){
System.out.println("Your result is "+(num1-num2)) ;
}
else if (operacia.equals("/")){
System.out.println("Your result is "+(num1/num2)) ;
}
else if (operacia.equals("*")){
System.out.println("Your result is "+(num1*num2)) ;
}
}
}`
It wants from me 2 numbers,I write them and them it writes "Choose the operation" and its over.No more inputs.Thank you very much :)
Your problem is simple.
Just replace the code with next() instead of nextLine().Effectively, the line your code is returning is receiving is a blank line. Hence when it reaches the conditional statement it has an empty string and terminates.
next()
Finds and returns the next complete token from this scanner.
nextLine()
Advances this scanner past the current line and returns the input that was skipped.
Your code should be fixed by a simple change.
public static void main(String[] args) {
double num1,num2;
String operacia;
Scanner in=new Scanner (System.in);
System.out.println("Write 2 numbers");
num1=in.nextDouble();
num2=in.nextDouble();
System.out.println("Choose the operation");
operacia=in.next();
if (operacia.equals("+")){
System.out.println("Your result is "+(num1+num2)) ;
}
else if (operacia.equals("-")){
System.out.println("Your result is "+(num1-num2)) ;
}
else if (operacia.equals("/")){
System.out.println("Your result is "+(num1/num2)) ;
}
else if (operacia.equals("*")){
System.out.println("Your result is "+(num1*num2)) ;
}
}
Scanner#nextDouble() consumes only the next token as a double from the input. It does not consume the new line you typed using the Enter on the keyboard while entering the two numbers. When the execution reaches operacia=in.nextLine();, this new line is consumed, never allowing the user a chance to type the operating string.
To solve this, you need to read the whole line using Scanner#nextLine() and convert it to a double:
String input = in.nextLine();
num1 = Double.parseDouble(input);
input = in.nextLine();
num2 = Double.parseDouble(input);
I believe the in.nextLine(); operation is reading only to the end of the line where you input 2 numbers. If you want your program to only consider the next line, you have to clear the current one first.
Try this, it should work:
System.out.println("Choose the operation");
in.nextLine(); //clear the current line
operacia=in.nextLine();
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(25 answers)
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 4 years ago.
So this is the code I am using:
System.out.println("Create a name.");
name = input.nextLine();
System.out.println("Create a password.");
password = input.nextLine();
But when it reaches this point it just says "Create a name." and "Create a password." both at the same time and then I have to type something. So it's basically skipping the Scanner parts where I need to type a String. After "Create a name." and "Create a password." is outprinted and I type then, both name and password are changing to what I typed in. How do I fix this?
This is the full class. I am just testing so it isn't actually going to be a program:
package just.testing;
import java.util.Scanner;
public class TestingJava
{
static int age;
static String name;
static String password;
static boolean makeid = true;
static boolean id = true;
public static void main(String[] args){
makeid(null);
if(makeid == true){
System.out.println("Yay.");
}else{
}
}
public static void makeid(String[] args){
System.out.println("Create your account.");
Scanner input = new Scanner(System.in);
System.out.println("What is your age?");
int age = input.nextInt();
if(age<12){
System.out.println("You are too young to make an account.");
makeid = false;
return;
}
System.out.println("Create a name.");
name = input.nextLine();
System.out.println("Create a password.");
password = input.nextLine();
return;
}
}
And sorry for my bad grammar. I am not English so it is kinda hard for me to explain this.
The nextInt() ate the input number but not the EOLN:
Create your account.
What is your age?
123 <- Here's actually another '\n'
so the first call to nextLine() after create name accept that as an input.
System.out.println("Create a name.");
name = input.nextLine(); <- This just gives you "\n"
User Integer.parseInt(input.nextLine()) or add another input.nextLine() after reading the number will solve this:
int age = Integer.parseInt(input.nextLine());
or
int age = input.nextInt();
input.nextLine()
Also see here for a duplicated question.
This doesnt actually tell you why the lines are being skipped, but as you're capturing names and passwords you could use Console:
Console console = System.console();
String name = console.readLine("Create a name.");
char[] password = console.readPassword("Create a password.");
System.out.println(name + ":" + new String(password));
You can also use next() instead of nextLine(). I have tested it in eclipse. its working fine.
next() will work but it will not read the string after space.