Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:57:52+00:00 2026-06-01T16:57:52+00:00

I am doing an exercise in which the user has to input a signed

  • 0

I am doing an exercise in which the user has to input a signed four digit decimal number such as +3364, -1293, +0007, etc using Java programming language.

As far as I know, Java does not support a primitive type decimal.
My questions are:

  1. How can I enter numbers like above?
  2. How can I provide a +, – sign for the above numbers?

UPDATE

The code below shows a snippet in which it asks the user to enter a valid number (no characters) – the unary + is not working using the code below!! is there a way to fix it.

public int readInt() {
    boolean continueLoop = true;
    int number = 0;
    do {
        try {
            number = input.nextInt();
            continueLoop = false;
        } // end try
        catch (InputMismatchException inputMismatchException) {
            input.nextLine();
            /** discard input so user can try again */
            System.out.printf("Invalid Entry ?: ");
        } // end of catch
    } while (continueLoop); // end of do...while loop

    return number;
} // end of method readInt()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:57:54+00:00Added an answer on June 1, 2026 at 4:57 pm

    Java has 8 primitive (non-Object/non-Reference) types:

    • boolean
    • char
    • byte
    • short
    • int
    • long
    • float
    • double

    If, by “decimal” you mean “base 10 signed integer”, then yes, Java supports that via byte, short, int and long. Which one you use will depend on the input ranges, but int is the most common, from what I’ve seen.

    If, by “decimal” you mean “base 10 signed floating-point number with absolute precision” similar to C#’s Decimal type, then no, Java does not have that.

    If Scanner.nextInt is throwing an error for you, like it is me, then the following should work:

    /* Create a scanner for the system in. */
    Scanner scan = new Scanner(System.in);
    /*
     * Create a regex that looks for numbers formatted like:
     * 
     * A an optional '+' sign followed by 1 or more digits; OR A '-'
     * followed by 1 or mored digits.
     * 
     * If you want to make the '+' sign mandatory, remove the question mark.
     */
    Pattern p = Pattern.compile("(\\+?(\\d+))|(\\-\\d+)");
    
    /* Get the next token from the input. */
    String input = scan.next();
    /* Match the input against the regular expression. */
    Matcher matcher = p.matcher(input);
    /* Does it match the regular expression? */
    if (matcher.matches()) {
        /* Declare an integer. */
    int i = -1;
    /*
     * A regular expression group is defined as follows:
     * 
     * 0 : references the entire regular expression. n, n != 0 :
     * references the specified group, identified by the nth left
     * parenthesis to its matching right parenthesis. In this case there
     * are 3 left parenthesis, so there are 3 more groups besides the 0
     * group:
     * 
     * 1: "(\\+?(\\d+))"; 2: "(\\d+)"; 3: "(\\-\\d+)"
     * 
     * What this next code does is check to see if the positive integer
     * matching capturing group didn't match. If it didn't, then we know
     * that the input matched number 3, which refers to the negative
     * sign, so we parse that group, accordingly.
     */
    if (matcher.group(2) == null) {
        i = Integer.parseInt(matcher.group(3));
    } else {
        /*
         * Otherwise, the positive group matched, and so we parse the
         * second group, which refers to the postive integer, less its
         * '+' sign.
         */
            i = Integer.parseInt(matcher.group(2));
        }
        System.out.println(i);
    } else {
        /* Error handling code here. */
    }
    

    Alternatively, you can do it like this:

        Scanner scan = new Scanner(System.in);
        String input = scan.next();
        if (input.charAt(0) == '+') {
            input = input.substring(1);
        }
        int i = Integer.parseInt(input);
        System.out.println(i);
    

    Basically just remove the ‘+’ sign if there’s one and then parse it. If you’re going to be doing programming, learning regular expressions is very useful, which is why I gave you that, instead. But if this is homework and you’re worried that the teacher will get suspicious if you use something that is beyond the scope of the course, then by all means do not use the regular expression approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing this small exercise. declare @No decimal(38,5); set @No=12345678910111213.14151; select @No*1000/1000,@No/1000*1000,@No; Results
I was wondering if enumeration is commonly used with user input. I'm doing an
I am doing a book exercise (not homework, since i'm self-learning) in which I'm
I'm doing K&R Exercise 5-4 (p107). Write the function strend(s,t) , which returns 1
I'm doing K&R's Exercise 1-10 Write a program to copy its input to its
I was doing an exercise on F# Wiki Book on List (scroll to the
Today I was doing the thread ring exercise from the Programming Erlang book and
Hey all. I'm doing a linked list exercise that involves dynamic memory allocation, pointers,
Okay, so I'm doing a little blowfish implementation in PHP as a programming exercise
I'm doing a book exercise that says to write a program that generates psuedorandom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.