How to get a whole line of input using scanner [duplicate] - java

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I know a lot of people have asked this but everyones answer is to use scanner.nextLine() however this does not work for me. I have this code:
if (option == 1) {
System.out.println("What is the name of the goal the task is under?: ");
String goalName = scanner.next();
}
When I use .next() I can only get one word, however when I use .nextLine() the command line does not allow for the user to input anything so it asks "What is the name of the goal the task is under?:" and then continues on the program without letting input occur.

The following test code works:
package asdfsadf;
import java.util.Scanner;
public class Asdfsadf {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("What is the name of the goal the task is under?: ");
String goalName = scanner.nextLine();
System.out.println("You entered: ");
System.out.print(goalName);
}
}
So the only problem would be if you used scanner before this say you had scanner.nextInt() and that would leave an empty "end of the line" in the scanner. So the nextLine would take the end of the line from the previous call. To fix this, just create a new scanner object.

Related

java Scanner doesn't pop up [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
I'm new to java and I wrote this code
import java.util.Scanner;
public class GamingJava {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String Name;
int password;
String yEs;
System.out.print("hello sir what is your name? ");
Name = input.nextLine();
System.out.print("what is your password? ");
password = input.nextInt();
System.out.println("your name was "+Name+" and your password was "+password);
System.out.print("are you sure? ");
yEs = input.nextLine();
System.out.println(yEs);
}
}
It only ask the name and the password, but Java doesn't ask the last one how did that happen?
It asks for the input and immediately takes it as an empty string i.e. "".
Root Cause : The issue is because of the nextLine() method. Since while providing an input, user has to press the Enter key, the cursor/prompt on the console moves to next line. This line is taken as an empty line by the nextLine() method.
Solution : You must use the next() method as it looks out for the presence of space character for taking the input.
FYR the following line of code
yEs = input.nextLine();
should be changed to
yEs = input.next();

nextLine() printing a blank line at the start of output in Java [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 3 years ago.
Everything is fine except that it is giving a blank line in the first line of output and it is because of String a=scanner.nextLine(); but i am not able to figure out how to solve this problem of blank line occuring due to this function can anyone help?
The first input is the number of strings that will be there for the program.
For each String the program should print the even-odd indexing characters of the string separated with a space.
Input:
1.2
2.Hacker
3.Rank
Output:
1.
2.Hce akr
3.Rn ak
Expected Output:
1.Hce akr
2.Rn ak
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int times=scanner.nextInt();
for(int j=0;j<=times;j++){
String a=scanner.nextLine();
for(int i=0;i<a.length();i++){
if(i==0 || i%2==0){
System.out.print(a.charAt(i));
}
}
System.out.print(" ");
for(int i=0;i<a.length();i++){
if(i!=0 && i%2!=0){
System.out.print(a.charAt(i));
}
}
System.out.println();
}
}
}
Don't mix nextLine and other next calls. If you want to read everything until the user presses enter, instead, do this to the scanner immediately after creating it:
scanner.useDelimiter("\r?\n");
This tells the scanner that tokens occur once every line (versus the default, which is between any whitespace).

how to take multiple input using NextLine() in java [duplicate]

This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
how to take more inputs from user using Nextline() in java
if i use scn.Nextline() after reading one input it will work but giving a null in answer.
public class Mapdemo {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
System.out.println("enter the number of phoneaddress you want to store");
int n=scn.nextInt();
int j=0;
System.out.println("enter the phonenumber first and then name of the person phoneaddress");
Map<String, Long> m1=new HashMap<String,Long>();
for(int i=0;i<n;i++)
{
Long l=scn.nextLong();
String s=scn.nextLine();
m1.put(s,l);
}
System.out.println("enter the name to be checked");
String s2=scn.next();
System.out.println(m1.get(s2));
After using .nextInt() for the first time do:
scanner.nextLine();
after that to clear the line, nextLine() stops at the newline character (OS dependant).
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

Scanner reading inputs but outputting previous inputs [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(25 answers)
Closed 8 years ago.
Consider the following snippet:
System.out.print("input iteration cycles");
Scanner reader=new Scanner(System.in);
int iteration = reader.nextInt();
System.out.print("input choice: Var or par");
String choice=reader.nextLine();
System.out.print(choice);
I was hoping to get one line where it prints the first input and the next print out to only print the next input which is either var or par, however it seems that the second print prints out the second input in addition to the first input. Any idea why?
nextInt() does not advance to the next line. So you must make an explicit call to nextLine() immediately after it. Otherwise, choice will be given that newline. Just add this line to your code:
System.out.print("input iteration cycles");
Scanner reader=new Scanner(System.in);
int iteration = reader.nextInt();
reader.nextLine(); //<==ADD
System.out.print("input choice: Var or par");
String choice=reader.nextLine();
System.out.print(choice);

Scanner not scanning all fields? [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 8 years ago.
I was writing my first, well second if you count hello world, program and ran into a small issue.
My code:
import java.util.*;
class test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("What is your name: ");
String name = scan.nextLine();
System.out.print("What is your favorite number: ");
int favoriteNumber = scan.nextInt();
System.out.print("What is your favorite game: ");
String game = scan.nextLine();
}
}
So it scans in name and favoriteNumber but the program ends before asking the user to enter a favorite game. Essentially the user can only enter text twice instead of three times like I would like.
Just a guess: scan.nextInt() does not consume the trailing newline character after the user presses enter. The subsequent call to scan.nextLine() then finds a newline character waiting.
Try switching out int favoriteNumber = scan.nextInt(); for String favoriteNumber = scan.nextLine(); to see if that fixes the issue. If it does, my hypothesis is correct.
If that is the case, then you should probably use Integer.parseInt to convert this string into an integer. The problem here is effectively that you really want to collect 3 lines of input, where a line is defined as "any sequence of characters ending with a newline". Your program is currently written to request a line, an int, then a line, rather than "3 lines of input, one of which contains an int".
an exception must be thrown in here scan.nextInt();
Check whether you entered a proper int

Categories

Resources