Jump to content
  • Advertisement
Sign in to follow this  
Xiachunyi

Java: Strings [Solved]

This topic is 4653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am currently trying to make an address book and I have come upon the problem of how to catch if the user has entered, in the name prompt, nothing at all. So far I have done something of the following:
do
{
...
//Prompt the user utilizing "BufferedReader" and "ReadLine()" for a String
...
//Check user input
	if(sDataArray[loop].length() == 0)
	{
		bErrorFlag = true;
	}
...
}while(...)
My purpose for this would be if the user pressed enter and nothing was written before, the input would be invalid and I could prompt the user again. This works, even if the user presses enter, because it changes the flag to true but upon the second run, no matter if I enter in the valid data or another enter, it still flags it as an error because the length returned is still zero. One more question: I have been reading about how Strings are immutable and looked into why you can do something like:
private String sStuff;
...
public void Foo(String Stuff)
{
        sStuff = Stuff;
}
I, through my reading, seem to have come to the conclusion that sStuff has its own memory address and when it is assigned to Stuff, it now has Stuff's memory address and the contents of that memory address. Now, assuming I am right, is that not bad since when this method exists, the local variable Stuff will no longer point to that memory address and that memory address is invalid? It seems kind of logical, to me anyway, that if something pointing to that memory address goes out of scope, then that memory address should be declared invalid as well regardless if anything else is pointing to it since its original reference is destroyed. Thank you. [Edited by - Xiachunyi on September 24, 2005 12:40:32 PM]

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure about your first problem...are you saying that you input a new string, but its length stays at zero always, if it was zero the first time?

Anyway, java keeps a reference count and it won't de-allocate memory that is referenced. If those were C char-arrays, you'd be in trouble, but in Java that method is safe.

Share this post


Link to post
Share on other sites
I can't tell from your post, but have you tried setting bErrorFlag to false prior to testing for length the second time? Perhaps just add an else clause if the string's length isn't invalid and set bErrorFlag to flase.

If you've already done this, then sorry for wasting your time.

Share this post


Link to post
Share on other sites
Gunslinger RR: You are right, I assigned my error flag outside the do-while loop.

Ah, on my CS test, I had a UML diagram that I to build and I ended up utilizing StringBuffers because I got confused between assigning something to a variable, in actual reality a container, to assigning the memory address to a reference variable. Darn!

Thank you all.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xiachunyi
Gunslinger RR: You are right, I assigned my error flag outside the do-while loop.

Ah, on my CS test, I had a UML diagram that I to build and I ended up utilizing StringBuffers because I got confused between assigning something to a variable, in actual reality a container, to assigning the memory address to a reference variable. Darn!

Thank you all.

No problem, glad to have helped.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!