Advertisement
Ligh7_of_H3av3n

01.Data Type Finder

Jan 20th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. package MoreExercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DataTypeFinder {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         while (true) {
  10.             String input = scanner.nextLine();
  11.  
  12.             if (input.equals("END")) {
  13.                 break;
  14.             }
  15.  
  16.             // Determine data type
  17.             String dataType;
  18.             if (isInteger(input)) {
  19.                 dataType = "integer";
  20.             } else if (isFloatingPoint(input)) {
  21.                 dataType = "floating point";
  22.             } else if (isBoolean(input)) {
  23.                 dataType = "boolean";
  24.             } else if (isCharacter(input)) {
  25.                 dataType = "character";
  26.             } else {
  27.                 dataType = "string";
  28.             }
  29.  
  30.             // Print result
  31.             System.out.println(input + " is " + dataType + " type");
  32.         }
  33.  
  34.         scanner.close();
  35.     }
  36.  
  37.     // Check if the input is an integer
  38.     static boolean isInteger(String input) {
  39.         try {
  40.             Integer.parseInt(input);
  41.             return true;
  42.         } catch (NumberFormatException e) {
  43.             return false;
  44.         }
  45.     }
  46.  
  47.     // Check if the input is a floating point number
  48.     static boolean isFloatingPoint(String input) {
  49.         try {
  50.             Float.parseFloat(input);
  51.             return true;
  52.         } catch (NumberFormatException e) {
  53.             return false;
  54.         }
  55.     }
  56.  
  57.     // Check if the input is a boolean
  58.     static boolean isBoolean(String input) {
  59.         return input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false");
  60.     }
  61.  
  62.     // Check if the input is a single character
  63.     static boolean isCharacter(String input) {
  64.         return input.length() == 1;
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement