Creating a custom method to slowly print text in Java

processing text effects
java code for printing a page
moving text - processing
how to print in java swing application
processing text box
text stroke processing
string processing
java swing print text

I am trying to create a method in Java that will accept a string to slowly print out. I found the code to do a slow print on here(Slowly print text in Java), and have been trying to make a method out of it, but I'm still very new to Java. Here's my code

public class propago
{
    public static void main(String[] args)
    {
        String strMessage;  
        strMessage = "Testing 1 2 3";
        slowPrint(strMessage);
    }//end main(String[])

    public static void slowPrint(String[] args) throws InterruptedException
    {
        char[] chars = strMessage.toCharArrray();

        for (int i = 0; i < chars.length; i++)
        {
            System.out.print(chars[i]);
            Thread.sleep(100);
        }
    }//end slowPrint(String[])
}//end propago

The answer might be obvious, but I'm just not getting it.

When trying to compile the code, I get the following errors:

propago.java:12: error: incompatible types: String cannot be converted to String[]

                slowPrint(strMessage);
                          ^
propago.java:17: error: cannot find symbol
                char[] chars = strMessage.toCharArrray();
                               ^
  symbol:   variable strMessage
  location: class propago
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors

After applying the suggested changes (hopefully correctly), incompatible types error was solved, but I still receive the following error

propago.java:10: error: cannot find symbol
                char[] chars = strMessage.toCharArray();
                               ^
  symbol:   variable strMessage
  location: class propago
1 error

Here is my current code with changes made

public class propago
{
    public static void main(String[] args)
    {
        String strMessage;  
        strMessage = "Testing 1 2 3";
        slowPrint(strMessage);
    }//end main(String[])

    public static void slowPrint(String args)
    {
        char[] chars = strMessage.toCharArray();

        for (int i = 0; i < chars.length; i++)
        {
            System.out.print(chars[i]);
            try {
            Thread.sleep(100);
            } catch (InterruptedException e){
                //Should not happen
            }
        }
    }//end slowPrint(String[])
}//end propago

Strings and Drawing Text \ Processing.org, If you are looking to display text onscreen with Processing, you've got to first become Although technically a Java class, because Strings are so commonly used, Create the font by referencing the font name and the function createFont​(). the process of loading a font into memory is slow and would seriously affect the  The printf() method allows us to format output to a java.io.PrintStream or java.io.PrintWriter.These classes also contain a method called format() which can produce the same results, so whatever we read here for the printf() method can also be applied to the format() method.

Your problem is that slowPrint that it takes a String array and may throw an unchecked Exception, namely InterruptedException.

To solve the problem with the String array, just change the method to take a String, not String[]

In your code there is no reason to believe someone will interrupt your code (only happens in multi-threaded code), so you may just ignore it by catching the exception when it occurs:

try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    return;
}

Then remove the throws InterruptedException from the declaration of slowPrint.

Articles: Tuning Java I/O Performance, The first approach simply uses the read method on a FileInputStream : An obvious question is: Will making the buffer bigger make I/O go faster? Another aspect of buffering concerns text output to a terminal window. The fact that approach 3 is quite a bit slower than approaches 1 and 2 doesn't mean that you shouldn't  We use print in java to output required text directly to the console of IDE. Let's see the syntax of print in java and the difference between print and println in Java. Syntax of Print in Java: Simple print statement: System.out.print(“Learning Java from SoftwareTestingMaterial”); A simple print statement with a new line: System.out.println(“Learning Java from

try this:

public class propago
{
    public static void main(String[] args)
    {
        String strMessage;  
        strMessage = "Testing 1 2 3";
        slowPrint(strMessage);
    }//end main(String[])

    public static void slowPrint()
    {
        char[] chars = this.strMessage.toCharArray();

        for (int i = 0; i < chars.length; i++)
        {
            System.out.print(chars[i]);
            try {
            Thread.sleep(100);
            } catch (InterruptedException e){
                //Should not happen
            }
        }
    }//end slowPrint(String[])
}//end propago

How to Print Text (The Java™ Tutorials > Creating a GUI With JFC , When you call the print method with no parameters, a print dialog is displayed, and then your text component is printed interactively without a header or a footer. This java program will print message using class method - here, we will print the message by using same class method and other class method. In this example we will print a message on screen by creating a class. We will make a separate class in which there is a method named printMessage to print the message. We will create a object of that class in public class in which Main() function is called. Using that object we will call that method and output will be on screen.

This is a little beyond the needed answer, but I wanted to define how I would approach it: having a thread-safe receiver for your calls. This could then queue up the messages in an appropriate delay in another thread:

public class MyLogger {

    private final Logger logger; //some logger
    private final Deque<String> out = new ArrayDeque();
    private final long msgDelayMS; //how long of a time must exist between messages

    public MyLogger(long msgDelayMS) {
        this.msgDelayMS = msgDelayMS;
    }

    public void log(String simpleString) {
        //Use a ReadWriteLock on `out` or somesuch to synchronize the data safely
        this.out.add(simpleString);
    }

    public ScheduledFuture<?> start(ScheduledExecutorService executor) {
        return executor.scheduleAtFixedRate(this::printNext, 0, this.msgDelayMS, TimeUnit.MILLISECONDS);
    }

    private void printNext() {
        //again, synchronize with a `lock` field or somesuch
        String next = this.out.poll();
        if (next == null) {
            return;
        }
        System.out.println(next); //prints however we like our valid queue'd item
    }
}

I didn't want to bog down into the multithreading details too much, but in this you essentially have a thread-safe wrapper for any form of logging you wanted to do. Then using it is just:

MyLogger logger = new MyLogger(1000L); //1000ms delay == 1 second
logger.start(Executors.newSingleThreadScheduledExecutor()); //should keep the executor somewhere
//elsewheres, from any thread, without having to keep track of time or sleep the thread
logger.log("Hello world!");

This separates the "execution" of your logging and your actual program logic. So things may be logged that happened 30 seconds in the past, if you logged 30 messages within a specific moment. But it does make your logging non-blocking, in the sense that you don't have to wait for it to be an appropriate time to print, you just set it aside for another thread to handle. I suppose this answer is to kind of entice a little programming spirit, and also because I was kind of toying with a similar idea in my head at the moment anyhow.

[PDF] Threads and Swing, Scrolling Text in a Custom The Swing graphical toolkit brings a host of new components to the Java platform. methods like paint() on Component, actionPerformed() on ActionListener, and all of the print(“creating code block for event thread”); short sleep of 0.1 seconds (line 33) is used to momentarily slow the event  Supported formats include HTML, RTF, and plain text. For common printing tasks such as simply printing a text document, use the print method directly. The print method has several forms with various argument sets. This method prepares your text document, gets a corresponding Printable object, and sends it to a printer.

Printing custom documents, Creating print output that is completely customized for your application requires more programming investment than the getString(R.string.app_name)} Document" Note: The last parameter in the print() method takes a PrintAttributes object. Java is a registered trademark of Oracle and/or its affiliates. Create a custom exception class in Java; Throw our custom Java exception; Catch our custom exception, and; Look at the output from our custom exception when we print a stack trace; A Java custom exception class. To get started, here's our simple Java custom exception class. As you can see, to create a custom exception class, all you have to do

Slow rendering, After looking at the systrace output, there might be methods in your app that you suspect are If you can't reproduce jank on a local device, you can build custom or piece of text changes between types, you can make that change at bind time​, Java is a registered trademark of Oracle and/or its affiliates. This article explains the print() and println() methods in Java. When using Java println( ), the output is displayed with the cursor in the next line. In case of Java print( ), the output is displayed with cursor in the same line.

Backpacker, Next, I make a list of all the attributes and features my ideal tent should contain. © Focus on necessities. Do I really need double vestibules? To avoid expensive  Definition and Usage. The print() method prints the contents of the current window. The print() method opens the Print Dialog Box, which lets the user to select preferred printing options.

Comments
  • What is the problem? Does it work? Does it show some error if you run it? Does it show an error if you compile it? Please edit your question to add the details about what is wrong. It helps a lot if you show what is wrong.
  • I knew I was forgetting to add something to my question, thank you
  • Why do you want to accept array of strings when you are using it as single string? Remove those [] from slowPrint(String[] args).
  • Also argument of slowPrint is named as args not strMessage. Those are separate variables and they belong to different code blocks.
  • Related (I am not marking it as duplicate since I am not sure if you want to know reasons behind errors or just get working solution): making text appear delayed
  • Thank you so much for clarifying that, I've just begun learning Java at my university this semester and I assume for what we are currently covering, we haven't needed to worry about the difference between using "String" and "String[]". Now I just have the following error: ``` propago.java:10: error: cannot find symbol char[] chars = strMessage.toCharArray(); ^ symbol: variable strMessage location: class propago ```
  • See my second item on the list, you are trying to use strMessage which is not declared anywhere in slowPrint scope. Change it to args which is the parameter you are passing to this method.
  • Oh! Thank you, I wasn't even thinking to change "strMessage.toCharArray()" to be "args.toCharArray()". It's working perfectly now!
  • Recommend catch (InterruptedException e) { Thread.currentThread().interrupt(); break; }