The Artima Developer Community
Sponsored Link

Java Answers Forum
Java inputstream reader

3 replies on 1 page. Most recent reply: Aug 26, 2003 8:02 AM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
karthik

Posts: 2
Nickname: karymov
Registered: Aug, 2003

Java inputstream reader Posted: Aug 26, 2003 3:51 AM
Reply to this message Reply
Advertisement
I am new to java.
I have a project in which I run a thread which wakes up every 25 millisecs checks for user input and if the enter key is pressed the program quits.
I am trying to use inputstream reader functions coz thats what we are expected to use.
I am unable to run my program as I cannot get the read keyboard input and compare it to /n.
cud someone help me.


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Java inputstream reader Posted: Aug 26, 2003 4:37 AM
Reply to this message Reply
I suppose you are reading in the Keyboard input character by character. If so you can get the ascii value and check whether its equal to 13 because new line char is always 13.

If you can paste your code here might be able to help better.

karthik

Posts: 2
Nickname: karymov
Registered: Aug, 2003

Re: Java inputstream reader Posted: Aug 26, 2003 5:51 AM
Reply to this message Reply
Hi
Here is the code

import java.io.*;
import java.io.IOException;

class KeyMonitor extends Thread
{
private InputStreamReader in;
private int name ;
private Server myServer;
private boolean running =true;
private int sleeptime = 25;
int ininit;
char inchar;


public KeyMonitor(Object someServer)
{



try
{ sleep(sleeptime);
in = new InputStreamReader(System.in.read());
char inchar = (char) in;


}
catch(InterruptedException e) {System.out.println("Quitting Program");}



}





public void run()
{
while (running == true)
{
try
{

}
catch(IOException ioe) { }
}
}
}





}
the whole point is I am a newbie in java and not clear how to make the thread wake up every 25 secs and check for enter.
I have to use this code snippet that is a constraint for the assignment.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Java inputstream reader Posted: Aug 26, 2003 8:02 AM
Reply to this message Reply
in = new InputStreamReader(System.in.read()); 
char inchar = (char) in;


What you are trying to case in which is an InputStreamReader to a char which is not possible. You might have to a loop to read the data.
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while (str != null) {
            System.out.print("> prompt ");
            str = in.readLine();
            process(str);
        }
    } catch (IOException e) {
    }


If you are new to java I think the site below will be really useful.

http://www.javaalmanac.com/

and the example code I have pasted below comes from the link

http://www.javaalmanac.com/egs/java.io/ReadFromStdIn.html

Flat View: This topic has 3 replies on 1 page
Topic: submit form on another site and take my users to external confirmation page Previous Topic   Next Topic Topic: Help please! coding problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use