The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Trimming, parsing, verifying grades entered are valid

I have been trying to trim the and get the length and parse the string into a double from what the user enters. And I think I may have gotten that? Can you tell me if I did this correctly??

The one that I am most concerned with is how do I validate correct grades to be entered? Any valid grades should be anything greater than 0 and grades less than 100.

So far what Ive done is asked the user to enter up to ten grades, I then added in there, if the user presses enter, then they are done entering. Then I would add up the grades and average them up and print it. I haven't added that coding in there yet, as I am still trying to trim, get length and parse the string into a double.

I know, It would make sense to read doubles from the user, but my instructor specifically asked that we read strings then convert to doubles if and only if the user entered a length > 0 of which I already established into my code.

Re: Trimming, parsing, verifying grades entered are valid

For verifying the grades are valid, you have a few options. The grades are entered as strings which poses the possibility for the user to enter invalid data. This is a common problem that sadly many dont tackle and get burned later in production. Anyways, how do we fix this? There are a few options ranging form trivial or complex or "bad practice but it works".

1. (Trivial) You can have a for loop that goes through the string and checks each character to make sure it is either a digit or a dicimal point. But then you have to make sure there is only one decimal point. This can get tricky.
2. (Complex) Use a regular expression. These are very powerful and will tell you exactly what you want to know very quickly. If you can learn these early in your career of programming it will be a great advantage.
3. (Bad Practice) Put a try/catch block around your Double.parseDouble to catch the NumberFormatException and instead of breaking, display a message that it is not valid, subtract the one from your count, and send it back through the loop.
--This could be the most simple, but it is considered bad practice to use a try/catch block in situations like this. Typically you should only use them for cases where your code goes wrong. There are countless debates about this. In my opinion, it is supposed to be used for exceptions...well people are supposed to give you doubles and in the exceptional case that they dont, this can handle it. Take it as you will, maybe ask your teacher about it. Some teachers take away points for "bad practice" as they see it.

Writing code is your job, helping you fix and understand it is mine.

<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!