On top of that I wanted to ask, if it's allowed to execute method call inside catch block?
package com.codegym.task.task15.task1519;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/*
Different methods for different types
1. Read data from the console until the word "exit" is entered.
2. For each value (except "exit"), call the print method. If the value:
2.1. contains a period ("."), then call the print method for Double;
2.2. is greater than zero, but less than 128, then call the print method for short;
2.3. less than or equal to zero or greater than or equal to 128, then call the print method for Integer;
2.4. otherwise, call the print method for String.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
while (!input.equals("exit"))
{
try
{
if (input.contains(".")) { print(Double.parseDouble(input)); }
else if(Short.parseShort(input)>0 && Integer.parseInt(input)<128)
{
print(Short.parseShort(input));
}
else if(Integer.parseInt(input)>=128 || Integer.parseInt(input)<=0)
{
print(Integer.parseInt(input));
}
}
catch (NumberFormatException g){
print(input); //Is it allowed to place a method call inside the catch block?
}
input=br.readLine();
}
}
public static void print(Double value) {
System.out.println("This is a Double. Value: " + value);
}
public static void print(String value) {
System.out.println("This is a String. Value: " + value);
}
public static void print(short value) {
System.out.println("This is a short. Value: " + value);
}
public static void print(Integer value) {
System.out.println("This is an Integer. Value: " + value);
}
}
package com.codegym.task.task15.task1519;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/*
Different methods for different types
1. Read data from the console until the word "exit" is entered.
2. For each value (except "exit"), call the print method. If the value:
2.1. contains a period ("."), then call the print method for Double;
2.2. is greater than zero, but less than 128, then call the print method for short;
2.3. less than or equal to zero or greater than or equal to 128, then call the print method for Integer;
2.4. otherwise, call the print method for String.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
while (!input.equals("exit"))
{
try
{
if (input.contains(".")) { print(Double.parseDouble(input)); }
else if(Short.parseShort(input)>0 && Integer.parseInt(input)<128)
{
print(Short.parseShort(input));
}
else if(Integer.parseInt(input)>=128 || Integer.parseInt(input)<=0)
{
print(Integer.parseInt(input));
}
}
catch (NumberFormatException g){
print(input); //Is it allowed to place a method call inside the catch block?
}
input=br.readLine();
}
}
public static void print(Double value) {
System.out.println("This is a Double. Value: " + value);
}
public static void print(String value) {
System.out.println("This is a String. Value: " + value);
}
public static void print(short value) {
System.out.println("This is a short. Value: " + value);
}
public static void print(Integer value) {
System.out.println("This is an Integer. Value: " + value);
}
}