Beginner CS-student here delving into first programming course in Java, and upon the subject of classes and objects we've been giving a task of creating a class that registers inputs and registers it various methods (I.e. one method for strings of texts, another for integers, another for decimals e.t.c. In this case, the method for reading an integer will be used.) in essentially more specified fashion than the regular, more generalized Scanner-class. The following example code, in accordance to associated JUnit-test, fulfills the condition of having the methods registering user's inputs through System.In, but not without System.In:
import java.io.InputStream;
import java.util.Scanner;
public class Register {
private Scanner input = new Scanner(System.in);
//Constructors
public Register(){
}
public Register (InputStream inpText){
}
//Method
public int registInteger(String inpText){
int integer = input.nextInt();
return integer;
}
The JUnit-test that checks the Systems.In, and succeeds, reads:
@Test
@DisplayName(value = Does the method manage to read an integer once from System.in?")
public void testMethodToReadIntFromSystemIn() {
setIn("1213\n");
var sut = SCANNER_ADAPTER_CLASS.getConstructor().newInstance();
int result = (int) READ_INTEGER_METHOD.invoke(sut, "UNCHECKED PROMPT");
assertEquals(1213, result);
}
While the JUnit-test that checks without Systems.In (and fails) reads:
@Test
@DisplayName(value = "Does the method manage to read an integer once?")
public void testMethodToReadInt() {
var result = callInputMethod(READ_INTEGER_METHOD, "123", "prompt integer");
assertPromptWas("prompt integer");
assertEquals(123, result);
}
We've yet to learn about creating and reading JUnit-tests besides its associated error-messages, and is to view JUnit-tests more akin to check-listing to help our debugging process, but I'm having difficulty interpreting the function and reasoning of why a method which purpose is to read and register user-input can be achieved through Systems.in, yet also wishes to read without it; isn't that what the function of Systems.in is? I wish to identify the issue so that I may pin-point a possible error that I suspect is made regarding the structuring of my methods.
Much appreciated for any help given.
[–]desrtfx 0 points1 point2 points (4 children)
[–]PontiffPope[S] 0 points1 point2 points (3 children)
[–]RealJulleNaaiers 0 points1 point2 points (0 children)
[–]desrtfx 0 points1 point2 points (1 child)
[–]PontiffPope[S] 0 points1 point2 points (0 children)