Results 1 to 3 of 3
  1. #1
    Red727 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Numerous 'else' without 'if' errors. HELP!!

    im pretty new to java so any thoughts as to why I keep getting this error would be greatly appreciated.
    __________________________________________________ _______________

    Java Code:
    import javax.swing.*;
    
    public class Pets{
    	public static void main (String[] args){
    
    		int residence, hoursHome;
    
    		residence = Integer.parseInt(JOptionPane.showInputDialog("PLEASE INDICATE WHICH TYPE OF RESIDENCE YOU LIVE IN.\n" +	"Enter an integer from 1 to 3 where:\n" + "1 = Apartment\n" + "2 = House\n" + "3 = Dormitory")); 
    
                              hoursHome = Integer.parseInt(JOptionPane.showInputDialog("PLEASE INDICATE HOW MANY HOURS YOU ARE HOME.\n" + "Enter an integer from 1 to 5 where:\n" + "1 = 18 or more hours\n" + "2 = 17 to 10 hours\n" + "3 = 9 to 8 hours\n" + "4 = 7 to 6 hours\n" + "5 = 5 to zero hours"));
    
    switch (residence){
        case 1:
    	if(hoursHome == 1 || hoursHome ==2);
                 {JOptionPane.showMessageDialog(null, "You have indicated you live in an Apartment and are home 10 or more hours.\n" + "The recommended pet is a Cat.");}
    
    	else if(hoursHome == 3 || hoursHome == 4);
                 {JOptionPane.showMessageDialog(null, "You have indicated you live in an Apartment and are home less than 10 hours.\n" + "The recommended pet is a Hamster.");}
    				      				else{JOptionPane.showMessageDialog(null, "You have indicated you live in an Apartment and are home less than 10 hours.\n" + "The recommended pet is a Hamster.");}
    	break;
    
        case 2:
    	if (hoursHome == 1);
    	{JOptionPane.showMessageDialog(null, "You have indicated you live in a House and are home 18 or more hours.\n" + "The recommended pet is a Pot-bellied Pig.");}
    								else if(hoursHome == 2);
    	{JOptionPane.showMessageDialog(null, "You have indicated you live in a House and are home 17 to 10 hours.\n" + "The recommended pet is a Dog.");}
    								else if(hoursHome == 3 || hoursHome == 4);
    	{JOptionPane.showMessageDialog(null, "You have indicated you live in a House and are home less than 10 hours.\n" +"The recommended pet is a Snake.");}
    								else{JOptionPane.showMessageDialog(null, "You have indicated you live in a House and are home less than 10 hours.\n" + "The recommended pet is a Snake.");}
    	break;
    
        case 3:
    	if(hoursHome == 1 || hoursHome ==2);
    	{JOptionPane.showMessageDialog(null, "You have indicated you live in a Dormitory and are home 6 or more hours.\n" + "The recommended pet is a Fish.");}
    								else if (hoursHome == 3 || hoursHome ==4);
    	{JOptionPane.showMessageDialog(null, "You have indicated you live in a Dormitory and are home 6 or more hours.\n" + "The recommended pet is a Fish.");}
    
    	else{JOptionPane.showMessageDialog(null, "You have indicated you live in a Dormitory and are home less than 6 hours.\n" + "The recommended pet is an Ant Farm.");}
    	break;
    
        default: {JOptionPane.showMessageDialog(null, "You have entered an illegal integer.");}
    		}
    
    		System.exit(0);
    
    	}
    }
    Last edited by Fubarable; 11-06-2010 at 09:51 PM. Reason: Moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Code tags added. Please read my signature link to learn how to use these yourself.

    You should never have a semicolon at the end of an if boolean test as that will short circuit the if. The compiler thinks that you want to call an empty block if the if statement is true.

    So this:
    Java Code:
    if (4 == 3);
      System.out.println("foo");  // why is this line always called?

    Is actually interpretted by the compiler to this:
    Java Code:
    if (4 == 3) {
       ;
    }
    System.out.println("foo");  // Now we see why the line is always called

    Better to do this:
    Java Code:
    if (4 == 3) { // no semicolon now and braces added.
      System.out.println("foo");
    }

    Also, always enclose all blocks in curly braces.

    Luck and welcome to the Java-Forums!
    Last edited by Fubarable; 11-06-2010 at 10:02 PM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Also, always enclose all blocks in curly braces.
    Which help you to avoid unexpected errors in your code, and some may very difficult to track at once. And also it's very clear the code, easy to read, not only for you for other as well. Keep theses practices with you all the way. :) Good luck!

Similar Threads

  1. errors
    By santosh chauhan in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 08:59 PM
  2. Getting errors
    By Abbinormal in forum New To Java
    Replies: 5
    Last Post: 01-15-2010, 07:01 AM
  3. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  4. write a selection sort without having numerous variable?
    By seandingobat in forum New To Java
    Replies: 6
    Last Post: 10-28-2008, 03:33 PM
  5. Return onyl 1 MX record if numerous exist
    By rlzyoner in forum New To Java
    Replies: 1
    Last Post: 08-06-2008, 12:41 AM

Posting Permissions

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