Trouble using nextInt and nextLine() - java

It's not letting me put my name in but it does the age works fine.
I know i can change the order of the statements but is there another way I could do it?
import java.util.Scanner;
public class ScannerErr2
{
public static void main(String [] args)
{
Scanner keyboard= new Scanner(System.in);
String name;
int age;
System.out.print("Enter your age : ");
age= keyboard.nextInt();
System.out.print("Enter your name: ");
name= keyboard.nextLine();
System.out.println("Age : "+age);
System.out.println("Name: "+name);
}
}

You problem is that the next int doesn't consider the new line character which goes in the input for your name part. Hence name is returned as blank.
You can change your code in 2 ways:
System.out.print("Enter your age : ");
age = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter your name: ");
name = keyboard.nextLine();
or
System.out.print("Enter your age : ");
age = Integer.parseInt(keyboard.nextLine().trim());
System.out.print("Enter your name: ");
name = keyboard.nextLine();
I personally like the second way.

Related

how to have multiple scanners for multiple user inputs

I am writing a small program to create a password based off what the user inputs their first middle last name and the birthday and create a password off that and i don't know what i am doing but believe i need multiple scanners but when i do i get this error "variable scan is already defined in method main(string[]) Scanner scan = new Scanner(System.in);"
here is my code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("Please Enter your first name: ");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
System.out.print("Please Enter your last name: ");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
scan.close();
}
}
anythoughs on what could work?
You do not need multiple Scanner objects to accept multiple inputs. You should create one Scanner object and use it to collect as many inputs as you want.
Here is an example:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your first name: ");
String fname = input.nextLine();
System.out.println("Enter your last name: ");
String lname = input.nextLine();
System.out.println("Enter your age: ");
int age = Integer.parseInt(input.nextLine());
System.out.println("Your details as entered:");
System.out.println("First Name: " + fname);
System.out.println("Last Name: " + lname);
System.out.println("Age: " + age);
}
}
Also some extra resources for you (for Scanner class): https://www.w3schools.com/java/java_user_input.asp
Edit:
replaced next() with nextLine()
replaced nextInt() with Integer.parseInt(input.nextLine());
Thank you very much for the information #Arvind Kumar Avinash. Reference thread: Scanner is skipping nextLine() after using next() or nextFoo()?.
That is because you cannot have two variables with the same name within the same scope.
You should rename one of them ..
Note that you did the same with the String variable name ;)
If you want to keep the same name for both use, then you should not re-declare the variable(s):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan;
String name;
System.out.print("Please Enter your first name: ");
scan = new Scanner(System.in);
name = scan.nextLine();
scan.close();
System.out.print("Please Enter your last name: ");
scan = new Scanner(System.in);
name = scan.nextLine();
scan.close();
}
}
Notes:
in this example, you will loosen the first value entered by the user. You might want to use an other variable name to store the last name.
there is obviously no reason to instantiate two different Scanner objects with the same InputStream.

How to make "un-entered" user input show "not available" -- Java

I am just a beginner, so any help would be appreciated.
So here is my code:
import java.util.Scanner;
public class AccountScanner {
public static void main (String[] args) {
String name, course;
int age;
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextInt();
System.out.print("Enter Course: ");
course = input.next();
System.out.println();
System.out.println("Name:" +name);
System.out.println("Age:" +age);
System.out.println("Course:" +course);
System.exit(0);
}
}
but what if the user did enter one of the details. What can I do to make it display "Not Available" instead
example:
enter name: Zack
enter age:
enter course: BSCS
output would be:
Name: Zack
Age: Not available
Course: BSCS
You can use a conditional operator to check if the inputs are empty and show the result correspondingly as follows:
String name, course, age;
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextLine();
System.out.print("Enter Course: ");
course = input.nextLine();
System.out.println();
System.out.println("Name:" +(name.isEmpty()?"Not Available":name));
System.out.println("Age:" +(age.isEmpty()?"Not Available":new Integer(age)));
System.out.println("Course:" +(course.isEmpty()?"Not Available":course));
System.exit(0);

Scanner is not taking input of another string and skipping it

Why scanner is not taking input of another string and skepping it? I cant understand, here is my code:
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String name;
String address;
int age;
System.out.println("Enter your name");
name = s.nextLine();
System.out.println("My name is :" + name);
System.out.println("Enter your age");
age = s.nextInt();
System.out.println("My age is :" + age);
System.out.println("Enter your address");
address = s.nextLine();
System.out.println("My address is :" + address);
}
}
Output :
Enter your namedkMy name is :dkEnter your age22My age is :22Enter your addressMy address is :
It is simple. You can use s.nextLine(); after the age = s.nextInt();
Scanner provides a method nextInt() to request user input and does not consume the last newline character (\n).
System.out.println("Enter your age");
age = s.nextInt();
s.nextLine(); // consumes the \n character
System.out.println("My age is :" + age);
You should close the scanner after the last system out
Place below statement after Statement.out.println("My age is",age);
s.nextLine();
Above statement will do flush, which will clear the cache location in order to accept new input.

FileWriter not receiving all input and mysterious dangling newline issue

I am trying to write a Java program in which the user specifies how many "student records" they would like to input, followed by the student's name, age, and GPA, which then gets stored as text. However, I am having a problem with my text not including all entered data and a mysterious dangling newline that I cannot get rid of.
Here is my program:
import java.io.*;
import java.util.Scanner;
public class CreateFile {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
FileWriter fwriter = new FileWriter("c:\\Students.dat");
PrintWriter StudentFile = new PrintWriter(fwriter);
String name = " ";
String next = " ";
int age = 0;
int hm = 0;
double gpa = 0.0;
System.out.print("How many student records would you like to enter: ");
hm = input.nextInt();
for (int x = 1; x <= hm; x++) {
System.out.print("Enter Name: ");
name = input.nextLine();
input.nextLine();
System.out.print("Enter Age: ");
age = input.nextInt();
System.out.print("Enter GPA: ");
gpa = input.nextDouble();
next = input.nextLine();
StudentFile.println(name);
StudentFile.println(age);
StudentFile.println(gpa);
}
StudentFile.close();
System.exit(0);
}
}
Here is sample input and output to illustrate my issues:
run:
How many student records would you like to enter: 3
Enter Name: Jon
Enter Age: 20
Enter GPA: 3.4
Enter Name: Bill
Enter Age: 24
Enter GPA: 3.6
Enter Name: Ted
Enter Age: 34
Enter GPA: 3.9
This is the produced text file:
20
3.4
Bill
24
3.6
Ted
34
3.9
Why doesn't it store the first name entered? Why isn't there a newline in the first entry, but it is in the others?
The problem is that you're using nextLine() when you need to be using next(). I'm assuming you put the second input.nextLine() in there because you were initially having a problem where it would print out "Enter Name: " and then immediately "Enter Age: ". nextLine() is telling your program to skip whatever is there, and not to wait for it. The reason that this paradigm worked at all for any of your entries is that you put next = input.nextLine() at the bottom of your loop. Here's a fix:
package createfile;
import java.io.*;
import java.util.Scanner;
public class CreateFile {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
FileWriter fwriter = new FileWriter("c:Students.dat");
PrintWriter StudentFile = new PrintWriter(fwriter);
String name = " ";
String next = " ";
int age = 0;
int hm = 0;
double gpa = 0.0;
System.out.print("How many student records would you like to enter: ");
hm = input.nextInt();
for (int x = 1; x <= hm; x++) {
System.out.print("Enter Name: ");
name = input.next();
System.out.print("Enter Age: ");
age = input.nextInt();
System.out.print("Enter GPA: ");
gpa = input.nextDouble();
StudentFile.println(name);
StudentFile.println(age);
StudentFile.println(gpa);
}
StudentFile.close();
System.exit(0);
}
}
You could also just move your input.nextLine() above name=input.nextLine() and it would have the same effect.
The other examples only work if you don't have names like "James Peter" - in their code examples only James would be saved as name.
I'd prefer this:
System.out.print("How many student records would you like to enter: ");
hm = input.nextInt();
input.nextLine();
for (int x = 1; x <= hm; x++) {
System.out.print("Enter Name: ");
name = input.nextLine();
System.out.print("Enter Age: ");
age = input.nextInt();
input.nextLine();
System.out.print("Enter GPA: ");
gpa = input.nextDouble();
input.nextLine();
StudentFile.println(name);
StudentFile.println(age);
StudentFile.println(gpa);
}
This is the corrected for loop:
for ( int x = 1; x <= hm; x++ )
{
System.out.print( "Enter Name: " );
name = input.next();
input.nextLine();
System.out.print( "Enter Age: " );
age = input.nextInt();
input.nextLine();
System.out.print( "Enter GPA: " );
gpa = input.nextDouble();
next = input.nextLine();
StudentFile.println( name );
StudentFile.println( age );
StudentFile.println( gpa );
}
Some things you may want to consider:
Handle the IOException - it should not be ignored!!
Use the methods hasNextXXX() of the Scanner to check if something is available.
Refactor your usage of the variable next, it's never really used.
It's not necessary to call System.exit( 0 ) from the main method - rather use the return statement with a meaningful value.

How to execute Print statement after getting input from user

I have code which is:
import java.util.Scanner;
class shoham
{
public static void main (String args[])
{
System.out.println("Enter your name: ");
Scanner name = new Scanner(System.in);
System.out.println("Your name is: ");
System.out.print(name.nextLine());
}
}
When I execute this command, I get output like:
Enter your name:
Your name is:
***** (user input)
***** (output)
Can anyone give a code that gives output like:
Enter your name: **** - (user input)
Your name is: **** - (output from user input)`
Well you're reading input after your print statements. You probably want:
Scanner in = new Scanner(System.in); // do this first, and give it a
// better name
System.out.print("Enter your name: "); // use print to keep it on one line
String name = in.nextLine(); // read input, store it
System.out.print("Your name is: ");
System.out.println(name); // print name
Enter your name: Joe
Your name is: Joe
You may try something like this ie try using print only instead of println:-
Scanner name = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = name.nextLine();
System.out.print("Your name is: ");
System.out.println(name);
try this
public static void main (String args[])
{
System.out.print("Enter your name: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine()
System.out.println("Your name is: "+name);
}
here is your code:
System.out.println("Enter your name: ");
Scanner name = new Scanner(System.in);
System.out.println("Your name is: "+name.nextLine());
You have got lot of sloutions here, but you need to know the issue with your code is,
You did not store the input from the user.
System.out.print("Enter your name: ");
String name = name.nextLine(); // read input, store it

Categories