I want to ask the user whether he wants to create a file named file.elt or not. I'm trying to do this with a switch statement using Scanner class.
Here is my code:
System.out.println("Do you want to create the file.elt? (Y/N)");
strOption=sc.nextLine();
OUTER:
while (sc.hasNext()) {
switch (strOption) {
case "Y":
case "y":
elements.createElements(file);
break OUTER;
case "N":
case "n":
System.out.println("There will be no file.elt created! .");
break OUTER;
default:
System.out.println("Please, type Y or N.");
strOption=sc.nextLine();
break;
}
}
sc.close();
The sc object is declared at the beginning of the program, where I ask for the name of the file.
The sc declaration is:
String file;
Scanner sc = new Scanner(System.in);
System.out.println("Type the name of the file .dat .");
file=sc.nextLine();
The problem is that the while loop is infinite and I don't know why.
You are not updating strOption.
You should move your strOption=sc.nextLine(); inside your while loop
Also, as TheLostMind pointed out, replace hasNext with hasNextLine.
Edit
You might consider to switch to Console. Also, you may create confirm utility method since it's fairly common task:
private Console console;
...
console = System.console();
...
if (confirm("Do you want to create the file.elt? (Y/N)")) {
elements.createElements(file);
} else {
System.out.println("There will be no file.elt created! .");
}
...
private boolean confirm(String message) {
String answer = console.readLine(message);
while (!answer.matches("[YyNn]")) {
answer = console.readLine("Please, type Y or N.");
}
return "Y".equalsIgnoreCase(answer);
}
Note: This doesn't work in eclipse.
Scanner is state based, and a bit difficult. I would not use it for non-token things.
//strOption=sc.nextLine();
OUTER:
while (sc.hasNextLine()) {
strOption=sc.nextLine();
...
default:
System.out.println("Please, type Y or N.");
//strOption=sc.nextLine();
2 options:
Because sc.hasNext() is always true . you need to call sc.nextLine to advances this scanner past the current line
sc.hasNext() is blocking (as describe in the documents )
if you cn tell if it's realy an infinite loop or blocking call - you will know how to solve it (simply add trace at the beginning of the loop , run the program , and check the output console )
First, don't use labels such as OUTER unless you know what you are doing. In this case it is not needed.
sc.hasNext() returns true (otherwise you wouldn't even enter the loop) and in the loop you don't do anything to change that state (you don't 'consume' the inputstream).
Before you even enter the loop, you read the first line, after which there is apparently more input to be read but you never read that input so sc.hasNext() keeps returning true and the while loop never finishes.
Your break OUTER; breaks to the loop defined in OUTER:, which means it breaks TO the while loop, not OUT of the while loop. Typically people use this construct to break out of an inner loop to an outer loop but as I said before, you're better of not using this construct.
EDIT: I confused labeled breaks with labeled continues. Basically, the break here works as intended but the label is superfluous (and I still advise against using labels).
The problem then is that the first line of input you read probably didn't equals a "y", "Y", "n" or "N" for some reason and since you don't consume the input, sc.hasNext() and strOption still contains the same string which doesn't equal any of your case statements, meaning the loop will go on infinitely.
Either use a plain break; or fix your loop so it consumes the input.
For example:
System.out.println("Do you want to create the file.elt? (Y/N)");
while (sc.hasNext())
{
String inputString = strOption=sc.nextLine();
// handle inputString
}
sc.close();
Related
Scanner kb = new Scanner(System.in);
String plt;
String msr;
double wgh;
System.out.println("'Welcome to interplanetary weight calculator.");
Thread.sleep(2500);
System.out.println("");
System.out.println("Please choose one planet for calculation:");
System.out.println("1.Venus 2.Mars 3.Jupiter");
System.out.println("4.Saturn 5.Uranus 6.Neptune");
System.out.print(">");
do {
while (!kb.hasNextLine()) {
System.out.println("That's not a planet!");
kb.nextLine(); // this is important!
}
plt = kb.nextLine();
} while (plt.equalsIgnoreCase("Venus"));
System.out.println("Thank you!Now will do calculation on " + plt); }}
I want to make a weight calculator between planets and I also want a spell checker too but when I write something, it just prints out "Thank you! Now, will do the calculation on ....".
It prints out integers too. I can't find where did I do wrong.
Essentially, what happens is that your loop will run once, you can enter something and it will then exit the loop because anything except "venus" (in any combination of upper and lower case) will exit the loop. This is why it will also print numbers, etc.
The solution to this might be to add an ! to the beginning of the condition. This will invert the condition, meaning now only entering "venus" will cause the loop to exit which will result in the program continuing to run everything below the loop. However, there's still not going to be any kind of error message. Spell-checking and error handling would be achieved differently.
I ran into an issue. Below is my code, which asks user for input and prints out what the user inputs one word at a time.
The problem is that the program never ends, and from my limited understanding, it seem to get stuck inside the while loop. Could anyone help me a little?
import java.util.Scanner;
public class Test{
public static void main(String args[]){
System.out.print("Enter your sentence: ");
Scanner sc = new Scanner (System.in);
while (sc.hasNext() == true ) {
String s1 = sc.next();
System.out.println(s1);
}
System.out.println("The loop has been ended"); // This somehow never get printed.
}
}
You keep on getting new a new string and continue the loop if it's not empty. Simply insert a control in the loop for an exit string.
while(!s1.equals("exit") && sc.hasNext()) {
// operate
}
If you want to declare the string inside the loop and not to do the operations in the loop body if the string is "exit":
while(sc.hasNext()) {
String s1 = sc.next();
if(s1.equals("exit")) {
break;
}
//operate
}
The Scanner will continue to read until it finds an "end of file" condition.
As you're reading from stdin, that'll either be when you send an EOF character (usually ^d on Unix), or at the end of the file if you use < style redirection.
When you use scanner, as mentioned by Alnitak, you only get 'false' for hasNext() when you have a EOF character, basically... You cannot easily send and EOF character using the keyboard, therefore in situations like this, it's common to have a special character or word which you can send to stop execution, for example:
String s1 = sc.next();
if (s1.equals("exit")) {
break;
}
Break will get you out of the loop.
Your condition is right (though you should drop the == true). What is happening is that the scanner will keep going until it reaches the end of the input. Try Ctrl+D, or pipe the input from a file (java myclass < input.txt).
it doesn't work because you have not programmed a fail-safe into the code. java sees that the scanner can still collect input while there is input to be collected and if possible, while that is true, it keeps doing so. having a scanner test to see if a certain word, like EXIT for example, is fine, but you could also have it loop a certain number of times, like ten or so. but the most efficient approach is to ask the user of your program how many strings they wish to enter, and while the number of strings they enter is less than the number they put in, the program shall execute. an added option could be if they type EXIT, when they see they need less spaces than they put in and don't want to fill the next cells up with nothing but whitespace. and you could have the program ask if they want to enter more input, in case they realize they need to enter more data into the computer.
the program would be quite simplistic to make, as well because there are a plethera of ways you could do it. feel free to ask me for these ways, i'm running out of room though. XD
If you don't want to use an EOF character for this, you can use StringTokenizer :
import java.util.*;
public class Test{
public static void main(){
Scanner sc = new Scanner (System.in);
System.out.print("Enter your sentence: ");
String s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s," ");//" " is the delimiter here.
while (st.hasMoreTokens() ) {
String s1 = st.nextToken();
System.out.println(s1);
}
System.out.println("The loop has been ended");
}
}
I had the same problem and I solved it by reading the full line from the console with one scanner object, and then parsing the resulting string using a second scanner object.
Scanner console = new Scanner(System.in);
System.out.println("Enter input here:");
String inputLine = console.nextLine();
Scanner input = new Scanner(inputLine);
List<String> arg = new ArrayList<>();
while (input.hasNext()) {
arg.add(input.next().toLowerCase());
}
You can simply use one of the system dependent end-of-file indicators ( d for Unix/Linux/Ubuntu, z for windows) to make the while statement false. This should get you out of the loop nicely. :)
Modify the while loop as below. Declare s1 as String s1; one time outside the loop. To end the loop, simply use ctrl+z.
while (sc.hasNext())
{
s1 = sc.next();
System.out.println(s1);
System.out.print("Enter your sentence: ");
}
If I run this code
Scanner sc = new Scanner();
while (true) {
if (sc.next().equals("1"))
System.out.println("--1--");
else if (sc.next().equals("2"))
System.out.println("--2--");
else if (sc.next().equals("3"))
System.out.println("--3--");
else if (sc.next().equals("4"))
System.out.println("--4--");
else if (sc.next().equals("help"))
System.out.println("--help--");
}
It will not read the first time I type enter. I have to type 2-4 times before it reads the input. A session could look like this:
1
1
1
1
--1--
3
3
--3--
help
2
1
help
--help--
No matter what I type, it will only read the last input of the four inputs.
Sometimes it reads after two inputs. I'm really confused about this.
Should I instead use multiple scanners?
Your concepts are wrong here.
Each time you ask for sc.next() it will wait for the input. If that input is equal to what you want it to be, then the code is executed.
You can correct this by storing sc.next() in a String variable, and then comparing it.
Here:
if (sc.next().equals("1"))
it asks for an input.
If that input is 1 then the code is executed and --1-- is printed out. Else, it jumps to this: if (sc.next().equals("2")). Now if the input is 2 then the code to print --2-- is executed. Else, it jumps to if (sc.next().equals("3")) and so on.
You can correct this by:
storing sc.next() in a String variable, and then comparing it.
using a switch-case block to compare the input.
You're calling sc.next() multiple times - so if the input isn't 1, it's going to wait for more input to see whether the next input is 2, etc. Each call to sc.next() will wait for more input. It doesn't have any idea of "that isn't the input you were looking for, so I'll return the same value next time you call".
Use a local variable to store the result of sc.next()
while (true) {
String next = sc.next();
if (next.equals("1"))
System.out.println("--1--");
else if (next.equals("2"))
System.out.println("--2--");
else if (next.equals("3"))
System.out.println("--3--");
else if (next.equals("4"))
System.out.println("--4--");
else if (next.equals("help"))
System.out.println("--help--");
}
Also consider using a switch statement instead...
You are calling sc.next() multiple times
Solution code :
Scanner scanner = new Scanner(System.in);
while(true){
switch (scanner.next()) {
case "1":
System.out.println("--1--");
break;
case "2":
System.out.println("--2--");
break;
case "3":
System.out.println("--3--");
break;
case "4":
System.out.println("--4--");
break;
case "help":
System.out.println("--help--");
break;
default:
break;
}
}
Task: To check that if a user input string has the same first and last character. If yes or no, output to the screen, if the user enters "done", the loop is exited.
Issue: While loop executes when condition is false
What I've tried: Using different types of loops, doing a loop within the loop to revalidate the code and all together giving up!
import java.util.*;
public class lab_15 {
public static void main(String args[]) {
String userInput = "";
String done = "done";
while (!userInput.equalsIgnoreCase(done))
{
int length;
Scanner sc = new Scanner(System.in);
userInput = sc.next();
length = (int)userInput.length();
if (userInput.charAt(0) == userInput.charAt(userInput.length()-1)) {
System.out.println("The first character equals the second character.");
}
else {
System.out.println("The first and second characters are different.");
}
}
// EXIT LOOP
System.out.println("Thank you for using this software!");
}
}
Inputs
+ bradley
+ hannah
+ done
I am still new to the site and have referred to the t's & c's regarding posts. Please do not negative if you find the question to not be challenging. I am new to programming and hope to progress.
Thank you!!!
This is because you change your userInput immediately once entering the loop. The condition is only checked when you reach the top of the loop, so if you invalidate the condition halfway through, it will continue executing until you reach the top.
The solution is to refactor so that the very last thing that happens is changing your userInput so that the condition is check immediately after the value is changed. (I would also pull the scanner instantiation out of the loop.)
Alternatively you could check your condition inside of the while loop and call break if the userInput has changed to match the terminating condition. The break keyword will force the logic to exit the loop immediately, without evaluating the condition again.
I ran into an issue. Below is my code, which asks user for input and prints out what the user inputs one word at a time.
The problem is that the program never ends, and from my limited understanding, it seem to get stuck inside the while loop. Could anyone help me a little?
import java.util.Scanner;
public class Test{
public static void main(String args[]){
System.out.print("Enter your sentence: ");
Scanner sc = new Scanner (System.in);
while (sc.hasNext() == true ) {
String s1 = sc.next();
System.out.println(s1);
}
System.out.println("The loop has been ended"); // This somehow never get printed.
}
}
You keep on getting new a new string and continue the loop if it's not empty. Simply insert a control in the loop for an exit string.
while(!s1.equals("exit") && sc.hasNext()) {
// operate
}
If you want to declare the string inside the loop and not to do the operations in the loop body if the string is "exit":
while(sc.hasNext()) {
String s1 = sc.next();
if(s1.equals("exit")) {
break;
}
//operate
}
The Scanner will continue to read until it finds an "end of file" condition.
As you're reading from stdin, that'll either be when you send an EOF character (usually ^d on Unix), or at the end of the file if you use < style redirection.
When you use scanner, as mentioned by Alnitak, you only get 'false' for hasNext() when you have a EOF character, basically... You cannot easily send and EOF character using the keyboard, therefore in situations like this, it's common to have a special character or word which you can send to stop execution, for example:
String s1 = sc.next();
if (s1.equals("exit")) {
break;
}
Break will get you out of the loop.
Your condition is right (though you should drop the == true). What is happening is that the scanner will keep going until it reaches the end of the input. Try Ctrl+D, or pipe the input from a file (java myclass < input.txt).
it doesn't work because you have not programmed a fail-safe into the code. java sees that the scanner can still collect input while there is input to be collected and if possible, while that is true, it keeps doing so. having a scanner test to see if a certain word, like EXIT for example, is fine, but you could also have it loop a certain number of times, like ten or so. but the most efficient approach is to ask the user of your program how many strings they wish to enter, and while the number of strings they enter is less than the number they put in, the program shall execute. an added option could be if they type EXIT, when they see they need less spaces than they put in and don't want to fill the next cells up with nothing but whitespace. and you could have the program ask if they want to enter more input, in case they realize they need to enter more data into the computer.
the program would be quite simplistic to make, as well because there are a plethera of ways you could do it. feel free to ask me for these ways, i'm running out of room though. XD
If you don't want to use an EOF character for this, you can use StringTokenizer :
import java.util.*;
public class Test{
public static void main(){
Scanner sc = new Scanner (System.in);
System.out.print("Enter your sentence: ");
String s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s," ");//" " is the delimiter here.
while (st.hasMoreTokens() ) {
String s1 = st.nextToken();
System.out.println(s1);
}
System.out.println("The loop has been ended");
}
}
I had the same problem and I solved it by reading the full line from the console with one scanner object, and then parsing the resulting string using a second scanner object.
Scanner console = new Scanner(System.in);
System.out.println("Enter input here:");
String inputLine = console.nextLine();
Scanner input = new Scanner(inputLine);
List<String> arg = new ArrayList<>();
while (input.hasNext()) {
arg.add(input.next().toLowerCase());
}
You can simply use one of the system dependent end-of-file indicators ( d for Unix/Linux/Ubuntu, z for windows) to make the while statement false. This should get you out of the loop nicely. :)
Modify the while loop as below. Declare s1 as String s1; one time outside the loop. To end the loop, simply use ctrl+z.
while (sc.hasNext())
{
s1 = sc.next();
System.out.println(s1);
System.out.print("Enter your sentence: ");
}