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 take input for the number of names to be stored in an array from user and then using that i was taking names from the user ,first i tried to take names from the user using next() method and all the things were fine but when i tried to take input using nextLine() method the output was as shown below
package learningJava;
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
Output for the nextLine() method
Enter the number of names you are gonna enter
5
Enter the name of friend 1
Enter the name of friend 2
It is not prompting me to enter the name of friend 1 and directly skipping it and coming to the friend 2 line.
I am beginner in Java , i know the basic difference in next and nextLine() that next() doesn't take input after a space but nextLine() takes complete input , So what is happening here ??
just in for loop, just change "println" to "print" because nextLine() consumes new line character.
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
check this answer: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
I want to make a program that will read user inputs and will printout them gradually. But when I am running the code, in the Console area, the first line is automatically skipping. But when I am taking input as Integer, all is running well. Where is my fault?
import java.util.*;
public class MainClass {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int limit, i, j;
System.out.print("How many names you want to take: ");
limit = input.nextInt();
String[] name = new String[limit];
for (i = 0; i < name.length; i++) {
System.out.print("Enter your name: ");
name[i] = input.nextLine();
}
for (String output : name) {
System.out.println("Names are: " + output);
}
}
}
Console area:
How many names you want to take: 3
Enter your name: Enter your name: Saon
Enter your name: Srabon
Names are:
Names are: Saon
Names are: Srabon
Invoke input.nextLine() after the input.nextInt() in order to clear the new line character produced by pressing Enter key (when you enter int number);
Alternatively, you can read your int as Integer.valueOf(input.nextLine()).
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 3 years ago.
The question is that write a class named Seyyed includes a method named seyyed. I should save the name of some people in a String array in main method and calculate how many names begin with "Seyyed". I wrote the following code. But the output is unexpected. The problem is at line 10 where the sentence "Enter a name : " is printed two times at the first time.
import java.util.Scanner;
public class Seyyed {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of names :");
int n = in.nextInt();
String[] names = new String[n];
for (int i = 0; i < names.length; i++) {
System.out.println("Enter a name : ");
names[i] = in.nextLine();
}
int s = seyyed(names);
System.out.println("There are " + s + " Seyyed");
in.close();
}
static int seyyed(String[] x) {
int i = 0;
for (String s : x)
if (s.startsWith("Seyyed"))
i++;
return i;
}
}
for example When I enter 3 to add 3 names the program 2 times repeats the sentence "Enter a name : " and the output is something like this:
Enter the number of names :3
Enter a name :
Enter a name :
Seyyed Saber
Enter a name :
Ahmad Ali
There are 1 Seyyed
I can enter 2 names while I expect to enter 3 names.
The problem occurs as you hit the enter key, which is a newline \n character. nextInt() consumes only the integer, but it skips the newline \n. To get around this problem, you may need to add an additional input.nextLine() after you read the int, which can consume the \n.
Right after in.nextInt(); just add in.nextLine(); to consume the extra \n from your input. This should work.
Original answer: https://stackoverflow.com/a/14452649/7621786
When you enter the number, you also press the Enter key, which does an "\n" input value, which is captured by your first nextLine() method.
To prevent that, you should insert an nextLine() in your code to consume the "\n" character after you read the int value.
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of names :");
int n = in.nextInt();
in.nextLine();
String[] names = new String[n];
Good answer for the same issue: https://stackoverflow.com/a/7056782/4983264
nextInt() will consume all the characters of the integer but will not touch the end of line character. So when you say nextLine() for the first time in the loop it will read the eol left from the previous scanInt(), so basically reading an empty string. To fix that use a nextLine() before the loop to clear the scanner or use a different scanner for Strings and int.
Try this one:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of names :");
int n = in.nextInt();
in.nextLine();
String[] names = new String[n];
for (int i = 0; i < names.length; i++) {
System.out.println("Enter a name : ");
names[i] = in.nextLine();
}
int s = seyyed(names);
System.out.println("There are " + s + " Seyyed");
in.close();
}
static int seyyed(String[] x) {
int i = 0;
for (String s : x)
if (s.startsWith("Seyyed"))
i++;
return i;
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I am trying to create a simple String Revert program that does the following:
Prompts the user for an integer n
Creates an array of n Strings
Keeps reading character strings from user and stores them in the array, until the end of the array or user types "quit"
Prints the strings from the array in reverse order excluding empty slots
Here is my attempt so far:
-However, when i take input and make the size 4, the buffer only reads 3 strings and stops rather than 4.
import java.util.*;
import java.io.*;
class StringRevert {
public static void main(String[] args) {
String myArray[];
Scanner Scan = new Scanner(System.in);
System.out.println("Enter Number: ");
int size = Scan.nextInt();
myArray = new String[size];
for(int i=0; i<myArray.length; i++) {
myArray[i] = Scan.nextLine();
}
}
}
You need to put Scan.nextLine(); before starting of for loop.
public static void main(String[] args) {
String myArray[];
Scanner Scan = new Scanner(System.in);
System.out.println("Enter Number: ");
int size = Scan.nextInt();
myArray = new String[size];
Scan.nextLine();
for(int i=0; i<myArray.length; i++) {
System.out.println("Enter String");
myArray[i] = Scan.nextLine();
}
}
This question already exists:
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 8 years ago.
I need to create a for loop that fills 2 arrays I have set up. However whenever I run the program. It keeps skipping the next prompt and read in and I have no idea why?
My code is below. It skips the part where I ask them to enter a sentance/word.
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
* Date: 18/02/2015
* Time: 15:33
* UPDATE COMMENT ABOUT PROGRAM HERE
*/
public class Week5Q2
{
public static void main(String[] args)
{
final int HOWMANY=5;
Scanner keyboard= new Scanner(System.in);
int choice;
int number [] = new int [5];//mechanism1
String sentence [] = new String [5];
for(int count=0;count<HOWMANY;count++)
{
System.out.println("Please enter a number below");
number[count]=keyboard.nextInt();
System.out.println("\nPlease enter a word below");
sentence[count]=keyboard.nextLine();
}//for
System.out.println("Thank you, now press 1 if you wish to see what you have entered");
choice=keyboard.nextInt();
switch(choice)
{
case 1:
for(int count=0;count<HOWMANY;count++)
{
System.out.println("Here are your numbers");
System.out.println("\n" + number[count]);
System.out.println("Here are your words");
System.out.println("\n" + sentence[count]);
}//for
}//switch
}//main
}//class
You must call nextLine() after nextInt(), to consume the end of the line that contained the int. Otherwise, the end of the line will be consumed when you expect to read the sentence/word.
for(int count=0;count<HOWMANY;count++)
{
System.out.println("Please enter a number below");
number[count]=keyboard.nextInt();
keyboard.nextLine();
System.out.println("\nPlease enter a word below");
sentence[count]=keyboard.nextLine();
}//for
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?
(13 answers)
Closed 9 years ago.
I used Java and I tried to write program that reads a number of patients and then let the employee enter their names and ages.
and I want the program reads full name such as (Maha Saeed) so I wrote the code like this but I don't know why it does not work
name = scan.nextLine();
and this is the full code
import java.util.*;
public class Answer1
{
static Scanner scan = new Scanner (System.in);
public static void main (String[] args)
{
int age ;
int PatientNumber ;
int PatientNO;
String name ;
System.out.print("Enter number of patients :");
PatientNumber = scan.nextInt();
PatientNO = 1;
while ( PatientNO <= PatientNumber)
{
System.out.println("Patient #" +PatientNO);
System.out.print("Enter patient's Name: ");
name = scan.nextLine(); //<- here is the problem if I write scan.next it works but it reads only the first name
System.out.print("Enter patient's Age: ");
age= scan.nextInt();
PatientNO = PatientNO + 1;
}
}
}
thanks all
See Why can't I enter a string in Scanner(System.in), when calling nextLine()-method? and Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
Simple solution, you can consume the \n character:
scan.nextLine();
name = scan.nextLine();