Java is not taking user input for the first variable [duplicate] - java

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

Related

Last String Input (y/n) is overlooked by the Program. While loop is not breaking

Here is a basic while loop program, where the program at the end should ask the user if you want to keep going or not. The bug is that the program doesn't let me input (y/n) which is the last String Input.
This does not happen when the last input is an integer value.
import java.util.Scanner;
public class lol {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age;
String name = "";
String height = "";
String userOption = "";
while (!userOption.equals("n"))
{
System.out.println();
System.out.print("Enter your name: ");
name = sc.nextLine();
System.out.println();
System.out.print("Enter your age: ");
age = sc.nextInt();
System.out.println();
System.out.print("Enter your height: ");
height = sc.nextLine();
System.out.println();
System.out.println("Do you want to keep going? (y/n)");
// The program over looks this line of code
userOption = sc.nextLine();
if(userOption.equals("y"))
{
System.out.println("Breaking");
break;
}
else
{
continue;
}
}
}
}
See this topic, additional nextLine() call could be a workaround
Scanner is skipping nextLine() after using next() or nextFoo()?
Minor adjustments to the code will fix its behaviour ;)
Scanner sc = new Scanner(System.in);
int age;
String name = "";
String height = "";
String userOption = "";
while (true) {
System.out.print("Enter your name: ");
name = sc.nextLine();
System.out.println();
System.out.print("Enter your age: ");
age = sc.nextInt();
System.out.println();
System.out.print("Enter your height: ");
height = sc.nextLine();
System.out.println();
System.out.println("Do you want to keep going? (y/n)");
// The program over looks this line of code
userOption = sc.nextLine();
if (userOption.equals("y")) {
// nothing
System.out.println("Continuing");
} else {
System.out.println("Stopping");
break;
}
}
System.exit(0);
I would however agree that you should take a look at Scanner is skipping nextLine() after using next() or nextFoo()?

User Input - Scanner Skips user inptu [duplicate]

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.

How to only allow input of letters? [duplicate]

This question already has answers here:
Check if String contains only letters
(17 answers)
Closed 7 years ago.
I am writing some code but am unsure how to set it so users can only input certain letters for grade. (A,B,C,D,F)
import java.io.IOException;
import java.util.Scanner;
public class Forloop {
public static void main(String[] someVariableName) throws IOException {
String Grade1;
String Grade2;
String Grade3;
String Grade4;
String Grade5;
Scanner in = new Scanner( System.in );
System.out.println("This program will ask you to input five grades \n");
System.out.println("Please enter leter grade one. \n");
Grade1 = in.next();
System.out.println("Please enter leter grade two. \n");
Grade2 = in.next();
System.out.println("Please enter leter grade three. \n");
Grade3 = in.next();
System.out.println("Please enter leter grade four. \n");
Grade4 = in.next();
System.out.println("Please enter leter grade five. \n");
Grade5 = in.next();
System.out.println("Your grades are ==>");
System.out.println(Grade1);
System.out.println(Grade2);
System.out.println(Grade3);
System.out.println(Grade4);
System.out.println(Grade5);
}
}
Variables should start with lowercase letter.
To ensure only valid data is entered, loop back and ask again if it's wrong.
Letter is spelled with 2 t's.
Use nextLine(), not next().
Easiest way to check valid text (for this case), is a regular expression, e.g.
String grade1;
do {
System.out.println("Please enter letter grade one: ");
grade1 = in.nextLine();
} while (! grade1.matches("[ABCDF]"));
Use this approach.
import java.io.IOException;
import java.util.Scanner;
public class Forloop {
public static void main(String[] someVariableName) throws IOException {
String[] grades = new String[5];
Scanner in = new Scanner( System.in );
System.out.println("This program will ask you to input five grades \n");
for(int i = 0; i < grades.length; i++) {
System.out.println("Please enter letter grade " + i + "\n");
grades[i] = in.nextLine();
while(!grade[i].matches("[abcdfABCDF]")) {
System.out.println("Please enter a grade from A to F");
grades[i] = in.nextLine();
}
}
System.out.println("Your grades are ==>");
for(int i = 0; i < grades.length; i++) {
System.out.println(grades[i]);
}
}
}

Do-while loop inside a method [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
Everything is working fine in this code sample that i did, but everytime it calls the do-while loop on the output screen, it skips the "First name" input, and goes straight to "How old are you?" input. why is that happening and how can i fix it? I want to be able to repeat the whole thing without skipping "First name" when i pick 'y' to perform the loop.
public class WeightGoalTest
{
public static void main(String[]args)
{
doWhile person = new doWhile();
person.userInput();
}
}
import java.util.*;
public class doWhile
{
Scanner keyboard = new Scanner(System.in);
private String inputFn = "";
private int inputAge = 0;
private int inputHeight = 0;
private double inputWeight = 0.0;
private int inputCalculation = 0;
private int inputGender = 0;
private char loop;
public void userInput()
{
do
{
System.out.println("First Name: ");
inputFn = keyboard.nextLine();
System.out.println("How old are you?");
inputAge = keyboard.nextInt();
System.out.println("Gender 1 - Male: ");
System.out.println(" 2 - Female: ");
inputGender = keyboard.nextInt();
System.out.println("What is your Height in inches? ");
inputHeight = keyboard.nextInt();
System.out.println("Current Weight in pounds: ");
inputWeight = keyboard.nextDouble();
System.out.println("\nDo you want to use the calculator again? (y/n)");
loop = keyboard.next().charAt(0);
}while(loop == 'y');
}
}
Please note:
String java.util.Scanner.next() - Returns:the next token
String java.util.Scanner.nextLine() - Returns:the line that was skipped
Change your code [do while initial lines] as below:
System.out.println("First Name: ");
inputFn = keyboard.next();
use keyboard.next() instead of .nextLine(). For further informations have a look in the Java API concerning the Scanner class. nextLine() will
Advance this scanner past the current line and return the input that was skipped."

scan.nextLine does not work [duplicate]

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

Categories

Resources