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 7491307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:57:44+00:00 2026-05-29T15:57:44+00:00

I am working on a homework assignment, and I am going a little "above

  • 0

I am working on a homework assignment, and I am going a little "above and beyond" what is called for by the assignment. I am getting a run-time error in my code, and can not for the life of me figure out what it is that I have done wrong.

Here is the assignment:

Write a program that displays a simulated paycheck. The program should ask the user to enter the date, the payee’s name, and the amount of the check. It should then display a simulated check with the dollar amount spelled out.

Here is my code:

CheckWriter:

/* CheckWriter.java */

// Imported Dependencies
import java.util.InputMismatchException;
import java.util.Scanner;

public class CheckWriter {
    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);

        // Try to get the name
        String name = "";
        NameValidator validateName = new NameValidator();
        while (validateName.validate(name) == false) {
            System.out.println("Enter the name: ");
            name = keyboard.nextLine();

            if (validateName.validate(name) == false) {
                System.out.println("Not a valid name.");
            }
        }

        // Get the date
        String date = "";
        DateValidator validateDate = new DateValidator();
        while (!validateDate.validate(date)) {
            System.out.println("Enter the date (dd/mm/yyyy): ");
            date = keyboard.nextLine();

            if (!validateDate.validate(date)) {
                System.out.println("Not a valid date.");
            }
        }

        // Try to get the amount of the check
        String checkAmount = "";
        CurrencyValidator validateCurrency = new CurrencyValidator();
        while (!validateCurrency.validate(checkAmount)) {
            
            System.out.print("Enter the Check Amount (XX.XX):  $");
            checkAmount = keyboard.nextLine();
            
            if (!validateCurrency.validate(checkAmount)) {
                System.out.println("Not a valid check amount.");
            }
        }

        String checkWords = checkToWords(checkAmount);  // ERROR! (48)

        System.out
        .println("------------------------------------------------------\n"
                + "Date: "
                + date
                + "\n"
                + "Pay to the Order of: "
                + name
                + "         $"
                + checkAmount
                + "\n"
                + checkWords
                + "\n"
                + "------------------------------------------------------\n");
    }

    private static String checkToWords(String checkAmount) {
        
        /**
         * Here I will use the string.split() method to separate out
         * the integer and decimal portions of the checkAmount.
         */
        
        String delimiter = "\\.\\$";
        /* Remove any commas from checkAmount */
        checkAmount.replace(",", "");
        /* Split the checkAmount string into an array */
        String[] splitAmount = checkAmount.split(delimiter);
        
        /* Convert the integer portion of checkAmount to words */
        NumberToWords intToWord = new NumberToWords();
        long intPortion = Long.parseLong(splitAmount[0]);       // ERROR! (84)
        intToWord.convert(intPortion);
        String intAmount = intToWord.getString() + " dollars";
        
        /* Convert the decimal portion of checkAmount to words */
        String decAmount = "";
        long decPortion = Long.parseLong(splitAmount[1]);
        if (decPortion != 0) {
            NumberToWords decToWord = new NumberToWords();
            decToWord.convert(Long.parseLong(splitAmount[1]));
            decAmount = " and " + decToWord.getString() + " cents.";
            
        }
        
        return (intAmount + decAmount);
    }
}

Note that I am using external class files to handle validation of the name, date, currency, and conversion from numbers to words. These class files all work as intended.

The error I am getting is:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Long.parseLong(Unknown Source)

at java.lang.Long.parseLong(Unknown Source)

at CheckWriter.checkToWords(CheckWriter.java:82)

at CheckWriter.main(CheckWriter.java:46)

I have commented the lines in my code that are causing the errors that I am experiencing.

Could someone please assist me in figuring where my code is going wrong? I can include the other class files if you feel that it would be needed.

EDIT: When I run the code, it asks for the name and date. Before asking for the check amount is when it throws the error.

EDIT 2: A huge thank you to cotton.m! Thanks to his advice, I have changed the while statements to look like this:

while(!validateDate.validate(date) && date == "")

This has now fixed my issue. It would appear that when validating data with a regex expression, an empty string will return true.

  • 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-05-29T15:57:45+00:00Added an answer on May 29, 2026 at 3:57 pm

    The String you are trying to parse in an empty length string.

    My suggestion would be to

    1) Check the value of checkAmount at the start of checkToWords – if it is blank there’s your problem

    2) Don’t do that split. Just replace the $ like you did the , (I think this is your real problem)

    Also you are going to have another issue in that 10000.00 is not a long. I see you are splitting out the . but is that really what you want?

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

Sidebar

Related Questions

I'm working on a homework assignment to modify code given by my professor using
I'm working on a homework assignment and I ran into a little snag. I'm
I am working on a homework assignment on implementing interfaces, and am a little
I'm working on a homework assignment in which I'm required to use char arrays
I'm working on a homework assignment (a project), for which one criterion is that
I am working on this homework assignment and I am stuck on what I
I am working on a homework assignment looking at inheritance in java. I am
For a homework assignment, I'm working with the following. It's an assigned class structure,
I'm working on a small homework assignment and I'm supposed to make a food
I am working on a homework assignment and I'm trying to figure out a

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.