Reading char from keyboard to stop threads?

I want to kill all the threads an terminate my program execution when I enter some char, for example 'S'.
BUT, I dont want the execution flow to wait in the sentence 'prompt.read()' until I enter some character.
does somebody knows the solution?
this is my program:
import java.io.*;
public class Finalizadora extends Thread {
     public static boolean ejecucion = true;
     public void run() {
          while (ejecucion) {
               try {
                    sleep(1000);
               } catch (Exception e) {
                    // Si falla el sleep lo simulamos
                    for (int i = 0; i < 1000 * 10; i++) {
                    } // fin del for
               } // fin del try
               try {
                    BufferedReader prompt = new BufferedReader(new InputStreamReader (System.in));
                    if (prompt.read() == 'S') {
                        ejecucion = false;
                    } // fin del if
               } catch (Exception e) {
                    System.out.println("SOME FAILS!!");
               //System.out.println("--------------------");
          } // fin del while
          System.out.println("ENDING...");          
     } // fin de parsear
} // fin de Finalizadorathanks!

Hello.I cant read chars from a file.I am trying to fing to character with Max Occurrence in a file but when I run it it stops abruptly.This is my code.Can you help me please.Thank you
* To change this template, choose Tools | Templates
* and open the template in the editor.
* @author Dimitrios
/*Assuming that the char with the max frequency in an standard English test is 'E' we can guess
* that the char with the max frequency in the cipher text is E.This will
* lead us to an approximation of the Key by shifting */
import java.io.*;
public class Frequency_Decprypt {
public static int Occurrence(String str, char ch) {
int occur=0;//the occurrence of char ch in the string
for(int i=0;i<str.length();i++){
if(str.charAt(i)==ch){
occur++;
return occur;
public static char Frequency() throws IOException{
BufferedReader in;
FileReader fr;
int key=0;
char Max_FreqChar=' ';//the char with the max frequency
int Max_Freq=0;// the max frequency of a char
try{
fr=new FileReader("Encrypted.txt");//the input file to read
in=new BufferedReader(fr);
//read a line from file
String s=in.readLine();
while(s!=null){
for(int i=0;i<s.length();i++){
if(Occurrence(s,s.charAt(i))>=Max_Freq){
Max_Freq=Occurrence(s,s.charAt(i));
Max_FreqChar=s.charAt(i);
s=in.readLine();
fr.close();
//key=(int)('e'-Max_FreqChar);
//key=Math.abs(key);
} catch (FileNotFoundException e)
{ System.out.println("File not found");}
return Max_FreqChar;
}

Similar Messages

  • How to read char from console

    Hi can any body help me..
    I want to read char from keybord. withought pressing ENTER.
    I am not sure how i can do it. I can't use
    BufferedReader br = new  BufferedReader(new InputStreamReader ( System.in )) ;
    char key ;
    key = (char )br.read() ;
    cuz i have to press ENTER every time to read char.
    Thanks in advance.

    try this,I' have found a part on Internet
    package exercises;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    * questa � la classe d' esempio per leggere l'input dalla console*/
    public class Echo {
    public static void main(String args[]) throws Exception{
    // This is where the magic happens. We have a plain old InputStream
    // and we want to read lines of text from the console.
    // To read lines of text we need a BufferedReader but the BufferedReader
    // only takes Readers as parameters.
    // InputStreamReader adapts the API of Streams to the API of Readers;
    // receives a Stream and creates a Reader, perfect for our purposes.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String input = "";
    while(true){
    System.out.print("ECHO< ");
    //As easy as that. Just readline, and receive a string with
    //the LF/CR stripped away.
    input = in.readLine();
    //Is a faster alternative to: if (input == null || input.equals(""))
    //The implementation of equals method inside String checks for
    // nulls before making any reference to the object.
    // Also the operator instance of returns false if the left-hand operand is null
    if ("".equals(input)){
    break;
    }else
    // Here you place your command pattern code.
    if ("ok".equals(input)){
    System.out.println("OK command received: do something �");
    //Output in uppercase
    System.out.println("ECHO> " + input.toUpperCase());
    System.out.println("ECHO> bye bye");
    //We exit without closing the Reader, since is standard input,
    // you shouldn't try to do it.
    // For all other streams remember to close before exit.
    }

  • Best way to read chars from InputStream

    Hope this is not a too newbie question.
    Suppose I have an unbuffered InputStream inputStream, what is the best way to read chars from it (in terms of performance)?
    Reader reader = new BufferedReader(new InputStreamReader(inputStream));
    reader.read()
    or
    Read reader = new InputStreamReader(new BufferedInputStream(inputStream))
    reader.read()
    Is there a difference between the two and if so, which one is better?
    thanks.

    If you are reading using a buffer of your own, then adding a buffer for binary data is a bad idea.
    However for text, using a BufferedInputStream could be better as it reduces calls to the OS.
    If it really matters, I suggest you do a simple performance test which runs for at least a few seconds to see what the difference is. (You should runt he test mroe than once)
    Edited by: Peter__Lawrey on 20-Feb-2009 21:37

  • How can I read something from keyboard using i/o operations?

    I wrote the following code in java using *"oracle Jdeveloper 11g release 2"* platform and i think it's correct, but I have a little problem when I try to execute him, it asks me to introduce a number, and this it's correct, but I don't know how to introduce that number. If i want to write the same code in c or c++ using Visual Studio it's very easy when i want to introduce a number i must run the code and then a console window will appear and then i introduce the number.
    Could someone to give me a link at a tutorial about how to use *"oracle Jdeveloper 11g release 2"* when I have to use i/o operations or something useful.
    package instante;
    import java.io.*;
    public class Read_from_keyboard {
    public static void main(String[] args) {
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in), 1);
    double number_read = 0.0;
    String line = "";
    try {
    System.out.flush();
    System.out.println("Give a number: ");
    line = keyboard.readLine();
    Double tmpDouble = Double.valueOf(line);
    number_read = tmpDouble.doubleValue();
    System.out.println("The number is: " + number_read);
    } catch (IOException e) {
    System.out.println(" Input from keyboard " + e.toString());
    System.exit(1);
    Thank you!
    Edited by: Iosif on Nov 9, 2012 5:06 AM

    Its a command line application. Perhaps JDeveloper has support built in to be able to do that in the IDE, but generally you'd just want to run a command line application on the command line / shell. How to do that is described in any Java book, in the Oracle Java tutorials and in just about a couple hundred/thousand articles you can find through Google.
    Other than that, the JDeveloper forum is here:
    JDeveloper and ADF

  • Read char from text file

    I am trying to read in questions, possible answers and the correct answer from a text file.
    The text file looks like this
    1. what is blah blah blah?
    a. something
    b. something else
    c. none of the above
    b
    where the last line of the file is a character which is the correct answer.
    I can read all these in as a string but I have to read the last line in as a character .
    can you please help.

    I'm not sure if this is what you need but, take a look:
    import java.io.*;
    public class questions
    public static void main(String[] args)
    try
    Question a=new Question();
    ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(new File("answers.txt")));
    out.writeObject(a);
    ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("answers.txt")));
    Question b=(Question)in.readObject();
    char answer=b.getAnswer();
    System.out.println(answer);
    catch(IOException e)
    System.out.println(e);
    catch(ClassNotFoundException e)
    System.out.println(e);
    class Question implements Serializable
    String q;
    String a1;
    String a2;
    String a3;
    char c;
    public Question()
    q=new String("The Question");
    a1=new String("1st possible choise");
    a2=new String("2st possible choise");
    a3=new String("3st possible choise");
    c='c';
    public void setQuestion(String que)
    q=que;
    public void setAnswer1(String an1)
    a1=an1;
    public void setAnswer2(String an2)
    a2=an2;
    public void setAnswer3(String an3)
    a3=an3;
    public void setAnswer(char an)
    c=an;
    public char getAnswer()
    return c;
    Ofcource all methods in Question class should be adapted to the needs of your program.
    If i helped help me too.. (duke $)

  • How to Read string from keyboard input??

    Hi,
    I am writing an application that requires the user to input string
    text. I have declared a variable String[] SData. The user will enter
    several words in a line of string, and I want to be able to split
    these and assign it to different variables, e.g. A= SData[1]
    My problem is how do you assign the SData to what is being inputted?
    I was using SData = System.in.read()
    But I got a type compatibility error.
    Is there another way I can get user input into a string?
    Does anyone have any ideas? Any help would be greatly appreciated.
    Thank you.

    Is there another way I can get user input into a
    string?
    KeyboardInput
    Adapted from Jacquie Barker's "Beginning Java Objects" (2002)
    pp. 324 - 325
    To use, in main():
    KeyboardInput k = new KeyboardInput();
    k.readLine();
    To get the input stored in k,
    k.getKeyboardInput();
    import java.io.*;
    class KeyboardInput
    //User input is saved in this string
    private static String keyboardInput;
    public String readLine()
    char in;
    // Clears previous input
    keyboardInput = "";
    try
    // Read one integer and cast it into a character.
    // Keeps going until a newline character is read.
    do
    in = (char) System.in.read();
    keyboardInput = keyboardInput + in;
    } while ( in != '\n' );
    catch (IOException e)
    // Reset the input
    keyboardInput = "";
    System.out.print("\n\nError in reading keyboardInput: \n" +
    e + "\n\n");
    // Strips off any leading and/or trailing whitespace
    keyboardInput = keyboardInput.trim();
    // Return the complete String;
    return keyboardInput;
    } // End String readLine()
    public String getKeyboardInput()
    return keyboardInput;
    } // End String getKeyboardInput()
    } // End class KeyboardInput

  • Reading integers from keyboard

    Hi,
    To my understanding,always we have to use the BufferedReader along with the InputStream reader to read the stream(here the stream is the keyboard).
    Now this will be a string input taken from it.To make it an integer,we have to use the Integer.parseInt() method thus it is integer.
    Is this the right method to read integers.Or is there someother method.Any help would
    Thanks
    AS

    Merriam Webster:
    Main Entry: doubt
    Function: noun
    1 a : uncertainty of belief or opinion that often interferes with decision-making b : a deliberate suspension of judgment
    2 : a state of affairs giving rise to uncertainty, hesitation, or suspense
    3 a : a lack of confidence : DISTRUST b : an inclination not to believe or accept

  • Read string from keyboard (similar to scanf)

    Dear all,
    I am trying to implement a high-score table, i.e. if some criteria is met, the user has to input a string, which is then written to a file. 
    Is there a way to implemet a text query at a certain stage in the flow in labview?
    Any help is greatly appreciated.
    Best wishes
    Chris

    I would make a new VI as a dialog box to make the user input data into a string control.  Use dataflow to "pause" the program while the dialog is up.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Reading char fail

    Hi everybody!
    Reading one char at a time from a file and saves where i last read.
    Next time when i start reading chars from where i last read in the file i
    only get char "?", and it reads chars like that the entire time. Any advice ?
    br.skip(last_read_in_file);
    while(readed_char != '\n'){
         if((int)(readed_char = (char) br.read()) != -1){
              if(readed_char != '\n'){
                   line_x = readed_line;
                   readed_line = new char[readed_line.length + 1];
                   for(int i=0;i<line_x.length;++i)
                        readed_line[i] = line_x;
                   readed_line[readed_line.length - 1] = readed_char;
         if(read_no_of_chars >= size_of_file){
              readed_char = '\n';
         System.out.println("readed_char: " + readed_char);
         ++read_no_of_chars;

    The FIRST time i read the file there is no problem when i'm reading chars.
    The SECOND time it returns "?" or 65535. There has to be something with skip(long n) ?
    fx = new File("C:\\Temp\\Log.txt");
    // Get the size of the file.
    noOfChars = (int)fx.length();
    // If last_read_in_file is smaller then the size of
    // of the file, it means that new errors
    // has been added to the file. Otherwise,
    // jump out of this error-detecting function.
    if (last_read_in_file >= noOfChars) {
         logger.info(fx.getName() + " - done, no changes in file.");
         return;
    // Initiate file-readers.
    in = new FileInputStream(fx);
    fr = new InputStreamReader(in, charset);
    br = new BufferedReader(fr);
    // Jump to the latest read line.
    read_no_of_chars = last_read_in_file;
    try {
         in.skip(last_read_in_file);
         fr.skip(last_read_in_file);
         br.skip(last_read_in_file);
    } catch (IOException e1) {
         e1.printStackTrace();
    // Reading a line from the file which, perhaps,
    // contains errors. In this while-loop a char is
    // being read one at the time and is being added
    // in a char-array. Thanks to the CharSet, when
    // we're at the end of the file, it won't
    // return the value -1. So instead, we have a
    // word-counter that we compare with 'noOfChars'
    // to see if we're at the end of the file.
    while(readed_char != '\n'){
         if((int)(readed_char = (char) br.read()) != -1){
              if(readed_char != '\n'){
                   line_x = readed_line;
                   readed_line = new char[readed_line.length + 1];
                   for(int i=0;i<line_x.length;++i)
                        readed_line[i] = line_x;
                   readed_line[readed_line.length - 1] = readed_char;
         if(read_no_of_chars >= noOfChars){
              readed_char = '\n';
         System.out.println("readed_char: " + readed_char + " (" + (int)readed_char + ")");
         ++read_no_of_chars;
    temp = new String(readed_line);

  • Help: How can I read an integer from keyboard

    Hi,
    I am new to java, and I know how to declare integer variable,
    but how can read an integer input from keyboard and assign it
    to the variable.
    Can anyone help me on this.
    Thanks
    Studentfj

    the easiest way is to
    import javax.swing.*
    and use the methods and functions in there
    otherwise from command line i think as far as i remember it is System.in

  • How to read input from handheld scanner on a keyboard port

    Hi everybody,
    i have an application that should read input from a scanner.
    If i use a jTextField to input scanned data it works fine.
    the problem is that i don't want to use any text component to input data into it.
    And if i set the jtextField not visible then the focus sustem doesnt work.
    I tried to read a buffered input stream from System.in but it doesnt work.
    If the scanner was installed on a serial port i could use javax.comm to monitor it.
    But unfortunately i must use the keyboard wedge thing...
    Do you have any ideas?
    Thank you in advance

    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))
    null

  • When moving from a list of threads view to a thread would be nice to jump to last thread read.

    When moving from a list of threads being viewed to a thread, it would be nice to jump to last thread read or first unread thread.
    Sometimes the threads get rather long.  Sometimes I haven't read a thread in awhile & I forget where I have left off reading a thread. It would be nice to have an icon to click on to move to your last read post or the first non-read post. 
    Seems this could be implemented in javascript without too much problem.  Might be a problem of when to get rid of the tracking data.
    Robert

    This would be a new capability.   I didn't know you could jump directly to the last post in a thread by clicking on the posters name in your discussions.  However, I am interested in jumping to the first post that I haven't read.
    Since writing my original post, I realize that software doesn't know what posts I have read. Lets say there are are 26 posts in a thread.  Lets assume these are divided into three pages with at most 10 items in a page:
    page 1 -- a, b, c, d, e, f, g, h, i, j
    page 2 -- k, l, m, n, o, p, q, r, s, t
    page 3 -- u, v, w, x, y, z
    The software know that I have viewed the first three pages.  It doesn't know what post on the pages I have read.  The software would need to assume I have read all the available posts on a page.
    Now lets assume that 12 more post were added.  This results in:
    page 1 -- a, b, c, d, e, f, g, h, i, j
    page 2 -- k, l, m, n, o, p, q, r, s, t
    page 3 -- u, v, w, x, y, z, aa,bb,cc,dd
    page 4 -- ee,ff,gg,hh,ii,jj,kk,ll
    It would be nice if I could go to aa. 
    Jumping to the last post in the thread doesn't hack it. You have to spend a lot of time finding where you left off your reading.
    Intesting, the list of your posts on your homepage usings the term Last Activity were as Your Discussions uses the term Last Post. The terminalogy should be the same & should act the same.  For the existing capability of going the the last post, I was thinking of Goto Last Post.

  • Help with reading in command from keyboard

    i need to read user response from the keyboard, how should this be done with system.in. or should i used JOptionPane to show input dialog?

    Depends on what you want to do.
    using System.in:
    import java.io.*;
    public class rin
    public static void main(String[] args)
    DataInputStream ins=new DataInputStream(System.in);
    try
    String cmd =ins.readLine(); //Reading from Keyboard
    System.out.println(cmd);
    catch (IOException e)
    System.out.println(e);
    }

  • How do I stop Adobe Reader X from reading documents meant for Adobe Digital Editions?

    I  was trying to download via  Sample e-Book Library, just to check if my Adobe Digital Editions was working properly.
    Turns out, PDFs / Urls are interrrupted by Adobe Reader X.
    Even though  Adobe Reader X is unable to read it, or as it  keeps telling me: "Adobe reader could not open URLLINK.acsm because it is either not a supported file type, or because the file has been damaged"
    Any suggestions to how I could abort Adobe Reader X from automatically read documents meant for Adobe Digital Editions?

    It's Windows 8.
    Turns out the problem was acsm.files preconfigured to be opened in Adobe Reader. The solution was to right click on the file(s) in Downloads, and then reconfigure  it through the option "open as/in ..."
    and somehow give the correct adress, through Computer:/ C and so on,  to the  Adobe Digital Editions.

  • HELP! In middle of upgrade to windows 8 and keyboard/trackpad stopped working!

    Hey all, new mac user here. Just bought a new macbook air 13" 2014 model. I succedded in installing windows 7 via bootcamp via a usb drive and was in the middle of installing a downloadable windows 8 upgrade (at a preferences page when they ask you to select your favorite color) when my macbook's keyboard/trackpad stopped functioning. I plugged in a usb mouse and that would not work either. I have absolutely no clue as to what to do next. If I had to install some driver or thing please tell me how to cancel the windows 8 install and return to windows 7. Thanks for all your help strangers

    I was not able to find any drivers specifically for the web cam. I did find software to use the camera, but that was all I could find.
    However, I do have a question about the Windows 8 upgrade process that was used.
    Did you do an in-place upgrade or a clean install of Windows 8?
    Go back to device manager. Is there anything here at all in an other category indicating it is having some type of an issue?
    There are certain events that will need to happen.
    Go to the start screen and search for uninstall a program. Select this option in the settings category.
    This should take you to the Desktop app and show the familiar uninstall a program feature from Windows 7.
    Select add/remove Windows Components on the left.
    Is Framework 3.5 checked here? If not, enable this and install it.
    Then, install the HP framework which is needed for some HP programs. This is a Windows 7 installation, but it should go through.
    http://goo.gl/jDI7X
    Once that is finished, reboot the system.
    Let me know what happens.
    ↙-----------How do I give Kudos?| How do I mark a post as Solved? ----------------↓

Maybe you are looking for