# Error in IF-Else Code

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• December 18th, 2011, 11:10 PM
jonathon6017
Error in IF-Else Code
Hello,
I'm new to java and I am trying to learn it as my first language and eventually move to Programming for Android. To get myself started for practice, I am working on a simple temperature conversion program. It is supposed to Ask the User for "C or F" (Celsius or Fahrenheit) Then if the reply was C, do the conversion, if F, convert to fahrenheit. If the user inputs anything else then give him/her an error. I keep getting errors on the code. First it was on the else code now it is on the if codes. Please help here is the code.

Code :

```import java.util.Scanner; import java.io.*; public class converter {   private static final boolean String = false; private static double sum;   public static void main(String args[]) {   Scanner scan = new Scanner(System.in); String usrInput;   System.out.print("C or F: "); String usrInpt = scan.next();   if (String) usrInpt = "C" {   Scanner temp = new Scanner(System.in); System.out.println("Temp: "); String celcius = scan.next(); double n = Double.parseDouble(celcius); double sum = ((n * 1.8) + 32); } if (String) usrInpt = "F" { System.out.println("Working on this"); }   else { System.out.println("ERROR"); }     System.out.println((double) sum); } }```
• December 19th, 2011, 12:49 AM
thatguywhoprograms
Re: Error in IF-Else Code
Ah a but a simple solution, you cannot have two if statements right next to each other, what you have to do for the second if is add a else behind it like:

else if (String) usrInpt = "F" {
System.out.println("Working on this");
}

else {
System.out.println("ERROR");

If you do that the program should work perfectly, also a suggestion, I'm assuming you didn't start with python so beware of your indents in your code cause when you get to more complex stuff, if its not aligned your code will be pretty unreadable not knowing what goes were even if they're curly braces.
• December 19th, 2011, 03:16 AM
Mr.777
Re: Error in IF-Else Code
Quote:

Originally Posted by thatguywhoprograms
Ah a but a simple solution, you cannot have two if statements right next to each other, what you have to do for the second if is add a else behind it like:

else if (String) usrInpt = "F" {
System.out.println("Working on this");
}

else {
System.out.println("ERROR");

If you do that the program should work perfectly, also a suggestion, I'm assuming you didn't start with python so beware of your indents in your code cause when you get to more complex stuff, if its not aligned your code will be pretty unreadable not knowing what goes were even if they're curly braces.

What do you mean you can not have two if statements right next to each other?
Code :

```if(a==b){ } if(b==c){ }```
What's problem in here?

To the OP: The if statement syntax : If StatementStatement ControlJava Tutorial See this.
• December 19th, 2011, 06:52 AM
Norm
Re: Error in IF-Else Code
Quote:

if (String) usrInpt = "C"
This is an assignment (=) statement not an equality(==) test.
However, You should use the equals() method to compare Strings. The == operator is for primitives.
• December 19th, 2011, 07:53 AM
jonathon6017
Re: Error in IF-Else Code
I done what you said but now I'm getting errors on the if and the else if statements here is my revised code. hat did I do wrong?

Code :

```if (String) usrInpt = "C" {   Scanner temp = new Scanner(System.in); System.out.println("Temp: "); String celcius = scan.next(); double n = Double.parseDouble(celcius); double sum = ((n * 1.8) + 32); } else if (String) usrInpt = "F" { System.out.println("Working on this"); }   else { System.out.println("ERROR"); }```
• December 19th, 2011, 09:10 AM
Norm
Re: Error in IF-Else Code
Quote:

if (String) usrInpt = "C" {
This is an assignment (=) statement not an equality(==) test.
However, You should use the equals() method to compare Strings. The == operator is for primitives.
Quote:

I'm getting errors
Copy and post here the full text of the error messages.
• December 19th, 2011, 09:15 AM
jonathon6017
Re: Error in IF-Else Code
Do you mind giving me an example on how to use the equals() method

Here is the full code
Code :

```import java.util.Scanner; import java.io.*; public class converter {   private static final boolean String = false; private static double sum;   public static void main(String args[]) {   Scanner scan = new Scanner(System.in); String usrInput;   System.out.print("C or F: "); String usrInpt = scan.next();   if (String) usrInpt = "C" {   Scanner temp = new Scanner(System.in); System.out.println("Temp: "); String celcius = scan.next(); double n = Double.parseDouble(celcius); double sum = ((n * 1.8) + 32); } else if (String) usrInpt = "F" { System.out.println("Working on this"); }   else { System.out.println("ERROR"); }     System.out.println((double) sum); } }```

Here is the errors

Code :

```Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error, insert ";" to complete Statement Syntax error on token(s), misplaced construct(s)   at converter.main(converter.java:17)```
• December 19th, 2011, 09:23 AM
Norm
Re: Error in IF-Else Code
Which statement is at line 17?
The compiler is saying that the statement on line 17 needs and ending ;

Quote:

how to use the equals() method
You need to understand how to read the API doc that describes how to use a class's methods.
Have you read the API doc for the String class? Do you have any questions about its description of the equals() method?
Locate String in lower left and click the link for the doc
Java Platform SE 6

The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
• December 19th, 2011, 08:23 PM
jonathon6017
Re: Error in IF-Else Code
Quote:

Originally Posted by Norm
Which statement is at line 17?
The compiler is saying that the statement on line 17 needs and ending ;

You need to understand how to read the API doc that describes how to use a class's methods.
Have you read the API doc for the String class? Do you have any questions about its description of the equals() method?
Locate String in lower left and click the link for the doc
Java Platform SE 6

The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

Sorry for replying so late. At the time of my last post I was away from my computer and was working on my phone through a VNC connection to my computer. Anyways Line 17 is
Code :

`if (String) usrInpt = "C" {`

And I am looking at the docs now, hopefully I can debug this code.
• December 19th, 2011, 08:36 PM
Norm
Re: Error in IF-Else Code
It's not a bug. Its a syntax error.
• December 19th, 2011, 08:39 PM
jonathon6017
Re: Error in IF-Else Code
Isn't a bug usually a programmer error and since this is my error it would be considered a bug?
• December 19th, 2011, 08:49 PM
Norm
Re: Error in IF-Else Code
• December 19th, 2011, 08:56 PM
jonathon6017
Re: Error in IF-Else Code
Quote:

Originally Posted by Norm

Per Wikipedia,
Quote:

A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people.
• December 19th, 2011, 09:04 PM
jonathon6017
Re: Error in IF-Else Code
Alright I got frustrated and decided to do a complete rewrite. This is what I have so far and it finally compiled and ran successfully.

Code :

```import java.util.*; public class convert {   public static void main(String args[]) { System.out.println("C or F?: "); Scanner scan = new Scanner(System.in); String A = scan.nextLine();   if (A.equals("C")) { System.out.println("C"); } else { System.out.println("I assume you said F"); }   } }```
• December 20th, 2011, 05:29 AM
Norm
Re: Error in IF-Else Code
Instead of assuming, you could test if A was "F" and if not, print out an error message.
• December 20th, 2011, 06:48 AM
jonathon6017
Re: Error in IF-Else Code
Well I was up till 1 last night trying to get it working and I think I finally did. It will ask for "C or F" Then if C or F is entered it will ask for the temperature and then do the conversion and loop back to the beginning. If anything but C or F was entered then it throws an error.

Code :

```import java.util.*; public class convert {   public static void main(String args[]) {   dothis(); } public static void dothis(){ System.out.println(""); System.out.println("C or F?: "); Scanner scan = new Scanner(System.in); String A = scan.nextLine();   if (A.equals("C")) { System.out.println("What is the temperature in Celcius: "); Scanner cel = new Scanner(System.in); double B = Double.parseDouble(cel.nextLine()); double ans = ((B * 9/5) + 32); System.out.print((double) ans); dothis(); } else if (A.equals("F")){ System.out.println("What is the temperature in Farenheit: "); Scanner fa = new Scanner(System.in); double F = Double.parseDouble(fa.nextLine()); double Fans = ((F - 32) * 5/9 ); System.out.print((double) Fans); dothis(); } else { System.out.println("ERROR"); } }           }```
• December 23rd, 2011, 09:44 PM
imsuperman05
Re: Error in IF-Else Code
Code :

`if(usrInpt.equals("C")) {`
You need to have parenthesis to surround the if statement.
• December 24th, 2011, 12:15 AM
jonathon6017
Re: Error in IF-Else Code
Quote:

Originally Posted by imsuperman05
Code :

`if(usrInpt.equals("C")) {`
You need to have parenthesis to surround the if statement.

What do you mean? The program runs without error so if I may ask, can I see an example and what will the parenthesis do? I don't mean to sound like a know-it-all, I'm just wondering why I performed an error
• December 24th, 2011, 12:43 AM
imsuperman05
Re: Error in IF-Else Code
Quote:

Originally Posted by jonathon6017
What do you mean? The program runs without error so if I may ask, can I see an example and what will the parenthesis do? I don't mean to sound like a know-it-all, I'm just wondering why I performed an error

Do you even compile the file? It should show errors.
• December 24th, 2011, 04:26 PM
jonathon6017
Re: Error in IF-Else Code
Quote:

Originally Posted by imsuperman05
Do you even compile the file? It should show errors.

The exact code I posted above is what I compiled and ran and it ran with no errors
• January 7th, 2012, 11:11 PM
ranjithfs1
Re: Error in IF-Else Code
The line should have been
if(usrInpt=="c")
• January 7th, 2012, 11:16 PM
copeg
Re: Error in IF-Else Code
Quote:

Originally Posted by ranjithfs1
The line should have been
if(usrInpt=="c")

FYI Never, unless you have reason to compare objects, use == to compare strings. String's are objects, and thus should be compared using the equals method
• January 7th, 2012, 11:19 PM
ranjithfs1
Re: Error in IF-Else Code
Quote:

Originally Posted by copeg
FYI Never, unless you have reason to compare objects, use == to compare strings. String's are objects, and thus should be compared using the equals method

But, I always use == to compare a variable(referring to a String) with a string constant in decision making statements. What's wrong with it?
• January 8th, 2012, 06:12 AM
Norm
Re: Error in IF-Else Code
Quote:

What's wrong with it?
The == operator tests if the two references point/refer to the same object.
It is possible for there to be two Strings (ie two different objects) with the same contents. The equals method would say they had the same value (true), the == operator would say the references point to different objects (false).
• January 18th, 2012, 01:45 AM
RaoPatek
Re: Error in IF-Else Code
Quote:

if (String) usrInpt = "C"
Try this one
if (String) usrInpt = ="C"

replace = with ==
it works...
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last