• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't figure out variable assignments.  RSS feed

 
Curtis Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56570
172
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Grumble, I think I have it now <3
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Curtis Wilson,

If you got the answer for your question, mark the post as resolved.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!