Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Some basic help with if-else statements

    Hi all, I'm looking for some help on an if-else statements project I have for my intro to java class. I'm done with the project, but I got stuck/confused on the last part. The final requirement is that when I input "n" for rent a home y / n, the program responds with "You entered no. Bye." I just can't figure out where in the code to place this statement, I keep getting the error "else statement without if statement."


    Thanks so much for any guidance / explanation on what I'm doing wrong.

    Here's the code :






    Java Code:
    import java.util.Scanner;
    
    public class House{
    
        public static void main(String[] args){
            Scanner kb = new Scanner(System.in);
    
       		double Compact=30.50;
    			double Full=40.50;
    			double discount=.07;
    			double FullDiscount= 37.67;
    			double CompactDiscount= 28.37;
    		 
    		  System.out.println("Programmed by John.");
            for (int i = 0; i < 4; i++)
                System.out.println();
    
            System.out.println("Rent A House? [Y or N]:");
            String answer = kb.next();
    
            if (answer.equalsIgnoreCase("y"))
    		  {
                System.out.print("Compact or Full-size? [C or F]:");
    
                answer = kb.next();
    
                if (answer.equalsIgnoreCase("c"))
    				{
    
                    System.out.println("You selected Compact.");
                    System.out.print("Have coupon? [Y or N]");
                    answer = kb.next();
                    if (answer.equalsIgnoreCase("n"))
                        System.out.print("Price is 30.50 per day.");
                    if (answer.equalsIgnoreCase("y"))	  
    						  System.out.printf("Price is " + FullDiscount + " per day");
    					 
    					
    
                } else if (answer.equalsIgnoreCase("f"))
    				{
    
                    System.out.println("You selected Full.");
                    System.out.print("Have coupon? [Y or N]");
                    answer = kb.next();
                    if (answer.equalsIgnoreCase("n"))
                        System.out.print("Price is 40.50 per day.");
                    if (answer.equalsIgnoreCase("y"))
    					 	  System.out.print("Price is " + CompactDiscount + " per day");
    					
    			
    
                }
    			     else 
    				{
                    System.out.println("You must enter C or F");
                    System.out.println("\nEnd of program.");
                }
    
            }
        }
    }

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Some basic help with if-else statements

    Where do you get that error?
    Your else in the posted code is behind the last else if (answer.equalsIgnoreCase("f"))

    You could add another

    else if (answer.equalsIgnoreCase("n"))
    {
    ...
    }

    after your current else { ... } }
    (So after 2 }} if I counted correctly!)

    Hint: If you have problems keeping the overview then it is a sign that the code is unreadable. You could simply create a function with some other stuff. So you could put the whole if area where you ask for Compact or full site into a function and then simply call that.

    With kind regards,

    Konrad

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Some basic help with if-else statements

    Well, you either want to rent a home or you don't. So it is a simple if/else block. If the answer is yes, then all the other stuff goes in the corresponding block. If the answer is no, then you print the message and exit the program.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Some basic help with if-else statements

    Quote Originally Posted by jim829 View Post
    Well, you either want to rent a home or you don't. So it is a simple if/else block. If the answer is yes, then all the other stuff goes in the corresponding block. If the answer is no, then you print the message and exit the program.
    But you could press a wrong key. So you should check if it is Y, N or something else.

    So a nice extension could be to have a function that gets a list of valid responses and then goes into a loop till a valid answer came. That way you could check it that way. (But maybe you want to have a yes / no / cancel question so the user can interrupt.

    WIth kind regards,

    Konrad

Similar Threads

  1. If Statements or Something Better?
    By collegeconfused in forum New To Java
    Replies: 1
    Last Post: 05-05-2013, 10:28 AM
  2. If Statements
    By WillHorne in forum New To Java
    Replies: 7
    Last Post: 03-01-2012, 12:47 PM
  3. If Statements
    By katymccl in forum New To Java
    Replies: 6
    Last Post: 02-02-2012, 08:06 AM
  4. Help with IF Statements and GUI
    By university123 in forum New To Java
    Replies: 8
    Last Post: 10-14-2010, 04:38 AM
  5. if else if statements
    By Allspark in forum New To Java
    Replies: 9
    Last Post: 09-28-2010, 06:50 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
  •