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

Combing Multiple String With No StringBuilder?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am working on a variation of the "FizzBuzz" code where I have to replace all the multiples of 3 with the word "coza" all the multiples of 5 with the word "loza" and so on. I also have to output it to a message box. I was able to get my project to work through the normal output window but I am having trouble getting it through the message box. The only way I can think to do this is if I combine multiple strings into one string. I am not aloud to use StringBuilder so the only other method I know is using "+" symbols to combine separate strings in the output window. But when I try to do that I get some errors in my code. They say that all my variables can't be resolved to variables. How can I fix this? Here is my code

 
author & internet detective
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the exact compiler error? It compiles on my machine.

Also, why do you have five nested loops? That feels wrong.
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you should indent your code better.
This fragment doesn't look well:
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is in here, but basically, put new line characters ("\n") between the strings.
 
Saloon Keeper
Posts: 10927
87
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
Why 7 nested loops? For a 'result' of 10, that would mean the inner logic would be executed 10 million times.

Your variables: A, B, C, ... are all defined with a block level scope and would therefore not be accessible outside of the block. Also, don't use leading upper-case letters for variable names.

Fix indentation.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Why 7 nested loops? For a 'result' of 10, that would mean the inner logic would be executed 10 million times.

Your variables: A, B, C, ... are all defined with a block level scope and would therefore not be accessible outside of the block. Also, don't use leading upper-case letters for variable names.

Fix indentation.



Forgive me for asking so many questions but lets say I define the strings before the nested loops, then how do I make the message box display the string each time its supposed to? For example here is my code. where I define the strings before the loops.




I am not sure what I need to put after the "if then" statements so it knows to display the string at that time. Also the code I currently have wont even compile. I get the following errors on line 72

"Error: Syntax error on tokens, ConstructorHeaderName expected instead

Error: Syntax error on token "showMessageDialog", invalid AnnotationName

Error: Syntax error on token ",", ? expected"

So again please forgive me for asking so many questions but I am very confused right now.

]
 
Carey Brown
Saloon Keeper
Posts: 10927
87
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

Has a syntax error. No operator '><'.
 
Carey Brown
Saloon Keeper
Posts: 10927
87
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
It's imperitive that you clean up your indentation. If you had you would have probably seen the error at the end.
 
Carey Brown
Saloon Keeper
Posts: 10927
87
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

Are you missing this case?
 
Carey Brown
Saloon Keeper
Posts: 10927
87
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
Without StringBuilder you might try something like this (I left out a whole bunch of code but I hope you get the idea).
 
Carey Brown
Saloon Keeper
Posts: 10927
87
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
I suggest temporarily outputting the strings using System.out.println() first, rather than working on concatenating your strings. You will find that you have some logic kinks to work out that would either cause your program to appear to hang or to blow up JOptionPane.
 
Marshal
Posts: 79949
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote: . . . Has a syntax error. No operator '><'.

Not necessarily. I have seen that before; I think there is a bug in the forum software which alters < to >< or alters > to ><. I think your correction to < throughout is correct.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Sing wrote:Forgive me for asking so many questions


No problem. That's what we're here for.

but lets say I define the strings before the nested loops, then how do I make the message box display the string each time its supposed to?


I think the others have covered that, but what makes you think you need nested loops at all? I thought FizzBuzz was just a counting game.

But here's a tip: Instead of all those separate fields, what if you just had three? viz:
How do you think that might help you? And how would you write your program then?

[Edit] I should add that it's usually better to declare loop variables inside the loop, unless you have a very good reason not to (and you don't), viz:

  for(int a = 0 ; a <= result ; a++) { ...

and you certainly don't need 7 of them.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 79949
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what you want. It tells you how to turn several Strings into a pretty‑print String. It also shows you how to do it with a Stream. All you need to do now is to work out which classes to import
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:This is what you want. It tells you how to turn several Strings into a pretty‑print String...


Oddly enough I already wrote pretty much that exact same class. A Repeater one too, along the same lines. Trust Java to incorporate it two releases too late.

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

Winston Gutkowski wrote:. . . Trust Java to incorporate it two releases too late. . . .

Bad spelling there. The number should be spelt s‑e‑v‑e‑n

They are quite good at adding things which many people wrote for themselves years and years ago.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic