package com.codegym.task.task15.task1519;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.CacheRequest;
/*
Different methods for different types
*/
public class Solution {
public static void main(String[] args) throws IOException {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input;
while (true) {
input = reader.readLine().trim().replaceAll("\\s+", "");
if (input.equals("exit")) break;
if (input.contains(".") && isDouble(input)) {
try {
Double doubleInput = Double.parseDouble(input);
print(doubleInput);
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
}
else if (isShort(input)) {
try {
Short shortInput = Short.parseShort(input);
if (shortInput > 0 && shortInput < 128) print(shortInput);
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
} else if (isInteger(input)) {
try {
Integer integerInput = Integer.parseInt(input);
if (integerInput <= 0 || integerInput >= 128) print(integerInput);
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
}
else print(input);
}
}
// check if input is a Double
public static boolean isDouble(String input) {
try {
Double doubleInput = Double.parseDouble(input);
} catch (NumberFormatException ex) {
return false;
} catch (NullPointerException ex) {
return false;
}
return true;
}
// check if input is a Short
public static boolean isShort(String input) {
try {
Short shorInput = Short.parseShort(input);
} catch (NumberFormatException ex) {
return false;
} catch (NullPointerException ex) {
return false;
}
return true;
}
//check if input is an Integer
public static boolean isInteger(String input) {
try {
Integer integerInput = Integer.parseInt(input);
} catch (NumberFormatException ex) {
return false;
} catch (NullPointerException ex) {
return false;
}
return true;
}
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);
}
}
A little Hint is appreciated. Please Help
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Oleh
14 April 2019, 11:21
Problem is in the short check.
For example:
isShort(1234) - returns true;
but 1234 doesn't match the 1-127 range so if statement ends without isInteger check.
0
Fa Yu
8 July 2019, 06:44
could you please help with my code - it works perfectly as per all conditions, but won't verified:
0