• 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

FiizBuzz implementation: count the number of occurrences of a word and print to a file

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have written a FizzBuzz implementation and ultimately printed to a file the expected conversion for each case:

1
2
Fizz
4
Buzz
6
7
8
9
Buzz
11
12
Lucky
14
FizzBuzz
16
17
18
19
Buzz

The above was written to a file as per the below method:



I have been trying to count the occurrences of each string/word, so that the output saved into a file will look like this:

fizz: 4 buzz: 3 fizzbuzz: 1 lucky: 2 integer: 10

I am indeed stuck and cannot be returned with the correct number of times each string appears (I am not implementing the number of times an integers shows yet). Here is my implementation:



Can you please help? How can I include the check against the integers, instead?

Cheers,
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why haven't you shown anything for 6, 9, and 18?
Why are you writing to a file and then counting from the text file? Why not do the counting directly from the program's output?
Have a look at this discussion about counting and the Java™ Tutorials about Maps.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your write on line 14 needs to be outside of the while loop. Then it will need to have its own loop to iterate through the entries and write them.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an aside, but quite often this algorithm is written this way
I usually write it this way. Does it make any difference?

 
Ranch Hand
Posts: 52
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 14, s.next() is called again . This should not be called multiple times inside the loop.
 
Geane Norm
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:This is an aside, but quite often this algorithm is written this way
I usually write it this way. Does it make any difference?



Hi,
this helped a lot, thank you. I have now changed it to this:



Although it now returns the expected results (good first step), as follows:

{11=1, Fizz=4, 14=1, 16=1, 17=1, 19=1, Buzz=3, Lucky=2, FizzBuzz=1, 1=1, 2=1, 4=1, 7=1, 8=1}

I would like to look like the following:

Fizz: 4 Buzz: 3 fizzbuzz: 1 lucky: 2 integer: 10

I also noticed that my implementation was incorrect as multiple of 3 should return : "Fizz", multiple of 5: "Buzz", multiples of 15: "FizzBuzz" and numbers including "3" should output 3. Here is hopefully the correct implementation:



I am struggling to see how to count all integer occurrences such that I get integer: 10

Thanks!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using Strings when a fizz‑buzz game is usually run by numbers?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:This is an aside, but quite often this algorithm is written this way
I usually write it this way. Does it make any difference?


With java 8, this can be shortened, at the cost of a somewhat strange looking syntax:
  (see the API of Map what this all means!)

Geane wrote:I would like to look like the following:

Fizz: 4 Buzz: 3 fizzbuzz: 1 lucky: 2 integer: 10


In that case, adjust the convert() method such that it returns the String "Integer" when the result is not Fuzz or one of the other Strings.
The map can then be gotten by

In fact, by using java 8 streams this whole reading of a file and creating the Map can be done even shorter, but that for some other time.


Edit: oops, just notices that 'convert' takes an integer as input. Well, make an overloaded version of 'convert' that takes a String as input.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic