Didart

Data Type Finder

Sep 28th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DataTypeFinder {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String input = scanner.nextLine();
  8.  
  9.         while (!input.equals("END")) {
  10.             String dataType = "";
  11.             if (input.equalsIgnoreCase("true")
  12.                     || (input.equalsIgnoreCase("false"))) {
  13.                 dataType = "boolean";
  14.             } else if (input.length() == 1) {
  15.                 char symbol = input.charAt(0);
  16.                 if (symbol < 47 || symbol > 58) {
  17.                     dataType = "character";
  18.                 } else {
  19.                     dataType = "integer";
  20.                 }
  21.             } else {
  22.                 boolean isString = false;
  23.                 boolean isFloat = false;
  24.                 for (int i = 0; i < input.length(); i++) {
  25.                     char currentSymbol = input.charAt(i);
  26.                     if (currentSymbol < 45 || currentSymbol > 57) {
  27.                         isString = true;
  28.                     }
  29.                     if (currentSymbol == 46) {
  30.                         isFloat = true;
  31.                     }
  32.                 }
  33.                 if (isString) {
  34.                     dataType = "string";
  35.                 } else {
  36.                     if (isFloat) {
  37.                         dataType = "floating point";
  38.                     } else {
  39.                         dataType = "integer";
  40.                     }
  41.                 }
  42.             }
  43.             System.out.printf("%s is %s type%n", input, dataType);
  44.             input = scanner.nextLine();
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment