File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pls help ! on charAt()" Watch "Pls help ! on charAt()" New topic

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;
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: 1090
Hi Jamieson
Do you mean somethin like this.
Orignal String : This is a test
Resulting String : Ti sats
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.
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
Don't get me started about those stupid light bulbs.
subject: Pls help ! on charAt()
It's not a secret anymore!