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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:01:35+00:00 2026-06-15T09:01:35+00:00

Im writing a program for homework where I need to validate user input and

  • 0

Im writing a program for homework where I need to validate user input and then create an object and add it to an array list. i have included just what i think is the relevant code, but im a beginner for sure so let me know if there is something else you need to see.

I have the user enter a string, then check to see if its a double. if its not a double, i throw an exception that i created

     try{
        price = Double.parseDouble(strPrice);
     }
     catch(NumberFormatException nfe){
        CDException cde = new CDException();
        cde.setMessage("Price must be a number,\nCannot create CD");
        throw cde;
     }

after making sure that it is a number, in my other class i check to make sure it is in the range that i want. (non negative in this case) then create an object with the value

public void setPrice(double newPrice)throws Exception{
    if(newPrice >= 0){
        this.price = newPrice;
    }
    else{
        CDException cde = new CDException();
        cde.setMessage("CD Price cannot be negative,\nCannot create CD");
        throw cde;
    }
}

so my question is…

is there a way to do this in one step, check both that the user entered a number and that the number is non negative. one other thing is that if the input is blank, that is the instruction to end the input loop.

  • 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-15T09:01:37+00:00Added an answer on June 15, 2026 at 9:01 am

    Well, you could change your code to read:

    try {
        price = Double.parseDouble(strPrice);
        if (price < 0) {
            throw new CDException("Can't be negative");
        }
    } catch (NumberFormatException ex) {
         ...
    }
    

    But the question is whether you’d really like to do that. From a design perspective, it might make more sense to do the “negative” check inside setPrice, rather than doing it as part of the parsing logic.

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

Sidebar

Related Questions

I am writing a program, for homework, that will add 2 8-bit binary numbers.
Im writing a program that should read input via stdin, so I have the
I am writing a linked list for my homework assignment and I need to
I have this insane homework where I have to create an expression to validate
Major bash-noob here. Writing my second program again. For my homework, I need to
I'm writing a program for homework, but I have stumbled upon a very hard
I have a homework assignment which requires input from the user and stores it
I'm writing a program about image-processing. I need to store an int square matrix
I'm writing a program for some homework that's going to entail some forking, but
Writing a program in which I need to split strings from a struct linked

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.