Hi, I have spent about 4 hours on this program so far but it's starting to kill me. If I add input
" This is a String. Value: -555000000000000000000000000000000000000000000000000000000000000000000000000000000 " in the program, nothing happens.
If I add just the number: -555000000000000000000000000000000000000000000000000000000000000000000000000000000 it prints that it is a String.
Can you please help me?
Thanks.
package com.codegym.task.task15.task1519;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.regex.Pattern;
/*
Different methods for different types
*/
public class Solution {
private static String nrInput;
private final static String Digits = "(\\p{Digit}+)";
private final static String HexDigits = "(\\p{XDigit}+)";
// an exponent is 'e' or 'E' followed by an optionally
// signed decimal integer.
private final static String Exp = "[eE][+-]?"+Digits;
private final static String fpRegex =
("[\\x00-\\x20]*"+ // Optional leading "whitespace"
"[+-]?(" + // Optional sign character
"NaN|" + // "NaN" string
"Infinity|" + // "Infinity" string
// A decimal floating-point string representing a finite positive
// number without a leading sign has at most five basic pieces:
// Digits . Digits ExponentPart FloatTypeSuffix
//
// Since this method allows integer-only strings as input
// in addition to strings of floating-point literals, the
// two sub-patterns below are simplifications of the grammar
// productions from section 3.10.2 of
// The Java Language Specification.
// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
"((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
// . Digits ExponentPart_opt FloatTypeSuffix_opt
"(\\.("+Digits+")("+Exp+")?)|"+
// Hexadecimal strings
"((" +
// 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "(\\.)?)|" +
// 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
")[pP][+-]?" + Digits + "))" +
"[fFdD]?))" +
"[\\x00-\\x20]*");// Optional trailing "whitespace"
private static Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");
public static boolean isNumeric(String strNum) {
if (strNum == null)
return false;
return pattern.matcher(strNum).matches();
}
public static void main(String[] args) throws IOException {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
nrInput = reader.readLine();
if (nrInput.equals("exit")) {
print(nrInput);
break;
}
try{
if (isNumeric(nrInput)) {
if (nrInput.contains(".")) {
if (isNumeric(nrInput)) print(Double.parseDouble(nrInput));
}
else if ((Integer.parseInt(nrInput) <= 0 || Integer.parseInt(nrInput) >= 128))
print(Integer.parseInt(nrInput));
else if ((Short.parseShort(nrInput) > 0 && Short.parseShort(nrInput) < 128))
print(Short.parseShort(nrInput));
}} catch (NumberFormatException e) {print(nrInput);}
print(nrInput);
}
}
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);
}
}