This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 3 years ago.
This is a simple test I made for my user input of my program but it skips over the second user input part. I am using sc.nextLine()
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int option;
String name;
System.out.println("1 or 2");
option = sc.nextInt();
if(option == 1)
{
System.out.println("Please enter a name");
name = sc.nextLine();
System.out.println(name);
}
}
The problem is that the enter ("\n") from the first user input isn't a number and is used for the second.
The quick and dirty option to fix this is to add a "sc.nextLine ();" after "nextInt()" line.
Like:
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int option;
String name;
System.out.println("1 or 2");
option = sc.nextInt();
sc.nextLine();
if(option == 1)
{
System.out.println("Please enter a name");
name = sc.nextLine();
System.out.println(name);
}
}
Use sc.nextLine() after sc.nextInt() in your code.
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
Hey there I was trying to make a phone contact project and I started to create a addContact() void that takes 2 inputs name and phone number in an infinite while loop. My problem is it doesn't take the phone number input after the name input. Can you please help me? Here is my code below
import java.util.*;
public class Problem {
private final Scanner scanner = new Scanner(System.in);
private final ArrayList<String> contactNames = new ArrayList<>();
private final ArrayList<Integer> contactNumbers = new ArrayList<>();
private final HashMap<String, Integer> contactMap = new HashMap<>();
public void addContact() {
while (true) {
System.out.println("Enter a phone number");
int contactNumber = scanner.nextInt();
if (contactNumbers.contains(contactNumber)) {
System.out.println("This number is already given in your contacts");
break;
}
System.out.println();
System.out.println("Enter your contacts name");
String name = scanner.nextLine();
name = name.toLowerCase();
if(contactNames.contains(name)){
System.out.println("This name is already given in your contacts");
break;
}
contactNumbers.add(contactNumber);
contactNames.add(name);
contactMap.put(name, contactNumber);
System.out.println("Person is successfully added to your contacts, here is your all contacts below");
System.out.println(contactMap);
System.out.println("Do you want to quit? -> 1=yes, 2=no");
int quit = scanner.nextInt();
if(quit==1)
break;
}
}
public static void main (String[] args){
Problem a = new Problem();
a.addContact();
}
}
Solution
scanner.nextLine(); //add this line
System.out.println();
System.out.println("Enter your contacts name");
This is to get rid of the newline created once the user has entered their phone number and pressed enter.
More info from here: Scanner is skipping nextLine() after using next() or nextFoo()?
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;
public class NoteIt {
public static void main(String[]args) {
Scanner s = new Scanner(System.in);
int Answer;
int i=2;
System.out.print("\nPlease Enter your Name: ");
String Name = s.nextLine();
System.out.println("Welcome to Note-It "+Name+", We hope you'll enjoy our application. ");
String[][] Main = new String[2][2];
Main[0][0]="Create new Note";
Main[1][0]="View My Notes";
System.out.println("\nPlease select what to do: \n");
for(int n=0; n<2; n++){
System.out.println((n+1)+") "+Main[n][0]);
}
System.out.print("\nPlease enter your response: ");
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print("\nTitle: ");
Main[i-1][0]=s.nextLine();
System.out.print("\nBody: ");
Main[i-1][1]=s.nextLine();
}
}
}
I don't know why it is not asking for Title?
According to #Rohit Jain, That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline.
Description here: check this question too
Try this it may help...
Scanner s = new Scanner(System.in);
int Answer;
int i=2;
System.out.print("\nPlease Enter your Name: ");
String Name = s.nextLine();
System.out.println("Welcome to Note-It "+Name+", We hope you'll enjoy our application. ");
String[][] Main = new String[2][2];
Main[0][0]="Create new Note";
Main[1][0]="View My Notes";
System.out.println("\nPlease select what to do: \n");
for(int n=0; n<2; n++){
System.out.println((n+1)+") "+Main[n][0]);
}
System.out.print("\nPlease enter your response: ");
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print("\nTitle: ");
Main[i-1][0]=s.next();
System.out.print("\nBody: ");
Main[i-1][1]=s.next();
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I don't know why, but the below code makes the user run the code again, whether they choose to or not. I've tried many things, but it doesn't work correctly.
Thanks!
public static void main (String [ ] args)
{
boolean a = true;
while (a)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer: ");
int x = scan.nextInt();
System.out.print("\n\nEnter a second integer: ");
int z = scan.nextInt();
System.out.println();
System.out.println();
binaryConvert1(x, z);
System.out.println("\n\nWould you like to run this code again? Enter \"Y\" or \"N\".");
System.out.print("Enter your response here: ");
String RUN = scan.nextLine();
String run = RUN.toLowerCase();
if (run.equals("n"))
{
a = false;
}
System.out.println();
System.out.println();
}
System.out.println("Goodbye.");
}
Scanner.nextInt() doesn't consume the line ending characters from the buffer, which is why when you read the value of the "yes/no" question with scan.nextLine(), you'll receive an empty string instead of the value the user entered.
A simple way to fix this is to explicitly parse the integer from raw lines using Integer.parseInt():
System.out.print("Enter an integer: ");
int x = Integer.parseInt(scan.nextLine());
System.out.print("\n\nEnter a second integer: ");
int z = Integer.parseInt(scan.nextLine());
This question already has answers here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
How to use scanner in java? [duplicate]
(7 answers)
Closed 7 years ago.
I have to say I am a newbie in java programming so I would like some help with the following question:
How can I delay a java program so that it waits for a user to give an input?
Here is my code:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
System.out.println("1st value= ");
Scanner aa1 = new Scanner(System.in);
System.out.println("2nd value= ");
Scanner bb1 = new Scanner(System.in);
What I get now from this code is "1st value= " and "2nd value= " displayed. After that the program requests the input.
Any ideas on how can I make this simple programm to wait for the user's answer before printing "2nd value= " in the console?
Ask for input from the user:
int index = aa1.nextInt();
You don't need to create two Scanner variables. You can use one of them and use the function input.nextInt(); to get both values, where input it's the name of the Scanner variable.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number1, number2;
System.out.println("1st value= ");
number1 = input.nextInt();
System.out.println("2nd value= ");
number2 = input.nextInt();
}
}
You must first import the Scanner before your Main class.
import java.util.Scanner;
You must then create a Scanner variable. I use "in" in my example:
Scanner in = new Scanner(System.in);
This will force the program to wait for user input.
` import java.util.Scanner;
public class Example{
public static void main( String[] args )
{
String name;
String address;
String cityStateZip;
Scanner in = new Scanner(System.in);
//User input
System.out.println("What is your name?");
name = in.nextLine();
System.out.println("Enter your address:");
address = in.nextLine();
System.out.println("Enter your City, State, and Zip code:");
cityStateZip = in.nextLine();`
You have to use only one Scanner variable. This is the solution :
import java.util.Scanner;
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println("1st value= " + str);
str = sc.nextLine();
System.out.println("2nd value= " + str);
}
}
You can see the different next***() on the doc on this link http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html.
I advise you to follow a Java formation if you want to code in this language.
You can use it as:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("1st value= "
+ input.nextInt()
+ "2nd value= "
+ input.nextInt());
}
For further reference to can go through Scanner java doc http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Add necessary imports by pressing control+shift+o after you have typed the main function.
public static void main(String agrs[])
{
// Take Scanner object
Scanner scanner =new Scanner(System.in);
// Ask user for 1st input
System.out.println("Enter 1st value:");
String value=scanner.nextLine();
System.out.println("1st value is ="+s);
// Ask user for 2nd input
System.out.println("Enter 2nd value:");
value=scanner.nextLine();
System.out.println("2nd value is ="+s);
}
NOTE : scanner.nextLine() will read the user input as a String,
If you need the input as int , use scanner.nextInt(); For float use scanner.nextFloat() and so on.
Now the console is halted for user input and then asks for second input.
Hope it helps.
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();