Classifying the user into an age bracket In this program, I have to check the range of a given age and then print a message as per the age. I want to optimize the if else  logic.

/* Sample code to read in test cases:*/

import java.io.*;
public class Main {
 public static void main (String[] args) throws IOException {
    File file = new File(args[0]);
    BufferedReader buffer = new BufferedReader(new FileReader(file));
    String line;
    while ((line = buffer.readLine()) != null) {
        line = line.trim();
        // Process line of input Here

        int age = Integer.parseInt(line);

          if(age&gt;=0 &amp;&amp; age&lt;=2){
              System.out.println("Still in Mama's arms");
             // break;
          }else if(age&gt;=3 &amp;&amp; age&lt;=4){
               System.out.println("Preschool Maniac");
              // break;
          }else if(age&gt;=5 &amp;&amp; age&lt;=11){
               System.out.println("Elementary school");
             // break;
          }else if(age&gt;=12 &amp;&amp; age&lt;=14){
               System.out.println("Middle school");
             // break;
          }else if(age&gt;=15 &amp;&amp; age&lt;=18){
               System.out.println("High school");
             // break;
          }else if(age&gt;=19 &amp;&amp; age&lt;=22){
               System.out.println("College");
             // break;
          }else if(age&gt;=23 &amp;&amp; age&lt;=65){
               System.out.println("Working for the man");
             // break;
          }else if(age&gt;=66 &amp;&amp; age&lt;=100){
               System.out.println("The Golden Years");
              //break;
          }else{
               System.out.println("This program is for humans");

          }
         }
      }
    }


Is there any other way to do this program? Using a switch, it becomes more complex to type with so many cases for the age.
A TreeMap&lt;Integer, String&gt; with a for loop and stop on first number greater than or equaling the number to find will solve your problem.

treemap.put(-1, "This program is for humans");
treemap.put(2, "Still in Mama's arms");
//etc...
treemap.put(Integer.MAX_VALUE, "This program is for humans");


Then, loop through entries...

for(Map.Entry&lt;Integer, String&gt; entry : treemap.entrySet()){
    if(entry.getKey() &gt;= age){
        System.out.println(entry.getValue());
        break;
    }
}


The default behaviour for a TreeMap is that the contents are sorted based on the key, which explains why this works. Use Integer.MAX_VALUE for the last case - it'll be equal to or greater than any other number.

You can even use a built-in function for this:

System.out.println(treemap.ceilingEntry(age).getValue());


Making the final code...

import java.io.*;
import java.util.TreeMap;

public class Main {
    private static TreeMap&lt;Integer, String&gt; treemap = new TreeMap&lt;&gt;();

    private static void initializeAgeDescriptions() {
        treemap.put(-1, "This program is for humans");
        treemap.put(2, "Still in Mama's arms");
        treemap.put(4, "Preschool Maniac");
        treemap.put(11, "Elementary school");
        treemap.put(14, "Middle school");
        treemap.put(18, "High school");
        treemap.put(22, "College");
        treemap.put(65, "Working for the man");
        treemap.put(100, "The Golden Years");
        treemap.put(Integer.MAX_VALUE, "This program is for humans");
    }

    public static void main (String[] args) throws IOException {
        initializeAgeDescriptions();
        File file = new File(args[0]);
        BufferedReader buffer = new BufferedReader(new FileReader(file));
        String line;
        while ((line = buffer.readLine()) != null) {
            line = line.trim();

            int age = Integer.parseInt(line);
            System.out.println(treemap.ceilingEntry(age).getValue());
        }
    }
}