Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Threads are driving me crazy

Threads are driving me crazy

Threads are driving me crazy

The objective of these code is to create a very basic advertisement that prints every second. You get to enter a new message if you press enter at any time while it's blinking, and you exit the program if you input
"n" or "N".

The program works fine the first round, but in the subsequent loops for some FRACKIN reason
1. it knocks off the first letter of any input
(ex. input "announcement" prints as "nnouncement.. nnouncement..")
2. it prints the "Please enter..." statement AGAIN before it starts the Advertisement loops

Tis my first time here but any and all help would be soooo appreciated...

Here is the code:
// -------- Begin: Advertisement.java ------------
class Advertisement extends Thread
String msg = "";

public Advertisement(String str) {msg = str;}
public void run()
boolean cont = false;
if (!msg.equals("")) cont = true;
System.out.print(msg + ".. ");
catch (InterruptedException e)
// --------End: Advertisement.java ------------
// -------- Begin: TestAdvt.java ------------
import java.io.*;

class TestAdvt
public static void main (String[] args)
String str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the advertisement message to be displayed (enter 'n' to exit):");
str = br.readLine();
if (str.equalsIgnoreCase("n")||str.equalsIgnoreCase("N")) break;
Advertisement advt = new Advertisement(str);
} while(true);
catch (IOException e)
System.out.println("error: " + e.getMessage());
// --------End: TestAdvt.java ------------

RE: Threads are driving me crazy

My guess is that the System.in.read(); before the advt.interrupt(); is eating the first key stroke, but it's just a guess.

Btw, is you use equalsIgnoreCase you just need to check for n or N, not both


RE: Threads are driving me crazy

So, yihyang, did you ever get it working?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close