This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Pls help ! on charAt() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pls help ! on charAt()" Watch "Pls help ! on charAt()" New topic
Author

Pls help ! on charAt()

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
I am trying to write a small code to read a String, and then print out every other character of the string start at first character.
Here i have so far, but not sure how to read the next character, and not print it out.
Any suggestion or hint? I greately appreciate it.

public class Test {
public static void main (String [] args)
{
String str = "This is a test" ;
boolean startWord = true;
for (int i = 0; i < str.length() ; i++)
{
if (startWord)
System.out.println (str.charAt(i));
if (str.charAt(i) == ' ')
startWord = false;
else
startWord = true;
}
}
}
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm not sure what startWord is supposed to be doing, but it looks like you're pretty close. What is it printing now? All the letters except a letter after a blank? If you're after every other letter you might toggle a boolean every time through the loop:

Or a better hint: Take a look at the for() statement. What part of that is telling it to look at the next character every time through the loop? Could you change it to look at every 2nd character?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Jamieson
Do you mean somethin like this.
Orignal String : This is a test
Resulting String : Ti sats
Or
Resulting String : Ti s et
Or neither of these.
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Anupam Sinha
Do you mean somethin like this

Orrignal String : This is a test
Resulting String : Ti sats.

Yes. I am looking for something like this result.
Thanks
jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Thank you Stan for your hint,
i got the answer,
Really appreciate it.
Anupam Sinha

I got the answer. thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pls help ! on charAt()