Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying a string one character at a time

Status
Not open for further replies.

A5k

Programmer
Jul 29, 2001
54
CA
Hi all,
A relatively simple question. Let's say I have a String "Java" and I want to display it one character at a time (J(sleep)a(sleep)v(sleep)a(sleep)), how would I go about doing this in Java?

-a5k
 
I should add that I've found a way (listed below) but I'm looking for another way in which I don't have to use {"J","a","v","a"} in an array...

public class test{
public static void main (String args[]){
String java[] = {"J","a","v","a"};
for(int i=0;i<4;i++){
System.out.print(java);
for(int j=0;j<10000000;j++){}
}
}
}

-a5k
 
Try the charAt( int i ) method of String. It basically treats the String as an array without it having to be one. For instance:

String java = &quot;Java&quot;;
for( int i = 0; i < java.length(); i++ )
{
System.out.println( java.charAt( i ) );
try{ Thread.sleep( 1000 ); }catch( Exception e ){}
}

BTW, if you want to make the thread sleep for a bit, use the Thread.sleep( long time ) method. Its quite a bit more reliable and predictable.

-gc
 
Thanks godcomplex! works like a charm!

-a5k
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top