CodeGym
Promotion
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
CodeGym/Help with Java Tasks/A little Hint is appreciated. Please Help
Satya Vath
Level 31
Vijayawada
  • 14.04.2019
  • 1118views
  • 2comments

A little Hint is appreciated. Please Help

Question about the task Different methods for different types
Java Core,  Level 5,  Lesson 12
Resolved

1. Read data from the console until the word "exit" is entered.
2. For each value (except "exit"), call the print method. If the value:
2.1. contains a period ("."), then call the print method for Double;
2.2. is greater than zero, but less than 128, then call the print method for short;
2.3. less than or equal to zero or greater than or equal to 128, then call the print method for Integer;
2.4. otherwise, call the print method for String.

Requirements:
  • The program must read data from the keyboard.
  • The program should stop reading data from the keyboard after "exit" is entered.
  • If the entered string contains a period (".") and can be correctly converted to a Double, the print(Double value) method should be called.
  • If the entered string can be correctly converted to a short and the resulting number is greater than 0, but less than 128, the print(short value) method should be called.
  • If the entered string can be correctly converted to an Integer and the resulting number is less than or equal to 0 or greater than or equal to 128, the print(Integer value) method should be called.
  • In all other cases, the print(String value) method should be called.
package com.codegym.task.task15.task1519; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.net.CacheRequest; /* Different methods for different types */ public class Solution { public static void main(String[] args) throws IOException { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input; while (true) { input = reader.readLine().trim().replaceAll("\\s+", ""); if (input.equals("exit")) break; if (input.contains(".") && isDouble(input)) { try { Double doubleInput = Double.parseDouble(input); print(doubleInput); } catch (NumberFormatException ex) { ex.printStackTrace(); } } else if (isShort(input)) { try { Short shortInput = Short.parseShort(input); if (shortInput > 0 && shortInput < 128) print(shortInput); } catch (NumberFormatException ex) { ex.printStackTrace(); } } else if (isInteger(input)) { try { Integer integerInput = Integer.parseInt(input); if (integerInput <= 0 || integerInput >= 128) print(integerInput); } catch (NumberFormatException ex) { ex.printStackTrace(); } } else print(input); } } // check if input is a Double public static boolean isDouble(String input) { try { Double doubleInput = Double.parseDouble(input); } catch (NumberFormatException ex) { return false; } catch (NullPointerException ex) { return false; } return true; } // check if input is a Short public static boolean isShort(String input) { try { Short shorInput = Short.parseShort(input); } catch (NumberFormatException ex) { return false; } catch (NullPointerException ex) { return false; } return true; } //check if input is an Integer public static boolean isInteger(String input) { try { Integer integerInput = Integer.parseInt(input); } catch (NumberFormatException ex) { return false; } catch (NullPointerException ex) { return false; } return true; } public static void print(Double value) { System.out.println("This is a Double. Value: " + value); } public static void print(String value) { System.out.println("This is a String. Value: " + value); } public static void print(short value) { System.out.println("This is a short. Value: " + value); } public static void print(Integer value) { System.out.println("This is an Integer. Value: " + value); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Oleh
Level 32 , Kyiv, Ukraine
14 April 2019, 11:21
Problem is in the short check. For example: isShort(1234) - returns true; but 1234 doesn't match the 1-127 range so if statement ends without isInteger check.
0
Fa Yu
Level 16 , Tashkent, Uzbekistan
8 July 2019, 06:44
could you please help with my code - it works perfectly as per all conditions, but won't verified:
package com.codegym.task.task15.task1519;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

/*
Different methods for different types

*/

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true) {
            String input = reader.readLine();
            try {
                if (input.equals("exit")) break;

                else if ((input.contains(".")) && (Double.parseDouble(input) >= 0 || Double.parseDouble(input) <= 0)) {
                    print(Double.parseDouble(input));
                } else if (Short.parseShort(input) > 0 && Short.parseShort(input) < 128) {
                    print(Short.parseShort(input));
                } else if (Integer.parseInt(input) <= 0 || Integer.parseInt(input) >= 128) {
                    print(Integer.parseInt(input));
                } else throw new Exception();

            } catch (Exception e) {
                print(input);
            }

            //write your code here
        }
    }

    public static void print(Double value) {
        System.out.println("This is a Double. Value: " + value);
    }

    public static void print(String value) {
        System.out.println("This is a String. Value: " + value);
    }

    public static void print(short value) {
        System.out.println("This is a short. Value: " + value);
    }

    public static void print(Integer value) {
        System.out.println("This is an Integer. Value: " + value);
    }
}
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2025 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2025 CodeGym