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
Related
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.
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);
Is the second part a good practice in order to avoid user input mismatch?
public class StringsAsPrimitiveDate{
Scanner input= new Scanner(System.in);
System.out.println("Please enter your number:");
int numberOne=input.nextInt();
}
Or
public class StringsAsPrimitiveDate{
Scanner input= new Scanner(System.in);
System.out.println("Please enter your number:");
String numberOne=input.next();
int result = Integer.parseInt(numberOne);
}
Thank you
A common problem with nextInt is that it leaves behind a new line character which may be accidentally automatically consumed by the next scan. Consider the following code:
import java.util.Scanner;
class Main{
public static void main(String args[]) {
int rollNo;
String name;
Scanner scan=new Scanner(System.in);
while (true) {
System.out.print("Enter the roll no of the student: ");
rollNo=scan.nextInt();
System.out.print("Enter the name of the student: ");
name=scan.nextLine();
System.out.printf("Roll no. %d, Name: %s has been registered%n",rollNo, name);
}
}
}
A sample run:
Enter the roll no of the student: 1
Enter the name of the student: Roll no. 1, Name: has been registered
Enter the roll no of the student: Arvind
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Main.main(Main.java:10)
Note that this problem can occur even with next but not with nextLine as shown with the following code:
import java.util.Scanner;
class Main {
public static void main(String args[]) {
int rollNo = 0;
String name;
boolean valid = true;
Scanner scan = new Scanner(System.in);
while (true) {
do {
System.out.print("Enter the roll no of the student: ");
try {
rollNo = Integer.parseInt(scan.nextLine());
} catch (Exception e) {
System.out.println("The value should be an integer.");
valid = false;
}
} while (!valid);
System.out.print("Enter the name of the student: ");
name = scan.nextLine();
System.out.println("Roll no. " + rollNo + ", Name: " + name + " has been registered");
}
}
}
A sample run:
Enter the roll no of the student: 1
Enter the name of the student: Arvind
Roll no. 1, Name: Arvind has been registered
Enter the roll no of the student: a
The value should be an integer.
Enter the roll no of the student: 12.5
The value should be an integer.
Enter the roll no of the student: 2
Enter the roll no of the student: Avinash
The value should be an integer.
Enter the roll no of the student:
Conclusion: it is better to use nextLine with proper logic (e.g. exception handling, loop back on invalid input etc.) instead of next or nextInt to avoid issues mentioned above. I also recommend you go through documentation on Scanner.
I am a beginner in java programming. I have a question on whats going on, whenever i try to compile it, it keeps giving me error like this :
Exception in thread "main" java.util.InputMissMatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at MoreUserInputOfData.main(MoreUserInputOfData.java:28)
if someone would like to help me clean up my code as well, it wouldn't hurt..
import java.util.Scanner;
public class MoreUserInputOfData{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String firstName, lastName, loginName;
int grade, studentID;
double gpa;
System.out.println("Please enter the following information, so i cann sell it for a profit!");
System.out.print("First name: ");
firstName = keyboard.next();
System.out.print("Last name: ");
lastName = keyboard.next();
System.out.print("Grade (9-12): ");
grade = keyboard.nextInt();
System.out.print("Student ID: ");
studentID = keyboard.nextInt();
System.out.print("Login: ");
loginName = keyboard.next();
System.out.print("GPA (0.0-4.0): ");
gpa = keyboard.nextDouble();
System.out.println("");
System.out.println("Your information:");
System.out.println("Login:"+loginName);
System.out.println("ID: "+studentID);
System.out.println("Name: "+lastName+", "+firstName);
System.out.println("GPA: "+gpa);
System.out.println("Grade: "+grade);
}
}
Look at your code at line 28. When you input data, maybe you input the wrong type that the variable can't accept. For example: nextDouble require dot like 0.3 but you input 3.
For nextDouble : InputMismatchException -- if the next token does not match the Float regular expression, or is out of range
you should write it by prevent exception like:
// find the next double token and print it
// loop for the whole scanner
while (keyboard.hasNext()) {
...
// if the next is a double, print found and the double
System.out.print("GPA (0.0-4.0): ");
if (keyboard.hasNextDouble()) {
gpa = keyboard.nextDouble();
} else {
System.out.print("Not double");
}
...
}
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.