Advertisement
Tsuki11

DataTypeFinder

Jan 28th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MineDataTypeFinder {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.  
  7.  
  8.         String input = scan.nextLine();
  9.         while (!"End".equals(input)) {
  10.             boolean isNumInt = true;
  11.  
  12.             try {
  13.                 int inputToInt = Integer.parseInt(input); // за инт - инт дава true
  14.             } catch (NumberFormatException e ) {
  15.                // System.out.println(e);
  16.                 isNumInt = false;
  17.             }
  18.  
  19.             boolean isNumDouble = true;
  20.  
  21.             try {
  22.                 double inputToDouble = Double.parseDouble(input); //  за инт - double Хвърля true
  23.             } catch (NumberFormatException e) { // exceptions are different types !!!
  24.                 isNumDouble = false;
  25.             }
  26.  
  27.             if (isNumInt) { // проверяваме първо за инт число,защото double побира инт и също е дал true,
  28.                 // ще влезе в неговата проверка
  29.                 System.out.printf("%s is integer type\n", input);
  30.             } else if (isNumDouble) {
  31.                 System.out.printf("%s is floating point type\n", input);
  32.             } else if ("true".equalsIgnoreCase(input) || "false".equalsIgnoreCase(input)) {
  33.                 System.out.printf("%s is boolean type\n", input);
  34.  
  35.             } else if (input.length() > 1) { // ако думата  има повече от една стойности
  36.                 System.out.printf("%s is string type\n", input);
  37.             } else {
  38.                 System.out.printf("%s is character type\n", input);
  39.             }
  40.  
  41.             input = scan.nextLine();
  42.  
  43.             if ("end".equalsIgnoreCase(input)){
  44.                 break;
  45.             }
  46.         }
  47.  
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement