Ask coding questions

← Back to all posts
How can I make letter by letter output show in Java?
ZacPlayz (64)

I want to make letter by letter output in my game "The War" to make it more nice and fancy, since I am considered a "beginner" in my coding lessons, but I do some self-taught, and so that's why I'm asking... :P so that I can gain some self-taught knowledge for my game. Please write in the comments what commands I should use, and please write the commands with the System.out.println(""); so that I can learn to use the command. Whoever answered me first and likes this post will get 5 cycles for the answer and a like on the comment itself, overall 6 cycles!! So be quick!! :P

Answered by SixBeeps (3047) [earned 5 cycles]
View Answer
Commentshotnewtop
SixBeeps (3047)

Ah yes, rolling text. Here's a method for ya:

public static void RollText(String msg) {
    for(int i = 0; i < msg.length(); i++) {
        System.out.print(msg.charAt(i));
        Thread.sleep(100);
    }
}
ZacPlayz (64)

Thanks!! You have earned 6 cycles @SixBeeps Congratulations!!

ZacPlayz (64)

wait wait wait look at this @SixBeeps !!

public class Main {
public static void main(String[] args) {

public static void RollText(String msg) {
for(int i = 0; i < msg.length(); i++) {
System.out.print(msg.charAt(i));
Thread.sleep(100);
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
}
}

}
}

ZacPlayz (64)

The ats (@) are just for to make it more run-on I guess but this thing doesn't work!! Did I put it in the wrong combination??

SixBeeps (3047)

@ZacPlayz No, you're supposed to call my method instead of using System.out.println for putting things to the screen.
Use this instead:

import java.lang.*;
public class Main {
    public static void main(String[] args) throws InterruptedException {
        RollText("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
    }
    public static void RollText(String msg) throws InterruptedException {
        for(int i = 0; i < msg.length(); i++) {
            System.out.print(msg.charAt(i));
            Thread.sleep(50);
        }
    }
}
ZacPlayz (64)

This is what pops up... @SixBeeps

Main.java:8: error: unreported exception InterruptedException; must be caught or declared to be thrown
Thread.sleep(100);
^
1 error

ZacPlayz (64)

btw u forgot to put another wavy bracket } at the end to mark the end of public class Main { @SixBeeps

ZacPlayz (64)

And when you put the last wavy bracket you get what I wrote that pops up

SixBeeps (3047)

@ZacPlayz Ah I forgot to end the for loop :P

SixBeeps (3047)

@ZacPlayz Ok I updated the code above. Imma test this out too because threading seems weird in Java.

EDIT: I just tested it and posted fixed code above.

ZacPlayz (64)

nice!! works now thx!!!! @SixBeeps