Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Combing Multiple String With No StringBuilder?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37222
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you should indent your code better.
This fragment doesn't look well:
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is in here, but basically, put new line characters ("\n") between the strings.
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Has a syntax error. No operator '><'.
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are you missing this case?
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 55681
161
  • Mark post as helpful
  • send pies
  • 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: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 55681
161
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!