Advertisement
desislava_topuzakova

01. Data Type Finder

Sep 24th, 2022
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DataTypeFinder_01 {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. //• Integer
  8. //• Floating point -> съдържа символа .
  9. //• Characters -> дължина 1 и код < 48 || код > 57
  10. //• Boolean -> true или false
  11. //• Strings -> ако имаме последователност само от символи (код < 45 || код > 57)
  12. String input = scanner.nextLine();
  13. //stop: input == "END"
  14. while (!input.equals("END")) {
  15. String type = "";
  16. //проверка за типовете
  17. //boolean
  18. if(input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false")) {
  19. type = "boolean";
  20. }
  21. //character
  22. else if(input.length() == 1) {
  23. char symbol = input.charAt(0);
  24. //символ
  25. if(symbol < 47 || symbol > 58) {
  26. type = "character";
  27. }
  28. //едноцифрено цяло число
  29. else {
  30. type = "integer";
  31. }
  32. } else { //string, floating point, integer
  33. boolean isString = false;
  34. boolean isFloat = false;
  35. for (int i = 0; i < input.length(); i++) {
  36. char currentSymbol = input.charAt(i);
  37. //проверка дали е символ
  38. if(currentSymbol < 45 || currentSymbol > 57) {
  39. isString = true;
  40. }
  41.  
  42. if(currentSymbol == 46) {
  43. isFloat = true;
  44. }
  45. }
  46. if(isString) {
  47. type = "string";
  48. } else {
  49. //число
  50. if(isFloat) {
  51. type = "floating point";
  52. } else {
  53. type = "integer";
  54. }
  55.  
  56. }
  57. }
  58. System.out.printf("%s is %s type%n", input, type);
  59. input = scanner.nextLine();
  60. }
  61. }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement