Nick Wills
Nick Wills

Reputation: 21

Display Characters in Java

How do I get java to read and all of the characters from a word of any length?

I know that I have to use the .charAt(x) method, but how can I get the characters from a word of any length (eg: 2 or more letters)

Also: how would I then get it to build this word up slowly again, as in display the letters slowly building up the letter (i am aware i need to use System.out.println())

eg: if my word was sugar, java would display: s then: su then: sug then: suga then: sugar

Thank You

Upvotes: 1

Views: 204

Answers (1)

Lucas
Lucas

Reputation: 3281

use toCharArray() method of String and iterate through the array returned by that method, use StrinfBuilder to incrementally build it again, put thread to sleep for the amount of milliseconds that suits you.

char[] chars = word.toCharArray();
StringBuilder builder = new StringBuilder();


for(char c : chars) {
  builder.append(c);
  System.out.print(builder.toString() + " ");
  Thread.currentThread().sleep(milliseconds);
}

Upvotes: 1

Related Questions