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();
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
import java.util.Scanner;
import static java.lang.System.out;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int Safeties;
out.print("Safeties:");
Safeties = input.nextInt();
out.print("Home Team:");
String HomeTeam = input.nextLine();
out.print("Touchdowns:");
double Touchdown2 = input.nextDouble();
}
}
The output is:
Safties:
Home Team:Touchdowns:
But it's supposed to be
Safties:
Home Team:
Touchdowns:
I'm assuming it's because I have an int above a string but I don't know how to fix this problem. I need them in that order. I'm completely lost. Can anyone help?
Add input.nextLine(); below out.print("Home Team:");
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 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 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 exists:
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 9 years ago.
I have problem with Scanner
When I run the program it skips this one after
System.out.println("name");
n1=s.nextLine();
This is the program "CEmploye " is a class
package Ex5_2;
import java.util.*;
public class XXXX {
public static void main(String[] args) {
int input;
int c1 ;
String n1;
Date d1 = null;
float p1;
float [] t = new float[3];
System.out.println("give nb of emp");
Scanner s = new Scanner(System.in);
input=s.nextInt();
Vector v = new Vector(input);
for(int i=0 ;i <input;i++)
{
System.out.println("cin");
c1=s.nextInt();
System.out.println("name");
n1=s.nextLine();
System.out.println("price");
p1=s.nextFloat();
for(int k=0 ; k<3;k++)
{
System.out.println("nb of hour");
CEmploye.tab[k]=s.nextFloat();
}
CEmploye emp = new CEmploye(c1,n1,d1,p1);
emp.CalculSalaire();
System.out.println(emp.salaire);
}
}
}
Can anyone give me solution ?
System.in's buffer isn't flushed until it gets a newline. So you can't use nextInt() or nextFloat() because they block until a newline.
You'll need to read everything on a line by itself then parse it (with some validation as needed):
cl = Integer.parseInt(s.nextLine());
and
pl = Float.parseFloat(s.nextLine());
and
CEmploye.tab[k]=Float.parseFloat(s.nextLine());
You can't use n1=s.nextLine(); with n1=s.nextInt(). Use n1=s.next();
nextInt() only reads the next integer available and leaves a newline character in the inputstream.
Your s.nextLine() then gets consumed thus not prompting for additional inputs.
Simply add another nextLine() to read more lines
c1=s.nextInt();
This just reads the integer value not the end of line. So when you do
n1=s.nextLine();
it just reads the end of line that you provided by pressing the enter while providing the integer input for the previous variable (c1) and thus seems like it skipped the input. (If you put an integer and some string in the same line when taking c1 input, you will get values for c1 and n1 both. You can check the same)
In order to fix it, either put nextLine() input after each nextInt(). Hope it helps.