• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double.parseDouble Troubles

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
^
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Thank you. I think I have it sorted now.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic