Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Error in IF-Else Code

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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);
    	}
    }


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Error in IF-Else Code

    Quote Originally Posted by thatguywhoprograms View Post
    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?
    if(a==b){
    }
    if(b==c){
    }
    What's problem in here?


    To the OP: The if statement syntax : If StatementStatement ControlJava Tutorial See this.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error in IF-Else Code

    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.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    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");
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error in IF-Else Code

    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.
    I'm getting errors
    Copy and post here the full text of the error messages.

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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

    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)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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 ;

    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

    You should also read about how to code an if statement:
    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Last edited by Norm; December 19th, 2011 at 10:26 AM.

  9. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

    Quote Originally Posted by Norm View Post
    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

    You should also read about how to code an if statement:
    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
    if (String) usrInpt = "C" {

    And I am looking at the docs now, hopefully I can debug this code.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error in IF-Else Code

    It's not a bug. Its a syntax error.

  11. The Following User Says Thank You to Norm For This Useful Post:

    chronoz13 (January 10th, 2012)

  12. #11
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error in IF-Else Code

    Ask google or wikipedia.

  14. #13
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

    Quote Originally Posted by Norm View Post
    Ask google or wikipedia.

    Per Wikipedia,
    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.

  15. #14
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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");
    }
     
    }
    }

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error in IF-Else Code

    Instead of assuming, you could test if A was "F" and if not, print out an error message.

  17. #16
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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");
    }
    	}
     
     
     
     
     
    }

  18. #17
    Member
    Join Date
    Dec 2011
    Posts
    34
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

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

  19. #18
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

    Quote Originally Posted by imsuperman05 View Post
    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

  20. #19
    Member
    Join Date
    Dec 2011
    Posts
    34
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

    Quote Originally Posted by jonathon6017 View Post
    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.

  21. #20
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

    Quote Originally Posted by imsuperman05 View Post
    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

  22. #21
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

    The line should have been
    if(usrInpt=="c")

  23. #22
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Error in IF-Else Code

    Quote Originally Posted by ranjithfs1 View Post
    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

  24. #23
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

    Quote Originally Posted by copeg View Post
    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
    Sorry, the above reply of mine was made seeing the last reply on page 1 of this thread.
    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?

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error in IF-Else Code

    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).

  26. #25
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in IF-Else Code

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

    replace = with ==
    it works...

Page 1 of 2 12 LastLast

Similar Threads

  1. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  2. Getting Error in code pls help
    By gokul1242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 08:35 AM
  3. Error in code
    By JCTexas in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2011, 05:53 PM
  4. Error with code
    By JJTierney in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:23 PM