Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By superhaNds

Thread: If/Else Compiling Error Problems

  1. #1
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Angry If/Else Compiling Error Problems

    The problem I am working on:


    Assume* that an int*variable* age has been declared* and already* given* a value*. Assume* further that the user has just been presented with the following menu:
    S: hangar steak, red potatoes, asparagus
    T: whole trout, long rice, brussel sprouts
    B: cheddar cheeseburger, steak fries, cole slaw
    (Yes, this menu really IS a menu!)
    Write some code that reads the String* (S or T or B) that the user types* in into a String*variable* choice that has already* been declared* and prints out a recommended accompanying drink as follows: if the value* of age is 21 or lower, the recommendation is "vegetable juice" for steak, "cranberry juice" for trout, and "soda" for the burger. Otherwise, the recommendations are "cabernet", "chardonnay", and "IPA" for steak, trout, and burger respectively. Regardless of the value* of age , your code should print "invalid menu selection" if the character* read into choice was not S or T or B.

    ASSUME* the availability of a variable*, stdin , that references* a Scannerobject* associated with standard input.

    And this is the coding that I have been submitting:

    Java Code:
    choice = stdin.next(); 
    if(choice.equals("S"))
    if (age <= 21) 
    System.out.println("vegetable juice"); 
    System.out.println("cabernet");{}
    else if (choice = "T"){ 
    if (age <= 21) 
    System.out.println("cranberry juice"); 
    System.out.println("chardonnay");} 
    else if (choice = "B"){ 
    if (age <= 21) 
    System.out.println("soda"); 
    System.out.println("IPA");} 
    else 
    System.out.println("invalid menu selection");
    But the compile error it is giving me is:
    CTest.java:14: error: 'else' without 'if'
    else if (choice = "T"){

    But I could've sworn my if statement of <= 21 was the if before the else if?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: If/Else Compiling Error Problems

    If you want more than one statement 'under' an if-clause, use curly brackets, i.e. if ( ... ) { ... } if you're not so proficient yet, always use those curly brackets.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: If/Else Compiling Error Problems

    Java Code:
    else if (choice = "T")
    else if (choice = "B")
    With this code you assign rather than compare, use equals method again.

    EDIT: Won't compile either because an if statement requires a boolean expression to evaluate, with your example you try to evaluate a string something
    that cannot happen since it's not a boolean.
    Last edited by superhaNds; 07-01-2013 at 03:44 PM. Reason: Missing clarification

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: If/Else Compiling Error Problems

    That's why you should always use braces with if/else statements (and for/while/do..while loops). The start of your code is equivalent to
    Java Code:
    choice = stdin.next(); 
    if(choice.equals("S")) {
      if (age <= 21) {
        System.out.println("vegetable juice"); 
      }
      System.out.println("cabernet");{}
      else if (choice = "T"){ // <--- huh?
    That's probably not what you intended.

    And what's that empty pair of braces doing at the end of line 5 (in your posted code) anyways?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: If/Else Compiling Error Problems

    And who's the slow old sod this time?
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: If/Else Compiling Error Problems

    Ok so I fixed the bracket issue and the else/if with T issue but now its telling me that "*⇒*****The value of _stdout is incorrect." And it highlights the phrases vegetable juice, cranberry juice, and invalid menu selection, also stating that there should possibly be something after the semi colon in the lines with vegetable juice and cranberry juice.
    Java Code:
    System.out.println("vegetable juice");
    I have tried putting brackets at the end of those statements, I have tried making an extra if line stating (if >=21) etc, I am just starting to get at a loss because I keep trying so many different things I am confusing myself.

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: If/Else Compiling Error Problems

    re-post your fixed code.
    DarrylBurke likes this.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: If/Else Compiling Error Problems

    Quote Originally Posted by superhaNds View Post
    Java Code:
    else if (choice = "T")
    else if (choice = "B")
    With this code you assign rather than compare, use equals method again.
    That won't even compile; please test your code snippets before posting.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Problems with running and compiling application
    By lugaru in forum New To Java
    Replies: 1
    Last Post: 07-02-2012, 12:12 AM
  2. Compiling problems
    By sunde887 in forum New To Java
    Replies: 16
    Last Post: 01-05-2011, 08:49 PM
  3. Problems compiling
    By Karnij in forum New To Java
    Replies: 6
    Last Post: 06-29-2009, 04:25 AM
  4. compiling problems.
    By Tikasas in forum New To Java
    Replies: 7
    Last Post: 04-13-2009, 10:00 PM
  5. problems when compiling
    By valery in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •