Jump to content

Having problems with parseDouble method

- - - - -

  • Please log in to reply
6 replies to this topic

#1
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
So here's a program that passes command-line arguments. I've never used the parseDouble method before, so I don't know what I'm doing wrong. I'm supposed to convert the elements in the args array to double and add their values. Here's the code:

public class Double
{
    public static void main(String[] args)
    {
        double sum = 0;
        double d;

        for (String b : args)
        {
            d = Double.parseDouble(args);
            sum += d;
        }

        System.out.println("Sum of arguments: " + sum);
    }
}
I keep getting this error:

symbol  : method parseDouble(java.lang.String[])
location: class Double
            d = Double.parseDouble(args);
                      ^
1 error

Tool completed with exit code 1


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Your variable args isn't just a String. It is a String[]. Or an array of Strings.

The paramater for Double.parseDouble is a String, not a String[].

Simply, you've mistakenly put args inside of the parseDouble method instead of 'b.'

#3
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts

lethalwire said:

Your variable args isn't just a String. It is a String[]. Or an array of Strings.

The paramater for Double.parseDouble is a String, not a String[].

Simply, you've mistakenly put args inside of the parseDouble method instead of 'b.'
I tried that, it still doesn't work. The error is "cannot find symbol." Isn't Double an implicit class? Or do I have to create it myself?

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Your code compiles fine in eclipse with you replace args with b.

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
public class Double
:|

You cannot name your class Double. Double is a class in java.lang. You must name your class something else. The reason you're getting "cannot find symbol" is because the Java Compiler thinks your reference to Double meant the class you were writing now, which does not have a parseDouble method.

@lethalwire: Did you use your own Main test class?
Wow I changed my sig!

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You can always use java.lang.Double.parseDouble.
But I must say it's kinda confusing to name your class Double.

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

ZekeDragon said:

public class Double
:|

You cannot name your class Double. Double is a class in java.lang. You must name your class something else. The reason you're getting "cannot find symbol" is because the Java Compiler thinks your reference to Double meant the class you were writing now, which does not have a parseDouble method.

@lethalwire: Did you use your own Main test class?

Yes I did, doh! If I would have used Double I would have seen how obvious that problem was. :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users