# Help simplifying huge nested if statement?

• October 10th, 2011, 03:31 PM
racecar333
Help simplifying huge nested if statement?
This is a programming project from a textbook, and it executes as intended. However, I can't help but thinking there are ways to simplify the code because the if statement I am using is massive, with nested if statements inside.

Any ideas on simplifying this code? Or any ideas on alternatives to the if statement I am using?

(The program is to convert 24-hour time into 12-hour time, and the jpb package is needed to use the SimpleIO class.)

Code Java:

```// the jpb package needed to use the SimpleIO class import jpb.*;   public class equivalent12HourTime { public static void main(String[] args) { // hh:mm user input SimpleIO.prompt("\nEnter a 24-hour time\n in military hh:mm format\n (example: 21:11 --> 11:11 p.m.) : "); String hh_mm = SimpleIO.readLine();   // int hour String hh = hh_mm.substring(0, 2); int hour = Integer.parseInt(hh);   // int minutes String mm = hh_mm.substring(3); int minutes = Integer.parseInt(mm);   /* ginormous nested if statement: 1. checks minutes for errors, 2. checks hours for errors, 3. converts 24-hour into 12-hour format and prints */ if ( (minutes <= 0) || (minutes >= 60) ) { System.out.println("\nIncorrect time entered, please try again."); } else { if ( (hour > 0) && (hour <= 12) ) { if (hour == 12) { System.out.println("\nEquivalent 12-hour time: " + hour + ":" + minutes + " p.m."); } else { System.out.println("\nEquivalent 12-hour time: " + hour + ":" + minutes + " a.m."); } } else if ( (hour > 12) && (hour <= 24) ) { if (hour == 24) { System.out.println("\nEquivalent 12-hour time: " + (hour - 12) + ":" + minutes + " a.m."); } else { System.out.println("\nEquivalent 12-hour time: " + (hour - 12) + ":" + minutes + " p.m."); } } else { System.out.println("\nIncorrect time entered, please try again."); } } // end of if statement } }```
• October 11th, 2011, 11:35 AM
luck999
Re: Help simplifying huge nested if statement?
In my opinion, there is no better solution than if statement in this situation.
• October 11th, 2011, 12:11 PM
KevinWorkman
Re: Help simplifying huge nested if statement?
Yeah, that if statement doesn't seem that bad.

You could try splitting it up into methods more though.

But really, at the early stages of programming, don't worry about getting everything exactly correct. Does it work? Do you know how it works?
• October 31st, 2011, 03:00 PM
racecar333
Re: Help simplifying huge nested if statement?
Hey thanks folks. Kevin -- what do you mean by splitting it up into methods more? Would this be to separate the information into categories for readability? (I'm thinking for the above code two separate methods for a.m. and p.m.?)

and please reply to another question here if you have the time --
http://www.javaprogrammingforums.com...i-am-noob.html
(I am having a lot of trouble with simple 2D animation right now)
• October 31st, 2011, 06:41 PM
KevinWorkman
Re: Help simplifying huge nested if statement?
Quote:

Originally Posted by racecar333
Hey thanks folks. Kevin -- what do you mean by splitting it up into methods more? Would this be to separate the information into categories for readability? (I'm thinking for the above code two separate methods for a.m. and p.m.?)

If you have a bunch of nested if statements, like this:

Code java:

```if(a){ if(b){ if(c){ //something } } } else{ if(d){ if(e){ //something else } } }```

You can make it more readable by doing something like this:

Code java:

```if(a){ doA(); } else{ if(d){ if(e){ //something else } } }   public void doA(){ if(b){ if(c){ //something } } )```

..and obviously you could further break that down.
• October 31st, 2011, 06:43 PM
KevinWorkman
Re: Help simplifying huge nested if statement?
Quote:

Originally Posted by racecar333
and please reply to another question here if you have the time --
http://www.javaprogrammingforums.com...i-am-noob.html
(I am having a lot of trouble with simple 2D animation right now)

Copeg already gave you the correct answer, I'm not sure what else you want me to say about it.
• November 1st, 2011, 01:03 AM
Tjstretch
Re: Help simplifying huge nested if statement?
Glancing at your code, I would recommen d that rather than all of the parameter if statements, it would be better to have one method that simply checks the validity of the input, returning aboolean. Then end the program if the parameters aren't valid. EG:
Code Java:

```String hh_mm = SimpleIO.readLine(); boolean isValid = checkValidity(hh_mm); if(!isValid) { System.err.println("Not a valid format"); System.exit(1); } //Rest vof the code public static boolean checkValidity(String hh_mm) { if(some validity check) { return false; } if(some other validity check) { return false; } return true; }```
But, as Kevin said, it is best not to worry to much about getting it perfect as a begginer. I remember when I looked back at my old code (around when I swtarted) a few weeks ago. I practically died laughing :)

@luck999 Nothing is perfect.