patternjavaMajor
Classifying the user into an age bracket
Viewed 0 times
theageuserintoclassifyingbracket
Problem
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.
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.
/* 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>=0 && age=3 && age=5 && age=12 && age=15 && age=19 && age=23 && age=66 && age<=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.
Solution
A
Then, loop through entries...
The default behaviour for a TreeMap is that the contents are sorted based on the key, which explains why this works. Use
You can even use a built-in function for this:
Making the final code...
TreeMap 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 entry : treemap.entrySet()){
if(entry.getKey() >= 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 treemap = new TreeMap<>();
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());
}
}
}Code Snippets
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");for(Map.Entry<Integer, String> entry : treemap.entrySet()){
if(entry.getKey() >= age){
System.out.println(entry.getValue());
break;
}
}System.out.println(treemap.ceilingEntry(age).getValue());import java.io.*;
import java.util.TreeMap;
public class Main {
private static TreeMap<Integer, String> treemap = new TreeMap<>();
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());
}
}
}Context
StackExchange Code Review Q#138224, answer score: 20
Revisions (0)
No revisions yet.