Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class DataTypeFinder1 {
- private static boolean tryParseInt(String value) {
- try {
- Integer.parseInt(value);
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- private static boolean tryParseDouble(String value) {
- try {
- Double.parseDouble(value);
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- while (!input.equals("END")) {
- if (input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false")) {
- System.out.printf("%s is boolean type%n", input);
- }else if (tryParseInt(input)){
- System.out.printf("%s is integer type%n",input);
- }else if (tryParseDouble(input)){
- System.out.printf("%s is floating point type%n",input);
- } else if (input.length() == 1) {
- System.out.printf("%s is character type%n", input);
- }else{
- System.out.printf("%s is string type%n",input);
- }
- input = scanner.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement