Advertisement
mark79

Java Fundamentals - Data Type Finder

Aug 22nd, 2019
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DataTypeFinder {
  4.  
  5.     private static boolean tryParseInt(String value) {
  6.         try {
  7.             Integer.parseInt(value);
  8.             return true;
  9.         } catch (NumberFormatException e) {
  10.             return false;
  11.         }
  12.     }
  13.  
  14.     private static boolean tryParseDouble(String value) {
  15.         try {
  16.             Double.parseDouble(value);
  17.             return true;
  18.         } catch (NumberFormatException e) {
  19.             return false;
  20.         }
  21.     }
  22.  
  23.     public static void main(String[] args) {
  24.         Scanner scanner = new Scanner(System.in);
  25.  
  26.         String input = scanner.nextLine();
  27.         while (!input.equals("END")) {
  28.             if (tryParseInt(input)) {
  29.                 System.out.printf("%s is integer type%n", input);
  30.             } else if (tryParseDouble(input)) {
  31.                 System.out.printf("%s is floating point type%n", input);
  32.             } else if (input.length() == 1) {
  33.                 System.out.printf("%s is character type%n", input);
  34.             } else if (input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false")) {
  35.                 System.out.printf("%s is boolean type%n", input);
  36.             } else {
  37.                 System.out.printf("%s is string type%n", input);
  38.             }
  39.             input = scanner.nextLine();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement