• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create instance of an object in a loop  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure out where to create the StringHandler object.
My code should take a string as input, then create StringHandler object ord with the string input. This should repeat until cancel is pressed, then ord should be sent to the Utskrift-method (a print method).

If I do like this, null is also sent to Utskrift. I dont want that to happen.
If I put StringHandler last in the loop ord can not be resolved.

Could someone point me in the right direction?


 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anders Gren wrote:I can't figure out where to create the StringHandler object.
My code should take a string as input, then create StringHandler object ord with the string input. This should repeat until cancel is pressed, then ord should be sent to the Utskrift-method (a print method).

Are you sure? Seems like an odd way to go about things. It would also be useful to see the Utskrift() method (which should actually be 'utskrift()').

There are several possible solutions, just one of which is to set the control variable (your String) in the loop statement itself, viz:

while ((text = showInputDialog(null, "Enter text:")) != null) { ...   (Note: the extra brackets ARE important)

or another is the old Dykstra method of repeating the control assignment at the END of the loop:Personally, I like the latter, because it's generally more flexible; but there's no "right" way to do this stuff, so it basically comes down to what you find easier to read.

HIH

Winston

PS: Welcome to javaRanch, Anders!
 
Anders Gren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer. Here is the utskrift-method as it looks at the moment. I'll develop the details later:




My problem is that the utskrift-method should be activated only if I press cancel.
So what should happen is for example this:

fire stone ants
<OK>
grandmother biscuits
<OK>
<CANCEL>

You wrote two strings.
The last string was "grandmother biscuits". It had two words.

 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should happen if the user presses cancel on the first dialog ?
This will work except for when the user clicks Cancel on the first dialog, but an extra null check for that case is easy enough to do.
 
Anders Gren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the easiest would be to just print "You pressed cancel before any strings were entered." Or just nothing.
 
Anders Gren
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that's the way to do it! I thought it had to be something like that but didn't manage to find the right solution. Thanks for the help!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anders Gren wrote:So that's the way to do it! I thought it had to be something like that but didn't manage to find the right solution. Thanks for the help!

Not sure about that, because even Joanne's solution doesn't handle the situation where the user presses 'Cancel' immediately.

You need to describe the exact behaviour that you expect in every case, which is just one of the reasons we advise you to write things down before you attempt your first line of Java code. (Have a look at the StopCoding (←click) page.)

I'm also not sure why you're using a StringHandler. It doesn't add anything to the logic (at least as you have it now), since you could just as easily print the String that was entered.

My suspicion is that your prof wants you to use the StringHandler to store ALL the Strings that were entered, and then print them out once at the end - but like I say, it's only a suspicion.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!