Results 1 to 13 of 13
- 01-21-2012, 01:47 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Program skips "If" code and goes straight to "Else"
My program runs correctly except for the fact that it seems to skip the "If" portion of the code and go directly to "Else" even when I hard code the user response in to be the correct response for the "If" portion.
Java Code://Imports package to enable scanner to read user input import java.util.Scanner; //'TempConverter' Class public class TempConverter { //'main' method public static void main(String[] args) { //Calls the 'convert' class so the program can gather input, perform calculations, and output data. convert(); //Calls 'exit' class to prompt user to exit or restart. exit(); } public static void convert() { //Creates variable for user input. User input should be 'C' or 'F'. String TempScale; //Creates variable for user inputted temperature. This is the number that will be converted. String RawTemp; //Variable to hold RawTemp conversion from String to Double so the calculation may be performed Double Temp; //Variable for converted temperature to be output to screen Double CTemp; //Output to prompt user for input System.out.println("Please input 'C' to convert 'Fahrenheit to Celsius' or input 'F' to convert 'Celsius to Fahrenheit'."); //Scanner to read user input Scanner ScaleInput = new Scanner(System.in); //Assigns 'TempScale' to user input TempScale = ScaleInput.nextLine(); //Verifies user input valid answer if(TempScale != "F" || TempScale != "f" || TempScale != "C" || TempScale != "c"){ System.out.println("Please input a valid answer. Valid answers are F, f, C, c"); convert(); //Runs conversion } else { if(TempScale == "F" || TempScale == "f"){ //Confirms user input System.out.println("You have select to convert from Celsius to Fahrenheit."); //Asks user for temperature System.out.println("Please input the Celsious temperature you want converted."); //Reads input Scanner TempInput = new Scanner(System.in); //Assigns input to 'RawTemp' RawTemp = TempInput.nextLine(); //Converts String 'RawTemp' to Double 'Temp' Temp = Double.parseDouble(RawTemp); //Converts to Fahrenheit CTemp = (Temp*1.8) + 32; //Outputs temperature in both measurements System.out.println("Celcius: " + Temp); System.out.println("Fahrenheit: " + CTemp); } else { //Confirms user input System.out.println("You have select to convert from Fahrenheit to Celsius."); //Asks user for temperature System.out.println("Please input the Fahrenheit temperature you want converted."); //Reads input Scanner TempInput = new Scanner(System.in); //Assigns input to 'RawTemp' RawTemp = TempInput.nextLine(); //Converts String 'RawTemp' to Double 'Temp' Temp = Double.parseDouble(RawTemp); //Converts to Fahrenheit CTemp = (Temp-32)/1.8; //Outputs temperature in both measurements System.out.println("Fahrenheit: " + Temp); System.out.println("Celsius: " + CTemp); } } } public static void exit() { //Variable prompting user to exit String ExitReply; System.out.println("If you would like to exit the program please type 'exit', otherwise type 'retry' to restart the program."); //Scanner to read user input Scanner ExitInput = new Scanner(System.in); //Assigns 'ExitReply' to user input ExitReply = ExitInput.nextLine(); //Test output System.out.println(ExitReply + " ECHO"); //Verifies user put in valid answer if(ExitReply == "exit" || ExitReply == "Exit" || ExitReply == "Retry" || ExitReply == "retry"){ //If-else statement to Retry or Exit program if(ExitReply == "Retry" || ExitReply == "retry"){ //Test output System.out.println("test"); //Restarts program convert(); exit(); } else { //Test output System.out.println("test2"); //Closes program System.exit(0); } } else{ //Prompts user to put in valid response System.out.println("Please input either 'exit' or 'retry'."); exit(); } } }
- 01-21-2012, 01:55 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Program skips "If" code and goes straight to "Else"
strings are objects-> compare objects (content) with the equals method and not with ==!
if("a".equals("a")).....
Java: ==, .equals(), compareTo(), and compare()
- 01-21-2012, 05:08 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Re: Program skips "If" code and goes straight to "Else"
Thank you. I have modified my code.
Did I do the "not equals" code correctly in the first If statement?
The reason I ask is because when I put in a valid answer (C,c, F, f) It displays my "Please input a valid answer. Valid answers are 'F', 'f', 'C', 'c'" message.
Java Code://Imports package to enable scanner to read user input import java.util.Scanner; //'TempConverter' Class public class TempConverter { //'main' method public static void main(String[] args) { //Calls the 'convert' class so the program can gather input, perform calculations, and output data. convert(); //Calls 'exit' class to prompt user to exit or restart. exit(); } public static void convert() { //Creates variable for user input. User input should be 'C' or 'F'. String TempScale; //Creates variable for user inputted temperature. This is the number that will be converted. String RawTemp; //Variable to hold RawTemp conversion from String to Double so the calculation may be performed Double Temp; //Variable for converted temperature to be output to screen Double CTemp; //Output to prompt user for input System.out.println("Please input 'C' to convert 'Fahrenheit to Celsius' or input 'F' to convert 'Celsius to Fahrenheit'."); //Scanner to read user input Scanner ScaleInput = new Scanner(System.in); //Assigns 'TempScale' to user input TempScale = ScaleInput.nextLine(); //Verifies user input valid answer if(TempScale != "F" || TempScale != "f" || TempScale != "C" || TempScale != "c"){ System.out.println("Please input a valid answer. Valid answers are F, f, C, c"); convert(); //Runs conversion } else { if(TempScale == "F" || TempScale == "f"){ //Confirms user input System.out.println("You have select to convert from Celsius to Fahrenheit."); //Asks user for temperature System.out.println("Please input the Celsious temperature you want converted."); //Reads input Scanner TempInput = new Scanner(System.in); //Assigns input to 'RawTemp' RawTemp = TempInput.nextLine(); //Converts String 'RawTemp' to Double 'Temp' Temp = Double.parseDouble(RawTemp); //Converts to Fahrenheit CTemp = (Temp*1.8) + 32; //Outputs temperature in both measurements System.out.println("Celcius: " + Temp); System.out.println("Fahrenheit: " + CTemp); } else { //Confirms user input System.out.println("You have select to convert from Fahrenheit to Celsius."); //Asks user for temperature System.out.println("Please input the Fahrenheit temperature you want converted."); //Reads input Scanner TempInput = new Scanner(System.in); //Assigns input to 'RawTemp' RawTemp = TempInput.nextLine(); //Converts String 'RawTemp' to Double 'Temp' Temp = Double.parseDouble(RawTemp); //Converts to Fahrenheit CTemp = (Temp-32)/1.8; //Outputs temperature in both measurements System.out.println("Fahrenheit: " + Temp); System.out.println("Celsius: " + CTemp); } } } public static void exit() { //Variable prompting user to exit String ExitReply; System.out.println("If you would like to exit the program please type 'exit', otherwise type 'retry' to restart the program."); //Scanner to read user input Scanner ExitInput = new Scanner(System.in); //Assigns 'ExitReply' to user input ExitReply = ExitInput.nextLine(); //Test output System.out.println(ExitReply + " ECHO"); //Verifies user put in valid answer if(ExitReply == "exit" || ExitReply == "Exit" || ExitReply == "Retry" || ExitReply == "retry"){ //If-else statement to Retry or Exit program if(ExitReply == "Retry" || ExitReply == "retry"){ //Test output System.out.println("test"); //Restarts program convert(); exit(); } else { //Test output System.out.println("test2"); //Closes program System.exit(0); } } else{ //Prompts user to put in valid response System.out.println("Please input either 'exit' or 'retry'."); exit(); } } }
- 01-21-2012, 08:03 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Re: Program skips "If" code and goes straight to "Else"
You're still trying to compare two Strings for (in)equality by using the == or != operator; don't do that, use the equals( ... ) method instead and read the API documentation for the String class.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-21-2012, 03:38 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Re: Program skips "If" code and goes straight to "Else"
Sorry, I thought I pasted the updated code but I must have still had the old code copied.
This is what i meant to post.
Java Code://Imports package to enable scanner to read user input import java.util.Scanner; //'TempConverter' Class public class TempConverter { //'main' method public static void main(String[] args) { //Calls the 'convert' class so the program can gather input, perform calculations, and output data. convert(); //Calls 'exit' class to prompt user to exit or restart. exit(); } public static void convert() { //Creates variable for user input. User input should be 'C' or 'F'. String TempScale; //Creates variable for user inputted temperature. This is the number that will be converted. String RawTemp; //Variable to hold RawTemp conversion from String to Double so the calculation may be performed Double Temp; //Variable for converted temperature to be output to screen Double CTemp; //Output to prompt user for input System.out.println("Please input 'C' to convert 'Fahrenheit to Celsius' or input 'F' to convert 'Celsius to Fahrenheit'."); //Scanner to read user input Scanner ScaleInput = new Scanner(System.in); //Assigns 'TempScale' to user input TempScale = ScaleInput.nextLine(); //Verifies user input valid answer if(!TempScale.equals("F") || !TempScale.equals("f")|| !TempScale.equals("C") || !TempScale.equals("c")){ System.out.println("Please input a valid answer. Valid answers are 'F', 'f', 'C', 'c'"); convert(); //Runs conversion } else { if(TempScale.equals("F") || TempScale.equals("f")){ //Confirms user input System.out.println("You have select to convert from Celsius to Fahrenheit."); //Asks user for temperature System.out.println("Please input the Celsious temperature you want converted."); //Reads input Scanner TempInput = new Scanner(System.in); //Assigns input to 'RawTemp' RawTemp = TempInput.nextLine(); //Converts String 'RawTemp' to Double 'Temp' Temp = Double.parseDouble(RawTemp); //Converts to Fahrenheit CTemp = (Temp*1.8) + 32; //Outputs temperature in both measurements System.out.println("Celcius: " + Temp); System.out.println("Fahrenheit: " + CTemp); } else { //Confirms user input System.out.println("You have select to convert from Fahrenheit to Celsius."); //Asks user for temperature System.out.println("Please input the Fahrenheit temperature you want converted."); //Reads input Scanner TempInput = new Scanner(System.in); //Assigns input to 'RawTemp' RawTemp = TempInput.nextLine(); //Converts String 'RawTemp' to Double 'Temp' Temp = Double.parseDouble(RawTemp); //Converts to Fahrenheit CTemp = (Temp-32)/1.8; //Outputs temperature in both measurements System.out.println("Fahrenheit: " + Temp); System.out.println("Celsius: " + CTemp); } } } public static void exit() { //Variable prompting user to exit String ExitReply; System.out.println("If you would like to exit the program please type 'exit', otherwise type 'retry' to restart the program."); //Scanner to read user input Scanner ExitInput = new Scanner(System.in); //Assigns 'ExitReply' to user input ExitReply = ExitInput.nextLine(); //Verifies user put in valid answer if(ExitReply.equals("exit") || ExitReply.equals("Exit") || ExitReply.equals("Retry")|| ExitReply.equals("retry")){ //If-else statement to Retry or Exit program if(ExitReply.equals("Retry") || ExitReply.equals("retry")){ //Restarts program convert(); exit(); } else { //Test output System.out.println("test2"); //Closes program System.exit(0); } } else{ //Prompts user to put in valid response System.out.println("Please input either 'exit' or 'retry'."); exit(); } } }
-
Re: Program skips "If" code and goes straight to "Else"
Use a Venn diagram to check the logic of this:
You'll then see why this is always true and how to fix it.Java Code:if(!TempScale.equals("F") || !TempScale.equals("f")|| !TempScale.equals("C") || !TempScale.equals("c")){
Also, please reduce your number of comments to a reasonable amount. All your copious comments do is to clutter your code making it near unreadable.
-
Re: Program skips "If" code and goes straight to "Else"
Last edited by Fubarable; 01-21-2012 at 04:33 PM.
- 01-21-2012, 05:00 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Re: Program skips "If" code and goes straight to "Else"
Thanks for the help guys. I got that part working by changing it to:
Java Code:if(!TempScale.equals("F") && !TempScale.equals("f") && !TempScale.equals("C") && !TempScale.equals("c")){ System.out.println("Please input a valid answer. Valid answers are 'F', 'f', 'C', 'c'"); convert();
But I don't understand why it works. I thought with this way it's saying "If Tempscale is equal to ALL 4 answers (F,f,C,c) then execute this code"
Where as with my original code
I thought it's saying "If TempScale is equal to anyone of the 4 answers (F, f, C, c) then execute this code".Java Code:if(!TempScale.equals("F") || !TempScale.equals("f") || !TempScale.equals("C") || !TempScale.equals("c")){ System.out.println("Please input a valid answer. Valid answers are 'F', 'f', 'C', 'c'"); convert();
As for the comments, I know I went a bit overboard on this and a lot of it could be trimmed down but in this situation it is necessary. Sorry if it made it any harder to help me.
-
Re: Program skips "If" code and goes straight to "Else"
The key is that you're using the not operator "!". Again read up on what Venn diagrams mean and then look at my pictures -- the answer is there in simple graphical form.
Or think of it like this:
Can you think of any situation where this is not true?Java Code:if (foo != 1 || foo != 2) { // ... do something }
- If foo == 1, then it doesn't equal 2 and the statement is true.
- If foo == 2, then it doesn't equal 1 and the statement is true.
- If foo == something else, then it doesn't equal 1 and it doesn't equal 2 and the statement is true.
- Thus the statement is always true
Likewise for your code, the simple logic shown above applies:
Java Code:if(!TempScale.equals("F") || !TempScale.equals("f") || !TempScale.equals("C") || !TempScale.equals("c")){ System.out.println("Please input a valid answer. Valid answers are 'F', 'f', 'C', 'c'"); convert();- if TempScale equals "F", then it doesn't equal "f", "C", or "c", and the statement is true.
- if TempScale equals "f", then it doesn't equal "F", "C", or "c", and the statement is true.
- if TempScale equals "C", then it doesn't equal "F", "f", or "c", and the statement is true.
- if TempScale equals "c", then it doesn't equal "F", "f", or "C", and the statement is true.
- if TempScale equals anything else, then it doesn't equal "F", "f", "C", or "c", and the statement is true.
- Thus the statement is always true.
Make sense?Last edited by Fubarable; 01-21-2012 at 05:11 PM.
- 01-21-2012, 05:30 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Re: Program skips "If" code and goes straight to "Else"
Ahhhh I see. Makes sense now. Thanks!
-
Re: Program skips "If" code and goes straight to "Else"
You're welcome -- glad that you understand!
- 01-21-2012, 05:35 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Re: Program skips "If" code and goes straight to "Else"
Thanks again for your help.
I will post the updated code in case anyone uses this thread as a reference later on.
Java Code://Imports package to enable scanner to read user input import java.util.Scanner; //'TempConverter' Class public class TempConverter { //'main' method public static void main(String[] args) { //Calls the 'convert' class so the program can gather input, perform calculations, and output data. convert(); //Calls 'exit' class to prompt user to exit or restart. exit(); } public static void convert() { //Creates variable for user input. User input should be 'C' or 'F'. String TempScale; //Creates variable for user inputted temperature. This is the number that will be converted. String RawTemp; //Variable to hold RawTemp conversion from String to Double so the calculation may be performed Double Temp; //Variable for converted temperature to be output to screen Double CTemp; //Output to prompt user for input System.out.println("Please input 'C' to convert 'Fahrenheit to Celsius' or input 'F' to convert 'Celsius to Fahrenheit'."); //Scanner to read user input Scanner ScaleInput = new Scanner(System.in); //Assigns 'TempScale' to user input TempScale = ScaleInput.nextLine(); //Verifies user input valid answer if(!TempScale.equals("F") && !TempScale.equals("f") && !TempScale.equals("C") && !TempScale.equals("c")){ System.out.println("Please input a valid answer. Valid answers are 'F', 'f', 'C', 'c'"); convert(); //Runs conversion } else { if(TempScale.equals("F") || TempScale.equals("f")){ //Confirms user input System.out.println("You have select to convert from Celsius to Fahrenheit."); //Asks user for temperature System.out.println("Please input the Celsious temperature you want converted."); //Reads input Scanner TempInput = new Scanner(System.in); //Assigns input to 'RawTemp' RawTemp = TempInput.nextLine(); //Converts String 'RawTemp' to Double 'Temp' Temp = Double.parseDouble(RawTemp); //Converts to Fahrenheit CTemp = (Temp*1.8) + 32; //Outputs temperature in both measurements System.out.println("Celcius: " + Temp); System.out.println("Fahrenheit: " + CTemp); } else { //Confirms user input System.out.println("You have select to convert from Fahrenheit to Celsius."); //Asks user for temperature System.out.println("Please input the Fahrenheit temperature you want converted."); //Reads input Scanner TempInput = new Scanner(System.in); //Assigns input to 'RawTemp' RawTemp = TempInput.nextLine(); //Converts String 'RawTemp' to Double 'Temp' Temp = Double.parseDouble(RawTemp); //Converts to Fahrenheit CTemp = (Temp-32)/1.8; //Outputs temperature in both measurements System.out.println("Fahrenheit: " + Temp); System.out.println("Celsius: " + CTemp); } } } public static void exit() { //Variable prompting user to exit String ExitReply; System.out.println("If you would like to exit the program please type 'exit', otherwise type 'retry' to restart the program."); //Scanner to read user input Scanner ExitInput = new Scanner(System.in); //Assigns 'ExitReply' to user input ExitReply = ExitInput.nextLine(); //Verifies user put in valid answer if(ExitReply.equals("exit") || ExitReply.equals("Exit") || ExitReply.equals("Retry") || ExitReply.equals("retry")){ //If-else statement to Retry or Exit program if(ExitReply.equals("Retry") || ExitReply.equals("retry")){ //Restarts program convert(); exit(); } else { //Confirms exit System.out.println("--END--"); //Closes program System.exit(0); } } else{ //Prompts user to put in valid response System.out.println("Please input either 'exit' or 'retry'."); exit(); } } }
-
Re: Program skips "If" code and goes straight to "Else"
Note that your program uses recursion to solve it's problem: i.e., the method calls itself here:
Unless I were specifically told my instructor to solve it this way, I wouldn't do this as it can lead to some traps. Instead I'd use a while loop and simply set the boolean variable used to exit the while loop from inside of this if condition or its else block:Java Code://Verifies user input valid answer if(!TempScale.equals("F") && !TempScale.equals("f") && !TempScale.equals("C") && !TempScale.equals("c")){ System.out.println("Please input a valid answer. Valid answers are 'F', 'f', 'C', 'c'"); convert(); // ***** here the convert method calls itself *****
Java Code:boolean done = false; while (!done) { if (!TempScale.equals... etc...) { // warn user that the wrong String was entered } else { done = true; // do other stuff that needs to be done in this else block... } }Last edited by Fubarable; 01-21-2012 at 05:43 PM.
Similar Threads
-
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
I'm new to JAVA, stuck on "int"...Need some help getting this stuff straight, thanks!
By moe123 in forum New To JavaReplies: 5Last Post: 09-16-2009, 01:36 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks