I am an extreme beginner in Java and I can't seem to get the user input. I am using eclipse mars. My code:
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("What is your name?");
Scanner UserName = new Scanner(System.in);
System.out.println(UserName);
}
}
You need to first create your Scanner, then call nextLine on it to get input from the user:
import java.util.Scanner;
class NameAsker {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("What is your name? ");
String userName = input.nextLine();
System.out.println("Your name is: " + userName);
}
}
try
System.out.println(UserName.nextLine());
Read Oracle docs for more info
you can use like.
Scanner input=new Scanner(System.in);
if you want to get integer.
int number=input.nextInt();
System.out.Println(number);
for String:
String str=input.nextLine();
There are other method for taking user input like bufferedReader and BufferedInputStream for more details check Java Docs.
Related
This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 2 years ago.
I am working on a java assignment for class. Very basic but I just starting to learn to program and this is my first assignment. The assignment is to write a Java program that asks for your name and then greet you like “hello {name}” in java and I'm using online IDE- Browxy
This is my attempt and reads errors every time I run it.
code:
import java.util.Scanner;
class usersName {
public static void main(String[] args) {
String usersName;
System.out.print("Please enter your name: ");
usersName = java.util.Scanner;
System.out.println("Hello, " + usersName! ");
}
}
look for something like this :
`import java.util.Scanner;
class usersName {
public static void main(String[] args) {
try(Scanner scanner = new Scanner( System.in ))
{
String usersName;
System.out.print("Please enter your name: ");
usersName = scanner.next();
System.out.println("Hello, " + usersName+"! ");
}catch( Exception e){
System.out.println("you didn't take any name");
}
}
}`
It says on the line String correctname="Pisay"; syntax error delete this token
package StringExple;
import java.util.Scanner;
public class StringExple{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String correctname=”Pisay";
System.out.println(“Enter your username:”);
String username = sc.nextLine();
if(username.equals(correctname)){
System.out.println(“Authorized user!!”);
} else
System.out.println(“Unauthorized user!!”);
if(username.equalsIgnoreCase(correctname)) {
System.out.println(“Authorized user!!”);
} else
System.out.println(“Unauthorized user!!”);
}
}
Couple of syntax issue is there, after fixing working.
Say Double Quotes " in print statements are not proper.
It's simple syntax issue if you concentrate then you should be able to fix them by own.
As you are doing 2 tests for both equals() and equalsIgnoreCase(), you may verify using equalsIgnoreCase() only because it will include equals() check also.
package sep2020;
import java.util.Scanner;
public class StringExple {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String correctname = "Pisay"; // String input
System.out.println("Enter your username:");
String username = sc.nextLine();
if (username.equalsIgnoreCase(correctname)) {
System.out.println("Authorized user!!");
} else
System.out.println("Unauthorized user!!");
}
}
Output:
Enter your username:
test
Unauthorized user!!
Enter your username:
Pisay
Authorized user!!
The adjusted code is below - the only issue is using ” opposed to ".
Also please format you code for future questions.
import java.util.Scanner;
class StringExple{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = "Pisay"; // String input
System.out.println("Enter your username:");
String username = sc.nextLine();
if(username.equals(correctname)){
System.out.println("Authorized user!!");
}else System.out.println("Unauthorized user!!");
if(username.equalsIgnoreCase(correctname)) System.out.println("Authorized user!!");
else System.out.println("Unauthorized user!!");
}
}
I have a method that asks for the user's name, then returns it. Now I want to access the user's name in another method, but when I print the user's name it says "null". I don't understand, I defined the variable, but how can I give it access across the entire class?
public static String userName() {
Scanner input = new Scanner (System.in);
System.out.print("Hello! Welcome to The Game. Whats your name? ");
String userName = input.next();
return userName;
}
This is the method where I try to access the userName variable but am given "null"
public static void homeMethod() {
Scanner input = new Scanner (System.in);
System.out.println("Hello " + userName + "! What would you like to do? (Type LIST for options)");
String userGameChoice = input.nextLine();
}
Calling the userName() method inside the homeMethod() i am given the same error.
Any help is much appreciated. Thanks!
I needed to create userName variable outside of the method.
This is what I had:
public class MainGameClass {
public static String userName;
public static String userName() {
Scanner input = new Scanner (System.in);
System.out.print("Hello! Welcome to The Game. Whats your name? ");
String userName = input.next();
return userName;
}
public static void homeMethod() {
Scanner input = new Scanner (System.in);
System.out.println("What would you like to do " + userName + "? (Type LIST for options)");
String userGameChoice = input.nextLine();
}
}
Output when calling homeMethod():
What would you like to do null? (Type LIST for options)
Solution:
public class MainGameClass {
public static String userName;
public static String userName() {
Scanner input = new Scanner (System.in);
System.out.print("Hello! Welcome to The Game. Whats your name? ");
String userName = input.next();
return userName;
}
public static void homeMethod() {
Scanner input = new Scanner (System.in);
System.out.println("What would you like to do " + userName + "? (Type LIST for options)");
userGameChoice = input.nextLine();
}
}
Output when calling homeMethod():
What would you like to do (userName)? (Type LIST for options)
Explanation:
I correctly defined the userName variable in the class, however, in the userName() method, I created a new variable under the same name. Therefore not returning the answer to the userName variable.
Either declare userName variable outside the method, in the class itself or call userName method in your homeMethod like this:
public static void homeMethod() {
Scanner input = new Scanner (System.in);
System.out.println("Hello " + userName() + "! What would you like to do? (Type LIST for options)");
String userGameChoice = input.nextLine();
}
You have to put userName out of the method like this:
System.out.print("Hello! Welcome to The Game. Whats your name? ");
String userName = input.next()
public static String userName() {
Scanner input = new Scanner (System.in);
return userName;
}
I'm doing a small project and I have everything done, just one small error. the error shows "symbol not found" and shows the red squiggly line under my scan.
package pkgif.elsestatements.java;
import java.util.Scanner;
public class IfElseStatementsJava {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String your_name;
System.out.print("What is your name?");
your_name = user_input.next();
System.out.println("Hi " + your_name);
String user_input2;
System.out.print(".");
user_input2 = user_input.next();
System.out.println("Do you like Gospel Music Paul?"); //Asks question
String input = scan.nextLine(); //Waits for input
if (input.equalsIgnoreCase("Yes")) { //If the input is Yes)
System.out.println("Here are some songs; Amazing Grace, I'll Fly Away, A Little Talk With Jesus ");
}
else { //If the input is anything else
System.out.println("Ok! Have a nice day!");
}
}
this line is the one giving me trouble ---- String input = scan.nextLine(); //Waits for input
I was feeling really great about finish this with no errors beforehand, then this. Any help is appreciated.
According to the code above. You've defined Scanner user_input = new Scanner(System.in); i.e. user_input as the oject ref.
So, changing String input = scan.nextLine(); to String input = user_input.nextLine(); should do.
I have declared two strings and reading the input using Scanner(System.in).
After this when i am closing the Scanner and again reading the another input using the Scanner,then it throws an error: NoSuchElementException.
Please guide me on this
import java.util.Scanner;
import java.io.*;
public class NumericInput
{
public static void main(String[] args)
{
// Declarations
Scanner in = new Scanner(System.in);
String string1;
String string2;
// Prompts
System.out.println("Enter the value of the First String .");
// Read in values
string1 = in.nextLine();
// When i am commenting below line(in.close) code is working properly.
in.close();
Scanner sc = new Scanner(System.in);
System.out.println("Now enter another value.");
string2 = sc.next();
sc.close();
System.out.println("Here is what you entered: ");
System.out.println(string1 + " and " + string2);
}
}
When you close your scanner it also closes System.in input stream, you are using it again, but it's closed, so when you try to use Scanner again, no open System.in stream is found.
There is no need to close a Scanner, since it implements AutoCloseable interface you should declare resources in try-with-resources as of java 7. If closing Scanner is an issue.
try(Scanner in = new Scanner(System.in); Scanner sc = new Scanner(System.in)){
// do stuff here without closing
}
catch(Exception){
e.printStackTrace();
}