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();
Related
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
This question already has answers here:
Scanner and nextInt discard integer
(3 answers)
Closed 4 years ago.
Why usage of nextLine() is not efficiently takes the input?
If i use next() instead of nextLine() the code is super good but why? whats the reason behind it.Where and all i should use next and nextLine();
import java.util.Scanner;
public class Sample{
public static void main(String args[]) {
String a,b;
Scanner scan = new Scanner(System.in);
q = scan.nextInt();
for(int m=0;m<q;m++) {
a = scan.nextLine();
b = scan.nextLine();
System.out.println(a + "" + b);
}
}
}
Expected:
2
mnopm
nop
mnop nop
abcdee
bee
abcdee bee
Actual:
2
mnop
mnop(output appears before taking the 2nd input)
abcee
bee
abcee bee
Replace q = scan.nextInt(); with q = Integer.valueOf(scan.nextLine()); This will do the trick
The Scanner.nextInt() method does not read the newline character in your input created by hitting enter. Please refer this link for more info
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was working on a project and I figured out that when I read more than one String from one obj of Scanner it may skip one and it's solved when I make more than 1 obj but why does that happen I mean what it's the cycle that processor go through !!
in this code variable z is skipped from reading and the program end so any one could help me figuring this out please and thanks in advance
package calculater;
import java.util.Scanner;
public class Calculater {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String x = scan.nextLine();
double y = scan.nextDouble();
String z = scan.nextLine();
}
}
You could try to write
double y = Double.parseDouble(scan.nextLine());
instead of
double y = scan.nextDouble();
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was trying to reverse the String using pre-defined method reverse() which is available in StringBuffer
I took input from the user and while printing the reverse string I used toString() method to avoid an extra space
import java.util.*;
public class Small {
public static void main(String a[])
{
int num;
Scanner sc=new Scanner(System.in);
num = sc.nextInt();
while(num>0)
{
String t;
t=sc.nextLine();
StringBuffer sb=new StringBuffer(t);
sb.reverse();
System.out.println(sb.toString());
num--;
}
}
}
Input:
2
hello
welcome
Output:
<Empty line>
olleh
Can anyone please advise why this blank space is coming and also not getting second output?
sc.nextInt() doesn't consume the newline at the end of the line containing the number.
Add sc.nextLine(); after it:
num = sc.nextInt();
sc.nextLine();
This question already exists:
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 8 years ago.
When I run the following code it takes the name as input, also the code but then then it bypass the address and the program stops! What is the Problem?
import java.util.Scanner;
class demo
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String name;
String address;
int code;
System.out.println("Enter Your NAME : ");
name = input.nextLine();
System.out.println("Enter Your CODE : ");
code = input.nextInt();
System.out.println("Enter Your ADDRESS : ");
address = input.nextLine();
}
}
Add input.nextLine(); after code = input.nextInt(); to accept the carriage return character left behind by the nextInt()