Results 1 to 3 of 3
- 11-06-2010, 08:41 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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 08:51 PM. Reason: Moderator edit: code tags added
-
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 09:02 PM.
- 11-07-2010, 01:39 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
errors
By santosh chauhan in forum New To JavaReplies: 5Last Post: 07-26-2010, 07:59 PM -
Getting errors
By Abbinormal in forum New To JavaReplies: 5Last Post: 01-15-2010, 06:01 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
write a selection sort without having numerous variable?
By seandingobat in forum New To JavaReplies: 6Last Post: 10-28-2008, 02:33 PM -
Return onyl 1 MX record if numerous exist
By rlzyoner in forum New To JavaReplies: 1Last Post: 08-05-2008, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks