• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Can't figure out variable assignments.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

SO I am having trouble and I feel if I could get past this part I could finish what I am working on. I have the following code which pulls numbers from a file listed in the following order : Employee ID, Wage(Hourly), Hours however I am having trouble assigning variables to this, this would be all I need help with and I would greatly appreciate it.



outbout is:



Please just help me assign variables and I will leave alone I cannot figure out.

PS there are 100 more lines just like that (Different numbers of course). The above code is just me making sure I can read it from the file.
 
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you know how you can read a second line using a loop? Are you aware of the hasNextXXX methods of Scanner? You can use one of them as the continuation condition for a loop.
Does every line of the file contain a int followed by a floating‑point number followed by another int? Did you know you can use a nextXXX method of Scanner to read those data individually?

Did you know things might go horribly wrong if you have an empty line at the end of your file? But you can cross that bridge when you get to it.

Another thing. You must close every Scanner/Reader/Writer/Formatter which does not point to System.in System.out or System.err. Assuming you are using Java7 or Java8 you can use “try with resources” for that. Most probably the best and easiest way to close your Scanner.
 
Curtis Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, I forgot to close the file at the end. I fixed that. So the numbers should be teh following;

int, floating, floating. If I remember correclty they should be int, double, double but I am not remembering how to give them the variable names (id,money,h) I do know that I can use has next, but I am still just trying to figure out my variable assignment. BTW, thanks for the welcome to the community.
 
Campbell Ritchie
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since each nextXXX methood returns an XXX you can very simply assign from that:-
XXX myNumber = myScanner.nextXXX();
 
Curtis Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so say I do the following:



I still get errors as follows when just compiling.




Sorry if my questions are so well stupid. I should know this but I am just having trouble.
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Curtis Wilson wrote:I still get errors as follows when just compiling.



Take a look carefully at the errors you have provided, you will be able to figure out what is the mistake you have done. Concentrate on the error part and you will get it.
 
Curtis Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Grumble, I think I have it now <3
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Curtis Wilson,

If you got the answer for your question, mark the post as resolved.
 
He does not suffer fools gladly. But this tiny ad does:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic