The Artima Developer Community
Sponsored Link

Java Answers Forum
Little help with a for loop inside a for loop

1 reply on 1 page. Most recent reply: May 15, 2003 12:04 AM by Rahul

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Kris K

Posts: 6
Nickname: krisk
Registered: Apr, 2003

Little help with a for loop inside a for loop Posted: May 14, 2003 7:27 PM
Reply to this message Reply
Advertisement
ok i have 2 strings
lets call it word = "hello" alpha = "abcdefg.... down to z"
char current1 = " "
char current2 = " "

ok now,

for (int x=0;x<word.length();x++)
{
boolean done = false;
current1 = word.charAt(x);
for (int y=0; y<alpha.length();y++)
{
current2 = alpha.charAt(y);
if (current1 == current2)
{
System.out.print("*");
done = true;
}
}
if (done = false)
{
System.out.print(current1)
}
}

basically, if the char is in the alpha string, it returns a *, as the char hasnt been guess... when a char from the word string is guessed, it is removed from the alpha string.

Hence, once both loops run, if the char in word isnt found in the alpha string then done should be false and the char in word is just written.

hence for example
word = "hello"
alpha = "all the alphabet letters, abcdefg"
then out put works as, *****

but if i make it like
word = "hello"
alpha = "all but e, like abcdfghijklmn..."
then the output is **** , i want it to be *e***

hence why i tried to use the boolean done, incase the char we are lookin for isnt done in the loop.


plz help
thanks =)


Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: Little help with a for loop inside a for loop Posted: May 15, 2003 12:04 AM
Reply to this message Reply
public class WordStar 
{
	public static void main(String[] args) 
	{
		String main = "concordance";
		String total = "abdefghijklmnopqrstuvwxyz";
		boolean done = false;
		
		for (int i=0;i<main.length() ;i++ )
		{
			done = false;
			for (int y=0;y<total.length() ;y++ )
			{
				if (main.charAt(i)==total.charAt(y)){
					System.out.print('*');
					done = true;
					break;
				}
			}
			if (done==false)
				System.out.print(main.charAt(i));
		}
	}
}

Flat View: This topic has 1 reply on 1 page
Topic: Simple Array Prob Previous Topic   Next Topic Topic: MDA at work! OMG technology on share

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use