I am learning Java and I wrote a method to update a record in a file. The problem I am having is when I ask the user if they would like to look for another file my reader is closed or is unable to assign any input to it.
protected boolean Update() throws InputMismatchException
{
RoomService Init =new RoomService();
Scanner input = new Scanner(System.in);
try {
boolean ans= true;
while(ans)
{
System.out.println("Please enter room number.");
String id = input.next();
Init.Update(id);
System.out.println("Press Enter to Add more or no to exit");
String choice = input.nextLine();// Skips this line
if (choice.equalsIgnoreCase(""))
{
continue;
}
else if(choice.equalsIgnoreCase("no"))
{
ans= false;
}
else
{
System.err.println("Wrong input");
throw new IOException();
}
}
} catch (IOException e) {
e.printStackTrace();
fail=true;
}
return fail;
}
Wondering what exactly is blocking me from entering anything I also used BufferedReader input = new BufferedReader(new InputStreamReader (System.in))
Thanks.
Edit:
Using Scanner Error is : java.util.NoSuchElementException
Using BufferedReader error is: java.io.IOException: Stream closed
If there is anywhere else in your code you are using a Scanner wrapped around System.in, make sure you do not call close() on it. A Scanner itself has no resources that need to be closed, unless you want to close the underlying input source, which for a Scanner wrapped around System.in, you don't, because that prevents all future input.
Related
I have two versions of Java code that gets user input until user types "q"
Version 1:
public class Test {
public static void main(String[] args) {
String input = "";
while (!input.equals("q")) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input: ");
input = scanner.nextLine();
System.out.println("Input was: " + input);
}
}
}
Version 2:
public class Test {
public static void main(String[] args) {
String input = "";
while (!input.equals("q")) {
try(Scanner scanner = new Scanner(System.in)){
System.out.print("Input: ");
input = scanner.nextLine();
System.out.println("Input was: " + input);
}
}
}
}
Version 1 works as expected but version 2 does not work as expected.
That is after reading user input for the first time, it produces an error
Input: 12
Input was: 12Exception in thread "main"
Input: java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at RealEstateCompany.main(RealEstateCompany.java:115)
My guess is since version 2 uses try with resource so it closes the scanner after being used and that is causing an error?
Thank you for your help in advance!
[Update]
Version 3:
public class Test {
public static void main(String[] args) {
String input = "";
try(Scanner scanner = new Scanner(System.in)){
while (!input.equals("q")) {
System.out.print("Input: ");
input = scanner.nextLine();
System.out.println("Input was: " + input);
}
}
}
}
Version 3 works. However, why version 3 is ok and version 2 is not ok?
Adding a little bit more detail to my comments
A try-with block is defined as follows:
try(...) {
...
}
where the argument in parenthesis needs to be an instance of java.lang.AutoCloseable. An example is the class java.io.InputStream, which is also the class for System.in.
A try-with attempts to automatically close its provided resource, once the block is left. Depending on the used resource, it closes all its own child resources as well.
Taking your example, you have try(Scanner scanner = new Scanner(System.in)), which uses Scanner as resource. The scanner itself uses System.in as resource. Once the try block is left (when } is reached) it tries to close its resources, which is the Scanner instance. This instance also tries to close its resource, the System.in.
Once System.in is closed, you can't get any input from the console anymore (at least not with some additional work, I think...).
Concretely, in your second example:
while (!input.equals("q")) {
try(Scanner scanner = new Scanner(System.in)){
...
} // <--- The block is left, scanner is closed, System.in is closed
} // <-- start a new iteration
Here after just one iteration, System.in gets closed. Sure, you create a new Scanner in the next iteration, but System.in remains closed, that's why you get your exception in this case.
Your third example:
try(Scanner scanner = new Scanner(System.in)){
while (!input.equals("q")) {
...
} // <-- start a new iteration, while still in the same try block
} // <-- only after the while, your resources are closed
Here you're looping your while, while still being inside try. So no resource gets closed, until you leave while and try. That means, the one Scanner remains intact and with it the one System.in. This allows you to keep reading from the console until you're done looping.
Try this:
String input = "";
try (Scanner scanner = new Scanner(System.in)) {
while (!input.equals("q")) {
System.out.print("Input: ");
input = scanner.nextLine();
System.out.println("Input was: " + input);
}
}
You can use every class thats implements Closeable or AutoCloseable in try-with-resources, When code reaches the end of the try call, It call close() function of the Scanner class in our example.
i run some tests and add the catch block into your code.here's the code
public static void main(String[] args) {
String input = "";
while (!input.equals("q")) {
try(Scanner scanner = new Scanner(System.in)){
System.out.print("Input: ");
input = scanner.nextLine();
System.out.println("Input was: " + input);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
when add the catch block,there are 2 kinds of results
1,only inputs q, works as expected
2,inputs any other String, exception
Input: java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at rews.pub.Test.main(Test.java:11)
when added the catch block, we will see that the program won't stop, because of the while loop
here is another easier test
public class Test {
public static void main(String[] args) {
String input = "";
Scanner scanner = new Scanner(System.in);
System.out.println("inout--1---");
input = scanner.nextLine();
scanner.close();
Scanner scanner2 = new Scanner(System.in);
System.out.println("inout--2---");
input = scanner2.nextLine();
scanner2.close();
}
}
and it goes same exception
inout--1---
11
inout--2---
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at rews.pub.Test.main(Test.java:15)
here's my opinion.
in the end of first run, try()block will close the resource which is in the block, means we close the system.in
system.in is a object of inputSteam,and system.in is final and static, we can't open it again like 'new Scanner(System.in)'
I am trying to output a file scanner object from my method. This is a school assignment and I am specifically instructed to NOT throw any exceptions, but use try/catch instead. The assignment requires that the command line prompt the user for a file to scan. If the file does not exist, we are supposed to tell the user, then prompt them for a file again. If the file does exist, then the method returns a scanner object that scans the file.
My code works, but it is not clean. It involves 2 methods. This is my code so far:
public static Scanner getInputScanner (Scanner console) {
File inputFile = null;
Scanner input = null;
try {
inputFile = getFile(inputFile, console);
input = new Scanner (inputFile);
return input;
} catch (FileNotFoundException e) {
try {
return input = new Scanner (getFile (inputFile, console));
} catch (FileNotFoundException f) {
System.out.println("An error has occured.");
return input;
}
}
}
public static File getFile (File inputFile, Scanner console) {
System.out.println("Enter input file: ");
inputFile = new File (console.nextLine());
while (!inputFile.exists()) {
System.out.println("File does not exist.");
System.out.print("Enter input file: ");
inputFile = new File (console.nextLine());
}
return inputFile;
}
The problem with the code is that the output looks like this:
Enter input file:
File does not exist.
Enter input file:
It then is waiting for the user's input. I don't want the output to have the 2 lines of code before the last line though.
Can anybody explain why my code is outputting these 2 lines?
Also, is there a simpler solution to getting an input file without throwing the FileNotFoundException?
Thanks!
If I understand correctly,
your program outputs these lines when you run it,
no matter what,
without you getting a chance to actually enter a filename.
Enter input file:
File does not exist.
And then the programs asks you again:
Enter input file:
And you don't want the first two lines above, right?
This can happen for example if the Scanner console you received has an unread newline in it.
You haven't posted that part of the code,
so it's hard to tell, but this is a common gotcha with Scanner.
Before calling getInputScanner,
make sure the Scanner console is ready to use,
with no unread garbage still buffered in it.
As for the second part of your question,
yes this can be written simpler and better, for example:
public static Scanner getInputScanner(Scanner console) {
try {
File inputFile = getExistingFile(console);
return new Scanner(inputFile);
} catch (FileNotFoundException e) {
throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
}
}
public static File getExistingFile(Scanner console) {
while (true) {
System.out.println("Enter input file: ");
File inputFile = new File(console.nextLine());
if (inputFile.exists()) {
return inputFile;
}
System.out.println("File does not exist.");
}
}
It execute below line as soon the getFile() being called.
System.out.print("Enter input file: ");
Since no file exist, the below lines keeps on executing :
while (!inputFile.exists()) {
System.out.println ("File does not exist.");
System.out.print("Enter input file: ");
You can use throws() instead of try/catch, then caller will take care of exception.
Had to consume whatever junk was being carried over from the scanner by inserting a Scanner.nextLine() before getting user input. Final code looks like this:
public static Scanner getInputScanner(Scanner console) {
try {
File inputFile = getExistingFile(console);
return new Scanner(inputFile);
} catch (FileNotFoundException e) {
throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
}
}
public static File getExistingFile(Scanner console) {
while (true) {
console.nextLine();
System.out.println("Enter input file: ");
File inputFile = new File(console.nextLine());
if (inputFile.exists()) {
return inputFile;
}
System.out.println("File does not exist.");
}
}
I'm developing an application that has to receive multiple pieces of input from the user's terminal, while elegantly handling invalid input and prompting the user to re-enter it. My firs thought would be to have a while loop whose body will take the input and verify it's validity, setting a flag when it gets valid input. This flag will mark the stage the application is at and will determine what type of input is required next, and will also be used as the terminating condition of the loop.
While functional, this seems rather inelegant and I was wondering if there was a way I could simply write a function that is called whenever the return key is pressed to indicated that there is new input to be parsed. Something along the lines of
public class Interface {
public void receiveInput( final String input ){
// Parse 'input' for validity and forward it to the correct part of the program
}
}
Perhaps this could be achieved by extending some Java class and reimplementing one of it's functions that would normally handle such an event, but that's perhaps my C++ background talking.
I'm not allowed to use any external libraries, other than those requires for building and unit testing.
While reading from the console you can use BufferedReader
BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
and by calling the readLine function, it will handle new line :
String readLine = br.readLine();
You can sure have a class in which there would be a function which reads the information and continue.
Here is the sample code for your reference
public class TestInput {
public String myReader(){
boolean isExit = true;
while (isExit){
System.out.print("$");
BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
try {
String readLine = br.readLine();
if (readLine != null && readLine.trim().length() > 0){
if (readLine.equalsIgnoreCase("showlist")){
System.out.println("List 1");
System.out.println("List 2");
System.out.println("List 3");
} if (readLine.equalsIgnoreCase("shownewlist")){
System.out.println("New List 1");
System.out.println("New List 2");
} if (readLine.equalsIgnoreCase("exit")){
isExit = false;
}
} else {
System.out.println("Please enter proper instrictions");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Finished";
}
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("Please Enter inputs for the questions asked");
TestInput ti = new TestInput();
String reader = ti.myReader();
System.out.println(reader);
}
Here is the output:
Please Enter inputs for the questions asked
$showlist
List 1
List 2
List 3
$shownewlist
New List 1
New List 2
$exit
Finished
Hope this helps.
I have a thread in which data is scanning from standard input stream as:
1. Scanner scan = new Scanner(System.in);
2. while(true)
3. {
4. String str = scan.nextLine();
5. System.out.println(str);
6. }
I want to stop this thread by another thread. I heard it is not good to use Thread.stop(). Using some boolean flag also will not solve it as the pointer (program execution step) will be always at line 4. String str = scan.nextLine();
Is there any way that I can stop the thread?
I think this article will help you, it comes with a code example that solves the problem.
You could either use that code directly, or write a new closable InputStream class, wrapping up the logic described in this article.
public class ConsoleInputReadTask implements Callable<String> {
public String call() throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("ConsoleInputReadTask run() called.");
String input;
do {
System.out.println("Please type something: ");
try {
// wait until we have data to complete a readLine()
while (!br.ready()) {
Thread.sleep(200);
}
input = br.readLine();
} catch (InterruptedException e) {
System.out.println("ConsoleInputReadTask() cancelled");
return null;
}
} while ("".equals(input));
System.out.println("Thank You for providing input!");
return input;
}
}
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.
Here is what I have so far, the fileName variable has been declared outside the main function
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");
}
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?
Many thanks.
If you flush after each write, you should at least get a complete file when the user hits control-c.
As for processing more information after control-c happens, you can do that by using a shutdown hook such as:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
// do something before quiting
}
});
however, i don't know that you can cancel the termination.
I would choose a more normal character input for the 'no more input' action.
You can catch the Ctrl+C signal using a SignalHandler. Although I wouldn't recommend this as it would make it difficult for the user to exit your application. Instead you could stop input when the user enters nothing, or use a different command signal.
You need to agree with a command indicating that user is done (for example "EOF") and add the following in your while loop:
while (line != null) {
System.out.print ("Enter move: ");
String r = inputReader.readLine();
if ( r != null ) {
if ( "EOF".equals(r) ) break;
}
out.write();
out.write(" ");
}