INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!

*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.

Jobs

Threads are driving me crazy

Threads are driving me crazy

(OP)
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;
while(cont)
{
try
{
System.out.print(msg + ".. ");
sleep(1000);
}
catch (InterruptedException e)
{
break;
}
}
}
}
// --------End: Advertisement.java ------------
// -------- Begin: TestAdvt.java ------------
import java.io.*;

class TestAdvt
{
public static void main (String[] args)
{
try
{
String str = null;
do
{
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;
else{
Advertisement advt = new Advertisement(str);
advt.setDaemon(true);
advt.start();
System.in.read();
advt.interrupt();
}
} 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

Cheers,
Dian

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!

Resources

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