• Post Reply Bookmark Topic Watch Topic
  • New Topic

NumberFormatException and Array Troubles  RSS feed

 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

So I have to convert strings to double numbers and there can be no exception.
The strings that aren't numbers or do not fit into a set criteria have to be discarded.

When I try to write this I get an exception when a non-numeric is entered and the code stops.

What can I do?

Also, am I finding the average of the array correctly?

Thank You

 
Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is this a copy of a compiling program? I tried it and found some errors.

* Is it ValidArray or sValidArray? I changed them to the same variable name (ValidArray).
* The average variable is declared twice, line 13 and 65.
* On line 45 you are assigning a String, sInput, into ValidArray instead of UserInput.

You say that you get an exception when a non-numeric is entered and the code stops. This code would only try to convert the last user input, the empty string. I believe you want to be parsing and capturing each line or looping over sArray.

If you want to keep going after getting an exception, you need to catch and handle it.

--
Jacob


 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I changed the code. Now I am having problems with counting the elements in the ValidArray, and finding the average of the elements in the array.

I am getting this as output: The number of valid grades entered is 10
[D@7ea987ac[D@7ea987acAverage: 0.0


How can I fix this?

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Anawalt wrote: * Is it ValidArray or sValidArray? I changed them to the same variable name (ValidArray).


Note that by convention, variable names (and also method names) in Java start with a lowercase letter. So, validArray might be a better name.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kara Wilimas wrote:I am getting this as output: The number of valid grades entered is 10
[D@7ea987ac[D@7ea987acAverage: 0.0

How can I fix this?

There was a recent discussion along the same lines. Maybe you can find your answer there.
http://www.coderanch.com/t/642179/java/java/Strange-output

edit: Or not. Look closely at the loop that follows the comment //for printing array backwards.
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I on the right track if I change it to this?
 
Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kara Wilimas wrote:
Now I am having problems with counting the elements in the ValidArray, and finding the average of the elements in the array.

I am getting this as output: The number of valid grades entered is 10
[D@7ea987ac[D@7ea987acAverage: 0.0


You print out that the number of valid grades is vArrayLen, but that doesn't appear to be the number of valid grades. It looks stuck as ValidArraySize (10).

[D@7ea987ac is the object instance ValidArray and it's hexed hash code printed twice. It looks like you are making progress by switching to ValidArray[i]. Is printing the array backwards a requirement?

You are on the right track for summing and calculating the average, if ValidArray were to contain ValidArraySize valid grades, instead of just the last line read from reader into initialInput. (The one where initialInput.length() < 1.)

I would consider reading the numbers while I read the lines like you did initially in your Double.parseDouble Troubles post (but don't forget try/catch) and skipping sArray altogether, unless it's a requirement.

Do you have to use fixed size arrays, or can you use Collections?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!