Results 1 to 5 of 5
  1. #1
    Plato is offline Member
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Using 'else if' statements in a scanner

    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...

    Java 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 String that just displays text.
    		String value1 = "Please enter your age";
    
    		/**
    		 * A simple for loop that pauses after a certain variable is reached.
    		 */
    		for (int i = 0; i <= 200000; i++) {
    			System.out.println(i);
    			if (i == 100000) {
    				System.out
    						.println("System has paused the counter. Trying again in 3 seconds.");
    				Thread.sleep(1000); // 1000 Mills = 1 second.
    				System.out
    						.println("System has paused the counter. Trying again in 2 seconds.");
    				Thread.sleep(1000);
    				System.out
    						.println("System has paused the counter. Trying again in 1 second.");
    				Thread.sleep(1000);
    			}
    		}
    
    		/**
    		 * 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 :)
    Last edited by Plato; 09-06-2013 at 07:41 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default 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:

    Java Code:
    //less than or equal to 0 (this part you already have)
    if(input <=0) {
       //do something
    }
    //between 1 and 30
    else if(input between 1 and 30}{
      //do something
    }
    //between 31 and 4999
    else if(input between 31 and 4999) {
      //do something
    }
    //over 5000
    else {
      //do something
    }
    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.
    Last edited by sehudson; 09-06-2013 at 11:10 PM.

  3. #3
    Ubiquitous is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default 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.

    For example:
    Java Code:
    // Without a method to call
    public static void main(String[] args){
      // Lets count to 10
      for (int i = 0; i < 10; i++)
        System.out.print((i + 1) + (i < 9 ? " " : "\n"));
    
      System.out.println("Let's count again!");
    
      // Count to 10 second time
      for (int i = 0; i < 10; i++)
        System.out.print((i + 1) + (i < 9 ? " " : "\n"));
    }
    
    // With a method
    public static void main(String[] args){
      // Lets count to 10
      count();
      System.out.println("Let's count again!");
      count();
    }
    
    public static void count(){
      for (int i = 0; i < 10; i++)
        System.out.print((i + 1) + (i < 9 ? " " : "\n"));
    }

  4. #4
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Using 'else if' statements in a scanner

    Isn't ugly bru, If/else statement provide a clearer instruction on how to pass the result from each statement.

  5. #5
    Anthony5347 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Using 'else if' statements in a scanner

    If anything, using if-else statements is a very clean method to program with. Now using nested if-else statements can get real ugly. You can even use case structures if it works for your program.

Similar Threads

  1. If Statements or Something Better?
    By collegeconfused in forum New To Java
    Replies: 1
    Last Post: 05-05-2013, 11:28 AM
  2. Help with IF Statements and GUI
    By university123 in forum New To Java
    Replies: 8
    Last Post: 10-14-2010, 05:38 AM
  3. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 08:02 PM
  4. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 10:50 PM
  5. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 05:24 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
  •