• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double.parseDouble Troubles  RSS feed

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

I am doing an assignment for my Java class.
It asks to 1) Read input from a user. 2) convert the input to a double number if valid 3) find average.
I am trying to convert the String entered by user into a double number using Double.parseDouble.
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies, forgot to mention that these errors occur:

Grades.java:29: error: ')' expected
double count = Double.parseDouble(String sInput);
^
Grades.java:29: error: illegal start of expression
double count = Double.parseDouble(String sInput);
^
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already declared a variable named count in line 13. You can't declare another variable with the same name again in the same method, which is what you are doing in line 28 - you are declaring a new variable called count there.

Probably you just wanted to assign a value to the existing variable, instead of declaring a new variable. If that's what you wanted to do, then remove the type name double in line 28.
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you. I think I have it sorted now.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use == false or == true. Not only are they poor style, but they are prone to your writing = by mistake in which case you might get two errors for the price of one.
Not b == false but !b
Not b == true but simply b
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using Double.parseDouble unless you are sure the text will be in the correct format for a double.
Find out about the methods of the Scanner class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!