I find using 'else if' like crazy is really ugly and I need to know if another way of doing it is better. I'll provide the code...

Code:

import java.util.*;

public class Main {

/**
* A counter that pauses for 3 seconds and then continues. After that, the
* user may enter their age into the console and the system will reply with
* if the user is young or old.
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

/**
* A scanner for the user to input their age into the console.
*/
Scanner scanner = new Scanner(System.in);
System.out.println(value1);
int input = scanner.nextInt();
System.out.println("You are " + input + " years old.");
Thread.sleep(1500);

if (input <= 0) {
System.out.println("Because you are " + input
+ " years old, you are unborn.");
} else if (input <= 30) {
System.out.println("Because you are " + input
+ " years old, you are young.");
} else if (input >= 5000) {
System.out.println("Because you are " + input
+ " years old, you are dead.");
} else if (input >= 31) {
System.out.println("Because you are " + input
+ " years old, you are old.");
}

}

}

You see near the bottom are what I am talking about. There must be some way I can change it to be nicer or cleaner. Thanks :)

09-06-2013, 08:05 PM

sehudson

Re: Using 'else if' statements in a scanner

To answer your question, no, I don't think you can get around using if/else logic, without using something like a ternary operator, but if/else statements are ok.
I would refactor it a little bit though to make things a little clearer.
For example, your last if/else block says that if input is >= 31, that implies that you are old.
Well, 5000 is greater than 31, but in the case of 5000 and over, you want the output to be you are old, you are dead.
The way you have it structured works, but to make it clearer, I would have my if and if/else conditions be based on the number falling into a range, but that's just a suggestion. Your logic jumps from <=30 to >=5000, and >= 31. If you clearly define your number ranges, that helps make sure that you have covered all conditions and get the output you desire.
Something like this would be clearer to me, but maybe get some other suggestions:

The last else is a catch all, since when you get to that point, you know the value of input is 5000 or greater, so there would be no need for an else if condition. I'll leave it up to you to fill in the conditions if you want, but this approach would be clearer as to the paths based on the input value.

09-06-2013, 08:07 PM

Ubiquitous

Re: Using 'else if' statements in a scanner

May I ask what you see as ugly about if and else-if statements? They provide a clean structure for the program and other programmers to follow. The program checks the condition and if it is not met then it skips all the code in between the { } for that statement.

Your prior post were similar and showed concern for compact code. The way you are writting it as of now is already good. Do not focus on compact code instead focus on maintainable and easy to follow code.

Now if your concern is the main method looking to cluttered you can move it the portion that checks age to another method where it does all the checking. The advantage to having a method you can call is being able to execute the same code more than once without having to write it over and over again.