A Simple Looping Question

Urgent, any help is appreciated!

Page 1 of 1

11 Replies - 571 Views - Last Post: 16 February 2010 - 11:07 PM Rate Topic: -----

#1 Guest_L.E.*


Reputation:

A Simple Looping Question

Posted 16 February 2010 - 09:54 PM

I've been working on this problem for over 10 hours straight, it's really starting to frustrate me! I'm writing a fairly simple program that requires several nested loops. I'm just about finished, everything runs fine, except for one small problem. For the sake of simplicity, I will generalize it:

I have a do-while loop set up. If the user answers yes to a question, the loop repeats. The user is then prompted to enter another name each time this occurs. However, each time the loop repeats and a new name is entered, it seems to overwrite the value of the previous name. After the loop, I have set up a dialog box that will display all of the names that were entered. My question is, how do I set the loop up to record/assign each of the names entered to a new value? Or more generally, is there a way to assign a String (such as a person's name) a new value each time the loop repeats, other than using something like an array?

Thanks in advance for any help!

Is This A Good Question/Topic? 0

Replies To: A Simple Looping Question

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: A Simple Looping Question

Posted 16 February 2010 - 10:01 PM

it could have been easier for me if you had posted your code here
here is the possible solution
see
for your problem with the loop
I recommend you to use while loop as
while (answer != "yes" )
{
// then continue the things you want to do
}

and if there is problem of overwriting
then use an array which stores the names in the respective array elements
if you want another name to be entered
then increment the counter in the while loop
if you don't want then do nothing
Was This Post Helpful? -1
  • +
  • -

#3 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: A Simple Looping Question

Posted 16 February 2010 - 10:11 PM

Why don't you post your code? It's easier to explain.
Was This Post Helpful? 0
  • +
  • -

#4 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: A Simple Looping Question

Posted 16 February 2010 - 10:13 PM

If I understand correctly, you want to keep track of all the names that were entered and then display them at the end? As pdkharkar said, this would be most easily accomplished using an array or similar data structure.

But in your post, you explicitly state that you don't want to use arrays. I'm curious why. It's a strange aversion.

However, the easiest thing I can think of WITHOUT using an array and without setting up a bunch of messy temp variables would be to concatenate all the names into a single string, separated by a distinct marker. Then during the program, if you need to use the most recently entered info, you could find the last instance of that marker and get a substring from that index to the end. Then at the end of the program you can chop your single string into any number of substrings based on the indexes of those markers.

But really, if you're going to all that trouble, why not use arrays?
Was This Post Helpful? 1
  • +
  • -

#5 Guest_L.E.*


Reputation:

Re: A Simple Looping Question

Posted 16 February 2010 - 10:30 PM

Thank you all for such quick replies!

In response to pdkharkar, that is essentially the setup I have, and the problem is not with the loop, but with the String variable being overwritten each time the loop occurs. For example, after the first loop the name entered might be Jason, and the second time the loop occurs, rather than the name being Jason + Cindy, it's just Cindy - i.e. it's overwritten. This is a problem because I need to recall the names individually later on in a separate dialog box occurring after the initial loop.

In response to anonymouscodder, I would be more than glad to post the code, but unfortunately the .java file is located on my laptop, which I do not have with me at the moment. The program contains multiple classes and is fairly long, but this particular problem involves a do-while loop that is looped each time } while (subRegistration == JOptionPane.YES_OPTION); , for example

In response to xor-logic, yes, that is exactly the problem I am trying to describe. There is not a specific reason for not wanting to use an array, I am just curious if there is a way to do it without one. I have spent some time trying to figure it out, and would be quite disappointed if all of it were wasted! Yes, I know it is not very efficient, but it seems to be a difficult challenge. I have tried a few ways of concatenating the names into one String using temporary variables, but I couldn't seem to do it correctly. Is there any way you could give me an example of the solution you're describing, regardless of how messy it is?
Was This Post Helpful? 0

#6 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: A Simple Looping Question

Posted 16 February 2010 - 10:33 PM

View PostL.E., on 16 February 2010 - 09:30 PM, said:

Is there any way you could give me an example of the solution you're describing, regardless of how messy it is?

Ok, the cleaner way to do this would be to declare two strings prior to the loop. Once a name is provided, assign it to one of the strings. At the end of the loop, add it to the end of the other string. This way you will have one string holding the current name, and one string holding all the previous ones. Just remember that when you add a name to the "storage" string, append it with a special character. Which one doesn't really matter. I typically use question marks.
Was This Post Helpful? 1
  • +
  • -

#7 Guest_L.E.*


Reputation:

Re: A Simple Looping Question

Posted 16 February 2010 - 10:49 PM

xor-logic:

Thank you very much for the advice. I actually have tried just what you suggested, except that I'm a little confused about the String I place at the end of the loop. That is, I'm not sure what value to assign it before I concatenate it with the original String. Would I just do something like String tempName = "" + nameOfPerson ? Also, how would I go about appending it what a special character? I apologize in advance - I have only been using Java for a few months, and this is one of the first problems I've had a significant amount of trouble with. Thanks again for your help.
Was This Post Helpful? 0

#8 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: A Simple Looping Question

Posted 16 February 2010 - 10:53 PM

View PostL.E., on 16 February 2010 - 09:49 PM, said:

xor-logic:

Thank you very much for the advice. I actually have tried just what you suggested, except that I'm a little confused about the String I place at the end of the loop. That is, I'm not sure what value to assign it before I concatenate it with the original String. Would I just do something like String tempName = "" + nameOfPerson ? Also, how would I go about appending it what a special character? I apologize in advance - I have only been using Java for a few months, and this is one of the first problems I've had a significant amount of trouble with. Thanks again for your help.

Create it as a blank before the loop, then at the end of each iteration, something like
tempName += nameOfPerson + "?";


would work well.
Was This Post Helpful? 1
  • +
  • -

#9 Guest_L.E.*


Reputation:

Re: A Simple Looping Question

Posted 16 February 2010 - 10:55 PM

Also, how would I continue to concatenate the additional names that were entered to the second temporary String? That is, if the second String is concatenated with the first one, how do I prevent it from (again) simply overwriting it, rather than adding a new one each time?
Was This Post Helpful? 0

#10 Guest_L.E.*


Reputation:

Re: A Simple Looping Question

Posted 16 February 2010 - 10:57 PM

Ahhh, how stupid of me. I did exactly that, but forgot the "?". xor-logic, you are brilliant, I will try this soon to see if it works.
Was This Post Helpful? 0

#11 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: A Simple Looping Question

Posted 16 February 2010 - 11:02 PM

View PostL.E., on 16 February 2010 - 09:57 PM, said:

...xor-logic, you are brilliant...

My wife would disagree sir.
Was This Post Helpful? 2
  • +
  • -

#12 Guest_L.E.*


Reputation:

Re: A Simple Looping Question

Posted 16 February 2010 - 11:07 PM

View Postxor-logic, on 16 February 2010 - 10:02 PM, said:

View PostL.E., on 16 February 2010 - 09:57 PM, said:

...xor-logic, you are brilliant...

My wife would disagree sir.



Haha, well mine might say the same about me when it comes to anything practical, such as house chores. When it comes to philosophy, music, and the abstract, I do quite well, as I'm sure you probably do too.

Anyway, xor-logic, I can't thank you enough. I had tried much more complex and unnecessary possible solutions that made quite a mess of things. I've spent 10 hours on this simple problem, and your solution worked perfectly! I tried it on an older program I wrote with a similar structure, and it seems to do the job. I can't thank you enough.
Was This Post Helpful? 0

Page 1 of 1