Is there other possible structure to implement the same functionality? Here is my code: public static void main(String[] args) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); while(true){ String s = r.readLine(); if(s.equals("exit")) break; if(s.contains(".")) { try{ Double d = Double.parseDouble(s); print(d); } catch(NumberFormatException e){ print(s); } } else{ try{ Integer i = Integer.parseInt(s); if(i>0 && i<128){ short sh = i.shortValue(); print(sh); } else print(i); } catch(NumberFormatException e){ print(s); } } } }