Questions tagged [system.in]

Predefined stream object attached to the standard input in Java console applications.

It's a static variable in Java's System class. This tag is intended for questions about Java applications that read from the standard input.

191 questions
1
vote
1 answer

How can I prevent java.util.Scanner from throwing NoSuchElementException when reading from System.in?

I'm attempting to ask the user for a double value, using Scanner.nextDouble(); but I'm getting a NoSuchElementException before I have a chance to type anything. From what I can see in the documentation, this means that Scanner is at the end of the…
1
vote
0 answers

Java JUnit: test for user input (pass standard input to Scanner)

Issue: I'm trying to test a simple program in Java that requires user input but while I'm able to feed the input to the test, I'm only able to do so in the first called method. The error thrown is java.util.NoSuchElementException: No line found The…
GustavoM01
  • 11
  • 3
1
vote
1 answer

Redirected System.in ByteArrayInputStream is read one JUnit test too late

I am Currently writing Junit tests for automated testing of student tasks. The topic is console input in java. I have multiple tests that need to take simulated user input and check for correct behaviour of tested methods (method is working fine).…
MeepMania
  • 103
  • 2
  • 13
1
vote
1 answer

How to test code that uses readline (System.`in`) in kotlin?

How do I test code in kotlin that does a readline like below: import org.junit.jupiter.api.* import org.junit.jupiter.api.Assertions.* import java.io.* fun foo() { val string="a b\nc d" var bais:…
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
1
vote
2 answers

How can the hasNext methods of Scanner "not advance past any input"?

In the hasNext and hasNextXxx methods of java.util.Scanner it reads: Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input. How can the…
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
1
vote
4 answers

System.out.print("Calculator ") doesn't show an output when it is followed with a Scanner or BufferReader which takes a System.in in IntelliJ IDEA

So I want to make a command line calculator that takes several arguments after the word "Calculator ". But with the following codes I cannot achieve it. In both of those methods the gradle waits to read the line before displaying the word…
t T s
  • 75
  • 1
  • 10
1
vote
0 answers

Interesting behavior when simulating user console input when testing multithreaded Java application with Junit

I am trying to develop an automatic assignment grading script using JUnit in Java. I am simulating user input by passing in test case inputs (read from a file in the classpath) by changing System.in to a ByteArrayInputStream containing the string…
sbsatter
  • 591
  • 3
  • 22
1
vote
0 answers

How long does System.in.read() blocks in Java

System.in is a InputStream in Java. The api for the read() method says This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. try { int inByte = System.in.read(); …
mrbela
  • 4,477
  • 9
  • 44
  • 79
1
vote
1 answer

How System.in works?

I'm landed into doubt where System class is having static field in of InputStream type. InputStream is a abstract class. Java document says, System.in stream is already open and ready to supply input data. Abstract class cannot be initialized until…
Andrea
  • 109
  • 2
  • 10
1
vote
0 answers

How to read / get notprintable character from handheld scanner

My goal is to get a code from a 2D barcode read with an Handheld Scanner and to decode it. The code is compliant with GS1 128. The structure of the code can include "group separator" to separate parts of the code. I need to read this group separator…
Flint75
  • 11
  • 3
1
vote
2 answers

Are Java System.in/out/err always open?

Javadoc says in, out and err are 'already open', I just wonder that do they close after program finishes? If not does it contradict the practise to close any I/O stream references after use?
mzoz
  • 1,273
  • 1
  • 14
  • 28
1
vote
0 answers

Terminal input treated different when programm is executed by gradle

When I execute a simple programm in the linux terminal, everything works as exspected. Example with python3: print('input value') value = input() print('your value', value) When this script is executed by gradle, the behavior is different: Example…
1
vote
3 answers

is System.in an object reference of InputStream class?

InputStream is an Abstract class.Then how are we able to access System.in.And moreover int read() is an abstract method in InputStream class.Then how are we able to access System.in.read() method if read() is an abstract method. Like int…
Sourav Deb
  • 119
  • 8
1
vote
0 answers

How to system.in reopen in java?

When i use scanner.close(), System.in parameter is also closed. I can understand a little this process with searching this site. Some said if System.in is closed, it can't be reopened. But they didn't have conviction for that. So my question is…
Lucky Day
  • 11
  • 4
1
vote
2 answers

Junit test of method with Scanner and System.in (Java)

I'm new to programmming and I have this simple method: public double input() { double result = 0; Scanner scanner = new Scanner(System.in); if (scanner.hasNextDouble()) { result = scanner.nextDouble(); }…
aLittleMind
  • 183
  • 2
  • 3
  • 12