This question already has answers here:
How do I compare strings in Java?
(23 answers)
java.util.NoSuchElementException - Scanner reading user input
(5 answers)
Closed 5 years ago.
I'm pretty new to coding, and I understand how to do a do while for a yes or no answer in C++. But java is giving me a hard time. This code doesn't give me any errors until I run it. When it asks for the yes or no answer it will say:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at midterm.main(midterm.java:40)
Line 40 is the:
ans = s.next();
Maybe I'm doing something wrong with String, I've tried char but had no luck either. Here is my code:
public class midterm
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String ans;
do
{
System.out.println("Welcome to the Menu!\nA)Guessing Game\nB)Calculator\n"
+ "\nEnter Letter: ");
String letter = s.next();
s.nextLine();
if ((letter.equals("A")) || (letter.equals("a")))
{
guessingGame();
}
else
{
calcDecide();
}
System.out.println("\nWould you like to go back to the menu or exit?\n"
+ "Press 'Y' yes or 'E' for exit: ");
ans = s.next();
s.nextLine();
}while (ans == "y" || ans == "y");
s.close();
}
Not sure why this was marked as a duplicate. I feel that people don't read the entire thing and like to assume this. This WAS NOT a simple String mess-up(That's part of the mistake,) but there's a bigger issue here, which I believe to be the scanner
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
String command = "";
Scanner sc = new Scanner(System.in);
while (command!="exit")
{
System.out.println("Please enter command: ");
command = sc.nextLine();
System.out.println("You enter: "+command);
if (command == "exit")
{
System.out.println("Exit program.");
}
}
sc.close();
Haven't written java in a year and I forgot how scanner work. The code would never enter the if part when I enter exit. I tried next() and nextLine().
Because operator == compares the address memory not the value for String variable. You need to use equals() method.
command.equals("exit")
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
I am a beginner in Java, I have written a simple input java program. I am giving the user the option to repeat the program with a do while, however it's not properly functioning. Can someone point my mistake please?
public static void main(String args[]){
char repeat = 0;
Scanner input = new Scanner(System.in);
do{
String word = null;
boolean oneWord = false;
while(!oneWord){
System.out.println("Please enter a word: ");
try{
word = input.nextLine().toLowerCase();
word= word.trim();
int words = word.isEmpty() ? 0 : word.split("\\s+").length;
if(words==1 && word.length()>1 && word.length()<100){
System.out.println("Success");
oneWord = true;
System.out.println("Continue(Y/N)");
repeat = input.next().charAt(0);
}else{
System.out.println("Failure.");
}
} catch (Exception e) {
System.out.println("Exception occurred");
}
}
}while(repeat=='Y'|| repeat=='y');
input.close();
}
I would suggest you using nextLine() function of Scanner class instead of next() function.
See the difference here
Even tho its a duplicate, have a look at the line
repeat = input.next().charAt(0);
and change it to
repeat = input.nextLine().charAt(0);
This will solve your problem. For further information regarding the problem, read the duplicate link.
This question already has answers here:
Odd error in my Java program
(2 answers)
Closed 6 years ago.
I made a calculator and now improving it so that it reads one line of code and turns it into three variables a: first number, b: second number, and function: what it does example: 10 * 10. This is my code:
System.out.println("problem: ");
problem = user_input.next();
StringTokenizer token = new StringTokenizer(problem," ");
a = Integer.parseInt(token.nextToken());
String Menu = (token.nextToken());
b = Integer.parseInt(token.nextToken());
It doesn't understand nextToken at all, it says at Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at mycalc.main(mycalc.java:20),
also I asked a friend that actually showed me how this works and he was confused too. Please help me any possible way you can!
When you do
problem = user_input.next();
this read just one word. e.g. if you input 10 * 10, then this word will be 10
So when you do
String Menu = (token.nextToken());
there is no such element as the error suggests.
Note: you are parsing for words two different ways. It would be simpler to just use the scanner.
System.out.println("problem: ");
a = user_input.nextInt();
String menu = user_input.next();
b = user_input.nextInt();
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
our project basically involves reading files about the premier league and returning 1. outcomes of games played, 2. fixtures yet to be played and 3. the leaderboard(table). I'm displaying a menu in the command line and also taking in the user input from there. I'm using a while loop to match the userinput with what output to display but I wanted to say if they don't enter 1,2 or 3 that there's an error and to display the menu again and ask them to enter another input. However now if I do enter 1,2 or 3, it doesn't display my results I can't get out of the while loop. Can anyone help me? This is my code!
while(userInput == false)
{
Scanner input = new Scanner(System.in);
String pattern = "[1-3]{1}";
String userChoice;
System.out.println(menuOptions);
userChoice = input.nextLine();
if(userChoice == "1")
{
System.out.print(outcomesResults);
userInput = true;
}
if(userChoice == "2")
{
System.out.print(fixturesResults);
userInput = true;
}
if(userChoice == "3")
{
System.out.print(leaderboardResults);
userInput = true;
}
else if(!userChoice.matches(pattern))
{
System.out.println();
System.out.println("Error: unidentified selection. Please choose 1,2 or 3");
userInput = false;
}
}
If you want to compare the String value, you should use equals():
if (userChoice.equals("1")) {
// do something
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I'm working on a small program that asks for your name using a Scanner. If you enter blankstring, then I would like the console to display a message.
Here's what I tried doing:
import java.util.Scanner;
public class Adventure
{
public static void main(String[] args)
{
Scanner myScan = new Scanner (System.in);
System.out.println("What's your name?");
String name = myScan.nextLine();
while (!(name == "")) //Always returns false.
{
System.out.println("That's not your name. Please try again.");
name = myScan.nextLine();
}
System.out.println("It's a pleasure to meet you, " + name + ".");
}
}
The code never enters the while loop. Why?
Change your condition to:
while(!name.equals("")) {
or as suggested below by m0skit0:
while(!name.isEmpty()) {
See also
why equals() method when we have == operator?