Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Scanners, Strings and doubles... Help!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 72441
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 72441
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
You got style baby! More than this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic