• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

re: try/catch block

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this exercise. It's working but I am confused as to how to call the method "purpleGold" from the main when using a Try/Catch block. I have not used try/catch blocks that much. thanks.

Code follows:

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't the correct way to open a Scanner, and you shouldn't explicitly close it. The correct way to open it is with try with resources, and that ensures the Scanner is certain to be closed correctly, unless the JVM stops altogether. Don&apso;t use a file input stream to read a text file.Apart from making everything static, which one shouldn't do, use hasNext() rather than hasNextLine() because the file is likely to finish with an empty line.
You haven't said what the relationship is between the text file and the game, nor how you are going to use the line in the game. You would want something like this:-
 
marc decareano
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Code is working:

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nonononono! You now have two nested loops.
 
marc decareano
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Nonononono! You now have two nested loops.




Ok. I was wandering about having two while loops. corrected code for main class:



 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks better, but you should have a method reading the file and one playing the game. I would make the purpleGold() method take String as its parameter.
But purpleGold() is a bad name for a method; it doesn't tell you what it does. It sound more like the name of a class; PurpleGoldGame might be better. That allows you to get away from a static context into an object context:Note that the runtime type of the List returned by Collectors.toList() is a random access type of List, so you can use get(i) without worrying about performance problems.

Please check carefully in case I have given you the wrong names for any of the methods. Links to some of those methods: 1 2 3 4 5 and to the Java™ Tutorials about input/output.

[edit]Maybe I should have written two methods myself Challenge: break my code into methods to read the file and to play the game.
 
marc decareano
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. let's make it two methods but I need some help since they are so many moving parts:

I edit the code just to see if I have the right idea. See below:
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

marc decareano wrote:. . . they are so many moving parts:

That is why you want multiple methods, so you have the fewest moving parts possible in each method.

. . . to see if I have the right idea.

Sorry. No cigar. I think, sorry, that you are going to have to start again. Please read this link. Then tell us what your design is, and also what the format of the text file is.
 
marc decareano
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

marc decareano wrote:. . . they are so many moving parts:

That is why you want multiple methods, so you have the fewest moving parts possible in each method.

. . . to see if I have the right idea.

Sorry. No cigar. I think, sorry, that you are going to have to start again. Please read this link. Then tell us what your design is, and also what the format of the text file is.




ok. will do. thanks for feedback. "Stop coding" is a very good document. I will try to learn from it and apply it to this problem.
this is the exercise:

Write a static method purpleGold that accepts as a parameter a Scanner that reads its input from a file containing a series of names followed by integers. The names alternate between purple team member names and gold team member names. Your method should compute the absolute difference between the sum of the purple teams' integers and the sum of the gold teams' integers. The input could end with either a purple or gold team member; you may not assume that it contains an even number of names. If the input file tas.txt contains the following text,

Mark 3 Laura 7 Ashley 14 Monica 13 Dan 4 Alexi 12 Susan 6 Michael 0

Method to play game in english:

will iterate through names and numbers as pairs (ie: Mark 3, Laura 7, etc)
Mark, Ashley, Dan and Susan will be purple team
Laura, Monica, Alexi and Michael will be gold team
as I cycle through the names will keep a tab of team members count as well as their sums in variables
when iteration finishes I will print to the console the results (count and sums)

Re: method to read a file. what do you mean by "what the format of the text file is"




 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark, Ashley, Dan and Susan will be purple team  

Do you mean, alternate people go into each team?

what do you mean by "what the format of the text file is"

I meant, what sort of information does it contain?
 
marc decareano
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mark, Ashley, Dan and Susan will be purple team  



Do you mean, alternate people go into each team?



Yes


what do you mean by "what the format of the text file is"

I meant, what sort of information does it contain?



it contains: Mark 3 Laura 7 Ashley 14 Monica 13 Dan 4 Alexi 12 Susan 6 Michael 0

furthermore, I will calculate the modulo of the count for each iteration to differentiate one team from another. Even results will be purple team and odd results will be gold team.
when the while loops has.Next() returns false I will then calculate the teams members count, the totals for each team and the difference.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. That is a strange way to calculate increments modulo 2: you only need to increment the index by 1 and there is no need to halve anything. That also means, yes, the names alternate.
 
marc decareano
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Thank you. That is a strange way to calculate increments modulo 2: you only need to increment the index by 1 and there is no need to halve anything. That also means, yes, the names alternate.




Learned a few things in this simple exercise. Cheers.
reply
    Bookmark Topic Watch Topic
  • New Topic