aspose file tools*
The moose likes Beginning Java and the fly likes Double.parseDouble Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Double.parseDouble Help" Watch "Double.parseDouble Help" New topic
Author

Double.parseDouble Help

Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
I'm trying to pass Double.parseDouble in a parameter list.
The compiler shows a ( error. If I correct that error, I
then get 27 new errors where it doesn't like any of my code.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

which line has the error?

[edit]
Double.parseDouble is a METHOD. the correct syntax to call it is

Double.parseDouble(someString)

it doesn't look like you are enclosing what you want to pass to it in parens - they are required.

if that then gives you 27 other errors, then you have other errors in your code that also need to be fixed. the missing parens just cause enough difficulties for the parser that it gives up before finding the other problems.
[ April 09, 2008: Message edited by: Fred Rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
The error codes:

C:\Documents and Settings\Administrator\Desktop\CSIS123\Projects\Nineth Assignment\CheckingAccountsTest.java:43: ')' expected
CheckingAccount currentAccount = new CheckingAccount(myFields[indexForAccountId], myFields[indexForFirstName], myFields[indexForLastName], Double.parseDouble myFields[indexForBalance]);
^
C:\Documents and Settings\Administrator\Desktop\CSIS123\Projects\Nineth Assignment\CheckingAccountsTest.java:43: illegal start of expression
CheckingAccount currentAccount = new CheckingAccount(myFields[indexForAccountId], myFields[indexForFirstName], myFields[indexForLastName], Double.parseDouble myFields[indexForBalance]);
^
C:\Documents and Settings\Administrator\Desktop\CSIS123\Projects\Nineth Assignment\CheckingAccountsTest.java:43: ';' expected
CheckingAccount currentAccount = new CheckingAccount(myFields[indexForAccountId], myFields[indexForFirstName], myFields[indexForLastName], Double.parseDouble myFields[indexForBalance]);
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Double.parseDouble is a method. you need parens. You need something like this:

in both places. Is myFields a String array?

if you then get other problems, we can work on those, too.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In other words, the syntax was correct in yesterday's post. People might find it easier to help if you continue discussions under the original topic, rather than starting new ones.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
Sorry Marc, please forgive my newness, I was just trying to use a more descriptive Topic Line.

I guess I'm going through this class to quick and everything is going mush brain.

1. What is the correct way to pass the parameters.

Is it CheckingAccount(myFields[indexForAccountId]

Or is it CheckingAccount(String myFields[indexForAccountId]

Or is it something else I'm missing

CheckingAccount is a class of course
myFields is a String array
indexForAccountId is an int

Then when I get through that,
what do I do with the Double.parseDouble

Is the myFields[indexForBalance] passed like the others?
or is it the parameter for the Double.parseDouble?

I'm so confused. I've crammed too many chapters into too little time
with too much coffee and not enough sleep

I get it down to only showing 2 or three errors, then when I do what the compiler suggest
I suddenly get as many as 30, all in the same two lines of code.

BTW, the second line with errors reads the same as this one just a different class. Any changes I make to this I make to it too.

Thanks again
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When you declare a method or constructor, you specify the parameter type along with a variable name that will represent that parameter inside the method or constructor body. For example, if CheckingAccount is a class, then you might declare a constructor that takes a double as a parameter...

When you call a method or constructor, you provide the appropriate type parameter by placing it inside the parentheses. For example, a double literal...

...new CheckingAccount(505.37);

If you have a String representation of the double value, you can use Double.parseDouble(myDoubleAsString) to get a double...

...new CheckingAccount(Double.parseDouble(myDoubleAsString));

In your example, you have an array called myFields that holds Strings. The String referenced by myFields[indexForBalance] is presumably one that can be parsed to a double. So you could pass this as...

...new CheckingAccount(Double.parseDouble(myFields[indexForBalance]));

Here, indexForBalance is an int that specifies which String element in the myFields array represents the balance. You get a double from this String by passing it to parseDouble, then you pass that double to the CheckingAccount constructor.
[ April 09, 2008: Message edited by: marc weber ]
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
I'm Sooooooo confused.

Let me talk myself through this again:

the pseudo code for this line is:

INSTANTIATE a local variable named currentAccount of class CheckingAccount
passing the following arguments: myFields[indexForAccountId],
myFields[indexForFirstName], myFields[indexForLastName],
Double.parseDouble(myFields[indexForBalance]


CheckingAccount compiles OK
myFields is a String array and is assigned to:
myFile.getCsvRecordFieldArray()
all the index parameters are static final int fields of CheckingAccountTest
(the file I'm working in)

the code of the moment is:




my compiler error is:



The little error arrow in my build window didn't paste here, but it points to "new"

This is in line 43 of my code. The same "routine" repeats in line 49 except with CheckingAccountPlus

All my error codes at this point are in those two lines.

I've tried putting the parameters in (args[])
String(args[])
(String(args[])

All turn me to over 100 errors and the first 6 or 7 are always in these two lines.

I'm working with about 300 lines of code, and think my problem lies in these two lines.

I even tried commenting them out, but that leaves a ton of things not found errors.

All the other files in the assignment compile OK, just need to get this one going so I can start debugging and find a few more hundred mistakes to keep me busy.

Thanks for your help, I KNOW I'm missing something basic and simple, but it sure is aluding me this week
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Richard Chambers:
...cannot find symbol
symbol : constructor CheckingAccount(java.lang.String,java.lang.String,java.lang.String,double)
...

How is your CheckingAccount constructor (or constructors) declared? It looks like you don't have one that takes 3 Strings and a double.
f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
could you post your complete source? it's difficult to see what's going on from what you have given.
[ April 09, 2008: Message edited by: f. nikita thomas ]

Imagination is more important than knowledge "Albert Einstein"
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
I'm sorry if I'm asking stupid or redundant questions.
My instructor handed us a 1500 page text book, our assignments
and said figure it out. A lot to absorb, and not being a programmer,
I have trouble figuring out what I need to learn, etc.

I'm looking at the constructor call

CheckingAccount is a class
I'm creating a new instance of that class
and naming it a local variable currentAccount

CheckingAccount currentAccount = new CheckingAccount

my questions, are if currentAccount is a local variable,
because it's being declared inside an IF statement,
doesn't that make it an instance variable?

if I declare it before the IF, that would make it local, but when I look at what it's doing, I only want its values inside the IF ELSE statement.

CheckingAccount is a superclass with CheckingAccountPlus as the sub
if that matters (I don't think it does)

Also, as I'm looking through the 11 pages of pseudocode the instructor provided, I noticed that the class CheckingAccount has a method checkingAccount. In his pseudocode though he named the method CheckingAccount. I changed to coding standards when I wrote the code, but wondering if there might be something there I'm missing.

I'm beginning to wish I'd just took a self study at your own pace course now instead of going to the local college. I know I'll have to anyway
if I want to "actually" learn functional java

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Richard Chambers:
...CheckingAccount currentAccount = new CheckingAccount

my questions, are if currentAccount is a local variable,
because it's being declared inside an IF statement,
doesn't that make it an instance variable?

if I declare it before the IF, that would make it local, but when I look at what it's doing, I only want its values inside the IF ELSE statement...

No. Variables are limited to the scope (braces) in which they are declared, and they have no meaning outside of that scope. So if you declare a variable inside an "if" statement, then it is local to that statement and has no meaning elsewhere. To be an "instance variable," it would need to be declared immediately inside the class, so that it can be used anywhere in the class.

For example, in the following code, "myLocalVariable" only has meaning inside the body of "method," but "myInstanceVariable" can be used anywhere inside MyClass...

Originally posted by Richard Chambers:
...CheckingAccount is a superclass with CheckingAccountPlus as the sub
if that matters (I don't think it does)...

What matters is the type of object you create with "new." If you use new CheckingAccountPlus() then your object will be an instance of CheckingAccountPlus and will also be an instance of CheckingAccount. But if you use new CheckingAccount then your object will just be an instance of CheckingAccount, and will not behave as a CheckingAccountPlus.
Originally posted by Richard Chambers:
...as I'm looking through the 11 pages of pseudocode the instructor provided, I noticed that the class CheckingAccount has a method checkingAccount. In his pseudocode though he named the method CheckingAccount...

Java is case sensitive, so "checkingAccount" is different than "CheckingAccount." It sounds like this is supposed to be a constructor (not a method) for the class CheckingAccount. (In addition to sharing the same name as the class, constructors do not have return types.)

I think this is the reason for your problem above. Can you post the complete code for this constructor/method?
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40


The psueudocode has checkingAccount as CheckingAccount

I'm just digging deeper into my confusion.
I think I'll re-read the last few chapters
and see if anything gets clearer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Originally posted by Richard Chambers:
The psueudocode has checkingAccount as CheckingAccount.
Yes, it's the name of a class; it customarily begins with a capital letter.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

note that that is NOT a constructor. Constructors do not have ANY return type. What you posted has a return type of 'void'. What you have is an ordinary method called checkingAccount.
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
OK, now I'll backtrack

the Pseudocode for it is:



I've coded it as:



Like I said earlier, I'm re-reading a few chapters this afternoon,
I'm cramming a ton of knowledge in a small mind in a short time.
(sounds like a country song, maybe I would do better writing music)

So I'll review Constructors, and method declarations etc, and see what I figure out.

Thanks for the help
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Look very carefully at what the psuedocode says:
This public method has no return value

now, look at your declaration:

you're very close. but...

when you put in that word "void", you are giving the method a return type - a type that happens to be 'void'. to become a constructor, you need to take that word out:
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

...and match the exact class name, with an uppercase 'C'.

Once you do this, I think the issues you thought were related to "parseDouble" will disappear, because that wasn't actually the problem. The real problem was that you were trying to call a constructor that wasn't defined properly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Originally posted by Richard Chambers:
no return value
Damn! I ought to have noticed that myself

Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
OK I finally got that part figured out.

I wasn't calling my super class correctly in CheckingAccountPlus file

I wasn't declaring the constructor CheckingAccount right

changed the spelling back from checkingAccount to CheckingAccount

and that worked.


Now I'm getting new errors in my overload methods.

As I'm taking 15 hours at school and working 40 hours a week,
I think I'll read the section on overloading methods again and go to bed.
I'll probably be back tommorrow with more dense questions.

Thanks for all the help everyone
Especially Marc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Double.parseDouble Help