Advertisement
Guest User

Untitled

a guest
Sep 26th, 2021
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DataTypeFinder1 {
  4.     private static boolean tryParseInt(String value) {
  5.         try {
  6.             Integer.parseInt(value);
  7.             return true;
  8.         } catch (NumberFormatException e) {
  9.             return false;
  10.         }
  11.     }
  12.     private static boolean tryParseDouble(String value) {
  13.         try {
  14.             Double.parseDouble(value);
  15.             return true;
  16.         } catch (NumberFormatException e) {
  17.             return false;
  18.         }
  19.     }
  20.     public static void main(String[] args) {
  21.         Scanner scanner = new Scanner(System.in);
  22.         String input = scanner.nextLine();
  23.         while (!input.equals("END")) {
  24.             if (input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false")) {
  25.                 System.out.printf("%s is boolean type%n", input);
  26.  
  27.             }else if (tryParseInt(input)){
  28.                 System.out.printf("%s is integer type%n",input);
  29.  
  30.             }else if (tryParseDouble(input)){
  31.                 System.out.printf("%s is floating point type%n",input);
  32.  
  33.             } else if (input.length() == 1) {
  34.                 System.out.printf("%s is character type%n", input);
  35.  
  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