Note that there are some explanatory texts on larger screens.

plurals
  1. POJava interactive input EOF
    primarykey
    data
    text
    <p>I'm trying to implement a user input interface for a board game. I'm trying to get user input one at a time and then writing it to a file (since I need to save the list of moves made by the user). What I have so far, works well (reading input and writing it to file), however, whenever the user wants to stop inputting, the program just stops working. I.E; when you press ctrl+c, the program just ends.</p> <p>Here is what I have so far, the fileName variable has been declared outside the main function</p> <pre><code>public static void main(String[] args) throws IOException { BufferedReader inputReader = new BufferedReader (new InputStreamReader (System.in)); try { FileWriter outFile = new FileWriter (fileName); PrintWriter out = new PrintWriter (outFile); System.out.print ("Enter move: "); String line = inputReader.readLine(); while (line != null) { System.out.print ("Enter move: "); out.write(inputReader.readLine()); out.write(" "); } out.close(); } catch (IOException e) { System.out.println (e.getMessage()); } System.out.println ("Reached here"); } </code></pre> <p>What I'm trying to do is whenever the user wants to stop inputting, I want to get to the print line where it says "Reached here". I want to do this because once outside of the loop, I can read the file and then split the input and maniplate it. I remember whilst programming in C, there used to be while (input != EOF); where whenever the user entered ctrl+d or ctrl+c, it stops whatever it is doing and then moves onto the next line of code. How can I do this in java?</p> <p>Many thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload