Two Laptop Bag*
The moose likes Beginning Java and the fly likes Scanners, Strings and doubles... Help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanners, Strings and doubles... Help!" Watch "Scanners, Strings and doubles... Help!" New topic
Author

Scanners, Strings and doubles... Help!

Cj Turner
Greenhorn

Joined: Mar 26, 2010
Posts: 2
Hi

So new to the whole Java shindig thats going on over here, I am 1 month down in my 4 year software engineering degree, And I am already stumped on how to pull doubles from a string using a Scanner. The code is a mess, and I'm sure it doesn't make much scene but working on one problem at a time,



Any point in the right direction would be amazing!

Cheers!

Cj
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Welcome to the Ranch
Try something like thisNice and short, does one thing, shows you the Scanner working, and allows you to alter your code without messing up your bigger class.
Cj Turner
Greenhorn

Joined: Mar 26, 2010
Posts: 2
Thanks for the speedy reply, I since posting this have worked out my initial problem, and have moved onto others... This is my code as it stands,




Now the issue with this is, where I have

The second if statement returns the current value, while the first always returns 0,

I'm sure the tempNum is assigned a value before it reaches the if, so it should have something greater than 0 to store there?
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
Cj Turner wrote:Thanks for the speedy reply, I since posting this have worked out my initial problem, and have moved onto others... This is my code as it stands,




Now the issue with this is, where I have

The second if statement returns the current value, while the first always returns 0,

I'm sure the tempNum is assigned a value before it reaches the if, so it should have something greater than 0 to store there?



hi,

I think the only issue is with if block.

Try below code instead of your if blocks:



Hope you will get proper answer.

Thanks
James
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Where are you initialising tempNum? Why are you using two Scanners? You only need one. Don't use nextLine() if you want a number, use nextXXX(). Don't pass the result from one Scanner to another, which is unnecessary.
You can enter several doubles and read them in a loop, like this
Enter your numbers: 1.23 2.34 3.45
Enter more numbers, or "close" to finish: 4.56 5.67 close
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Scanners, Strings and doubles... Help!