Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MoreExercise;
- import java.util.Scanner;
- public class DataTypeFinder {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- while (true) {
- String input = scanner.nextLine();
- if (input.equals("END")) {
- break;
- }
- // Determine data type
- String dataType;
- if (isInteger(input)) {
- dataType = "integer";
- } else if (isFloatingPoint(input)) {
- dataType = "floating point";
- } else if (isBoolean(input)) {
- dataType = "boolean";
- } else if (isCharacter(input)) {
- dataType = "character";
- } else {
- dataType = "string";
- }
- // Print result
- System.out.println(input + " is " + dataType + " type");
- }
- scanner.close();
- }
- // Check if the input is an integer
- static boolean isInteger(String input) {
- try {
- Integer.parseInt(input);
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- // Check if the input is a floating point number
- static boolean isFloatingPoint(String input) {
- try {
- Float.parseFloat(input);
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- // Check if the input is a boolean
- static boolean isBoolean(String input) {
- return input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false");
- }
- // Check if the input is a single character
- static boolean isCharacter(String input) {
- return input.length() == 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement