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

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()

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();

User Input of sentence in Java [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
Whenever I use sc.nextLine in java I get this error. It skips one line. Anybody knows how to fix it. sc.nextLine fails for multiple test cases as takes skips one line and then take input. Anybody knows another way to take sentence input in java?
For Input:
2
geeksforgeeks
geeks for geeks
your output is:
geeksforgeeks
Code:
class GFG {
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
while(testcases--!=0)
{
String str=sc.nextLine();
System.out.println(str);
}
//code
}
}
you have to read a line after int testcases=sc.nextInt(); because reading the Int from scanner is not reading a line Termination chars....
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
sc.nextLine(); //<--HERE!
while(testcases--!=0)
{
String str=sc.nextLine();
System.out.println(str);
}

Java: Why while(true) loop breaks automatically when using nextInt()? [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
I am a beginner programmer student from Finland, this is my first post here so hello everyone! So, I am writing this Java program that reads user inputs and creates new Books according to the inputs and then adds them to list and sorts them according to their recommended ages and names. The following code doesn's seem to work, the loop breaks automatically after one go. I actually fixed the issue using "Integer.valueOf" instead of nextInt but I started wondering why the nextInt doesn't work here?
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
ArrayList<Book> books = new ArrayList<>();
while (true) {
System.out.println("Enter books name, blank will end the loop:");
String bookName = reader.nextLine();
if (bookName.equals("")) {
break;
}
System.out.println("Enter the books recommended age:");
int minimAge = reader.nextInt();
Book book = new Book(bookName, minimAge);
books.add(book);
}
System.out.println("Total " + books.size() + " books.");
System.out.println();
System.out.println("Books:");
Comparator<Book> vertaus = Comparator
.comparing(Book::getMinimAge)
.thenComparing(Book::getName);
Collections.sort(books, vertaus);
for (Book k : books) {
System.out.println(k);
}
}
}
While entering number for minimAge when you press enter, the scanner takes two input, the number and a EOF character. As a result EOF character is set as the next bookname. Which is equivalent to empty string and breaks the loop. To skip this add the following code
int minimAge = reader.nextInt();
reader.nextLine();

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

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.

Java String Output Error Using Java.Util.Scanner [duplicate]

This question already has answers here:
Scanner issue when using nextLine after nextXXX [duplicate]
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 9 years ago.
I am writing a basic code which will be taking the number of question as input. After that I am using a for loop to Read each question. However, It is not asking for the first question and directly moving to the second question. Here is the code :
import java.util.Scanner;
class quiz
{
static Scanner in = new Scanner(System.in);
public static void main(String args[])
{
int numberofquestion,i;
String[] question;
question = new String[200];
System.out.print("Enter the number of Question : ");
numberofquestion = in.nextInt();
System.out.println("The number of question you entered is : "+numberofquestion);
for(i=0;i<=numberofquestion-1;i++)
{
System.out.print("Enter the Question : ");
question[i] = in.nextLine();
}
for(i=0;i<numberofquestion;i++)
{
System.out.print("Question = "+question[i]);
}
}
}
One solution is to add
in.nextLine();
after the in.nextInt();
Why? Because nextInt() doesn't read the "newline" character in the input, so your nextLine() in the loop was consuming it.

Categories

Resources