Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:48:08+00:00 2026-05-28T17:48:08+00:00

I’ve managed to make a typewriter class that does what I want it to

  • 0

I’ve managed to make a typewriter class that does what I want it to for the most part. It will output the string given to it one character at a time, pausing between each one as if they were typed, pausing a bit longer after periods. The problem I’m having now is that when I use this class it only works once. When I call it twice (or more) it tries to run them at the same time. This causes major problems. So I need a way for the first instance of this class to run and each one that follows to wait “it’s turn” before starting. Below is an example of the desired outcome and the current.

import objectdraw.*; // Where active object comes from.     
import javax.swing.JTextArea;

public class Typewriter extends ActiveObject {

  private JTextArea out;
  private String in;

  public Typewriter(String s, JTextArea output) {

    in = s;
    out = output;
    start();

  }

  public void run() {

    synchronized(out) {

      for(int i=0; i<in.length(); i++) {

        out.append(in.substring(i,i+1));
        if(in.charAt(i) == '.') {
          pause(30);
        } else {
          pause(200);
        }

      }

    }      

  }

}

Current:

CODE:
new Typewriter(“\nHello”, output);
new Typewriter(“\nWorld”, output);

CURRENT OUTPUT

HW ol elr ldo

DESIRED OUTPUT

Hello
World

Obviously I left out most of the code from the Typewriter class. If that’s really needed I could post it. The javadocs for ActiveObject can be found here. This is how I was taught threads and I’m afraid it may be the problem.

EDIT:

Per an answer below I’ve added the synchronized(out) line but I’m getting a nullpointerexception when I try to run the code.

Exception in thread "main" java.lang.NullPointerException
    at objectdraw.ActiveObject.<init>(ActiveObject.java:239)
    at com.caldwellysr.TBA.Typewriter.<init>(Typewriter.java:11)
    at com.caldwellysr.TBA.Client.initGame(Client.java:78)
    at com.caldwellysr.TBA.Client.<init>(Client.java:66)
    at com.caldwellysr.TBA.Client.main(Client.java:24)

The Typewriter line 11 is the header for the constructor.
Client line 78 is where I call new Typewriter(“Testing”, output); where output is a JTextArea
Client line 66 is a call to initGame() which has the Typewriter in it
Client line 24 is the JFrame constructor.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:48:09+00:00Added an answer on May 28, 2026 at 5:48 pm

    It seems that ActiveObject inherits from the Thread class and runs asynchronously so you won’t know when your thread runs. When you create 2 ActiveObject instances, the 2 for loops will not necessarily run one after the other. The result you see is your text area being updated by 2 threads simultaneously.

    EDIT:

    You can run your code synchronously with the synchronized clause:

    synchronized(out){
        for(int i=0; i<in.length(); i++) {
            out.append(in.substring(i,i+1));
    
            if(in.charAt(i) == '.') {
                pause(30);
            }
            else {
                pause(200);
            }
        }      
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.