Java Do While loop?
0
Hey my fellow Fakku users! I have started to learn java programming language a while ago, and got small problem with my code. If any of you have break between reading can you look and tell me where i went wrong?
Here is screenshot of my code, and if you prefer i have pasted the code below.
http://puu.sh/4Xx0k
And thanks for your time!
import java.util.Scanner;
public class StringCW {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner mainIn = new Scanner(System.in);
boolean nextLoop = false;
String menu;
System.out.println("Please use number input to choose from menu");
do {
if(nextLoop) {
System.out.println("Wrong input, try again");
menu = ""; //clearing menu, just in case (trying to fix loop error)
System.out.println("Clearing menu, menu contains:" + menu); //checking if menu is cleared
}
System.out.println("Table Menu - Commands Avaliable:"); //Prints table menu
System.out.println("1 - readWords()");
System.out.println("2 - splitString()");
menu = mainIn.next(); //Takes string input
System.out.println("Input acquired. Menu contains:" + menu); //Checking if input was acquired from scanner (fix loop error)
nextLoop = true;
if(menu == "1") { //Should be "If input is 1, then call command readWords.. does not work.
readWords();
}
else {
System.out.println("Input was 1 and it still goes here. Why?");
}
} while (menu != "1" && menu != "2"); //Does not work either. Why?
Here is screenshot of my code, and if you prefer i have pasted the code below.
http://puu.sh/4Xx0k
And thanks for your time!
import java.util.Scanner;
public class StringCW {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner mainIn = new Scanner(System.in);
boolean nextLoop = false;
String menu;
System.out.println("Please use number input to choose from menu");
do {
if(nextLoop) {
System.out.println("Wrong input, try again");
menu = ""; //clearing menu, just in case (trying to fix loop error)
System.out.println("Clearing menu, menu contains:" + menu); //checking if menu is cleared
}
System.out.println("Table Menu - Commands Avaliable:"); //Prints table menu
System.out.println("1 - readWords()");
System.out.println("2 - splitString()");
menu = mainIn.next(); //Takes string input
System.out.println("Input acquired. Menu contains:" + menu); //Checking if input was acquired from scanner (fix loop error)
nextLoop = true;
if(menu == "1") { //Should be "If input is 1, then call command readWords.. does not work.
readWords();
}
else {
System.out.println("Input was 1 and it still goes here. Why?");
}
} while (menu != "1" && menu != "2"); //Does not work either. Why?
1
Basically, the == != comparators for Strings and other non primitive types don't work the way you think it will. When you do menu == "1", you're telling java to compare the references to the strings, not the strings' values themselves. To compare non primitive types, you need to use the .equals() method
http://stackoverflow.com/questions/767372/java-string-equals-versus
Also, if your input is a single character, you can consider using a switch case instead of multiple if statements
switch(menu){
case '1': readWords(); break;
case '2': splitString(); break;
default:
System.out.println("neither 1 or 2 was selected"); break;
}
Note that your input reads the variable menu in as a string however, the switch case I wrote would throw up an error since the cases are of type char and not String. This is easily fixed by having a variable store menu converted from string to char used for the case. I think you can also just change the cases to "1" and "2" though to compare them as type string but either should work.
http://stackoverflow.com/questions/767372/java-string-equals-versus
Also, if your input is a single character, you can consider using a switch case instead of multiple if statements
switch(menu){
case '1': readWords(); break;
case '2': splitString(); break;
default:
System.out.println("neither 1 or 2 was selected"); break;
}
Note that your input reads the variable menu in as a string however, the switch case I wrote would throw up an error since the cases are of type char and not String. This is easily fixed by having a variable store menu converted from string to char used for the case. I think you can also just change the cases to "1" and "2" though to compare them as type string but either should work.
0
Rovencrone wrote...
Basically, the == != comparators for Strings and other non primitive types don't work the way you think it will. When you do menu == "1", you're telling java to compare the references to the strings, not the strings' values themselves. To compare non primitive types, you need to use the .equals() methodhttp://stackoverflow.com/questions/767372/java-string-equals-versus
Thanks a lot! This helps me, now it works.
Thanks for your effort! I hope you will have great day:)
0
Turaos wrote...
Rovencrone wrote...
Basically, the == != comparators for Strings and other non primitive types don't work the way you think it will. When you do menu == "1", you're telling java to compare the references to the strings, not the strings' values themselves. To compare non primitive types, you need to use the .equals() methodhttp://stackoverflow.com/questions/767372/java-string-equals-versus
Thanks a lot! This helps me, now it works.
Thanks for your effort! I hope you will have great day:)
Since it looks like you're doing your work in eclipse, when calling a method of some variable or type of object after . hit ctrl+space and it'll bring up a neat list of available methods of that object's class, also including javadocs entries for what each method does.