aspose file tools*
The moose likes Beginning Java and the fly likes parse to double required?  variable declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parse to double required?  variable declarations" Watch "parse to double required?  variable declarations" New topic
Author

parse to double required? variable declarations

Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
found a site with some simple problems that I'm working through as a refresher/learning. I keep getting an error and think that:
a. my code is sloppy - do I need to declare variables elsewhere?
b. I'm having problems with the rad calculation as its calculating with int and double which is giving an error?
c. inData * inData seems silly to gets its square. Is there a Math function for square. Couldn't find one.

thanks for any help.

Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
a) It's a little cleaner to declare the variables only when they get some meaning. Say

String inData = stdin.readLine();;
int num = ...;
double rad = ...;

No reason to declare it with nonsense values.

b) Yes, you do need to parse that string. You can't multiply strings by strings.

c) Which would be sillier, typing x*x, or using static method of another class to do the same thing?
[ November 07, 2004: Message edited by: Nick George ]

I've heard it takes forever to grow a woman from the ground
Patrick Haley
Greenhorn

Joined: Sep 15, 2004
Posts: 10
a. Not so much. I could read it.

I might have called the final value holder *area* instead of *rad* because that is what you calculate.

b. Actually, your calculation is being done with a String object (inData) and a double (Math.PI).

Remember, you read the buffer into a String, and then you parse the int with the Integer.parseInt method. Changing inData to num in the line of code above will fix the problem.

c. Math.pow(num, 2) will square it for you, but you would be splitting hairs on this.

Keep coding!


Wannabe SCJP 1.4<br /> <br />It wasn't raining when Noah built the ark.<br /> --Howard Ruff
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Hi Matt,
I want to help you...
This is my answers for you..
a. Yes, you can declare variable anywhere before its is used.
b. I have updated your code ( and I fix some errors) like this..



c. You can use Math.sqrt(double a).

Correct me if I am wrong
hope this helps...

daniel
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
a. my code is sloppy - do I need to declare variables elsewhere?
Many programmers declare local variables on or just before their first use. It makes it easier when you're reading someone else's code.
b. I'm having problems with the rad calculation as its calculating with int and double which is giving an error?
You just have two errors, both in one line - you're missing a ; and your expression should be num*num.
c. inData * inData seems silly to gets its square. Is there a Math function for square. Couldn't find one.
There is Math.pow(a,b) to compute a^b, but Math.pow( inData, 2 ) seems like overkill.


Mike Gershman
SCJP 1.4, SCWCD in process
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Perhaps, I am splitting hairs as Fisher says. However, I also think it's good for a programmer to understand the advantages and disadvantages associated with two different ways of accomplishing the same task. This will allow said programmer to make an informed decision about which way is "best" for the current situation.

With that said, I typically prefer "x * x" over "Math.pow(x, 2)" for several reasons:

1) less typing
2) faster execution (i.e. calling a method has a slight overhead associated with it)

HTH

Layne


Java API Documentation
The Java Tutorial
Matt Fielder
Ranch Hand

Joined: Oct 27, 2004
Posts: 158
Wow that helped. I was doing a lot of those changes but forgot some small ones that made the whole thing complete. So I kept getting an error. So, I understand parsing num to double, but here's the stupid part that I forgot to include.

The assignment says,

Each of these programs calls for reading integer data from the keyboard. (Of course you will have to read in character data and convert it to an int using the wrapper class Integer.) Once you have read in an integer it is OK to do double precision arithmetic with it. Just be sure that your arithmetic expressions don't accidentally call for integer math when you don't want it.


So is it me or does the assignment as for extra steps that aren't necessary? You parse the String num to int as required then parse it again to double for the math? That just doesn't make sense. Why not just bring it in as a double especially since the radius doesn't necessarily have to be an integer in the real world.
[ November 08, 2004: Message edited by: Matt Fielder ]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
The wrapper class Double has a parseDouble(String s) method, so you have no real need to parse to the Integer class, but I think your assignment calls for that unnecessary step. This is probably so that you learn casting.
Patrick Haley
Greenhorn

Joined: Sep 15, 2004
Posts: 10
Try something in a piece of code that does double precision math with an integer.



Then read about conversions and promotions.
Conversions and Promotions
Especially Numeric Promotion.


[ November 08, 2004: Message edited by: Patrick Haley ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parse to double required? variable declarations
 
Similar Threads
Java Help... so frustrated.... again!!
HELP! Why not Compiling ? Easy but.......
Programing challenge
Must be caught or declared? Exceptions
Circle program errors