I made a utility class with a method that is takes user input, and returns a lower case version of it so I don't have so much repetitive code, but it could only be used once. After that, it wouldn't scan. What can I do to fix this?
p.s. I have seen a lot of people saying it is a bad idea to reuse a scanner. Why is this? Shouldn't it be fine if the scanner is reset?
public String scan() {
String input;
Scanner s = new Scanner(System.in);
if (s.hasNextLine()) {
input = s.nextLine();
} else {
input = "ERROR";
}
s.reset();
s.close();
return input.toLowerCase();
}
You are correct every time you use the scanner its getting reseted. A solution to your problem would be the following:
Add all the parameters that you want to pass to scan() in an array and using a for loop pass them all to scan() while when returning, adding them again in their respective array position so then you can easily reuse them wherever you want.
You could actually reuse your Scanner instead of creating a new one each time you need to read a line , here is an example:
public class Test {
public static void main(String[] args) {
Test t = new Test();
//create your scanner object
Scanner s = new Scanner(System.in);
//send it as a parameter whenever you need to read a line
System.out.println(t.scan(s));
System.out.println(t.scan(s));
System.out.println(t.scan(s));
//close it after finishing
s.close();
}
public String scan(Scanner s) {
String input;
if (s.hasNextLine()) {
input = s.nextLine();
} else {
input = "ERROR";
}
return input.toLowerCase();
}
}
But if you make a reader utility class for you it's more logical to be an Instance variable in the utility class .
For Example:
public class Reader {
Scanner s ;
public Reader() {
s = new Scanner(System.in);
}
public String scan() {
String input;
if (s.hasNextLine()) {
input = s.nextLine();
} else {
input = "ERROR";
}
return input.toLowerCase();
}
public void close(){
s.close();
}
}
And when you use it , it will be like this :
public class Main {
public static void main(String[] args) {
//create your scanner object
Reader r = new Reader();
//send it whenever you need to read a line
System.out.println(r.scan());
System.out.println(r.scan());
System.out.println(r.scan());
//close it after finishing
r.close();
}
}
I have seen a lot of people saying it is a bad idea to reuse a
scanner. Why is this?
It is not a bad idea to reuse a scanner object in most cases. Without further context, it is hard to determine their reasons for saying that. It could've been they were mistaken. Maybe you reused it incorrectly when they stated that. Who knows?
One thing is certain: When you invoke Scanner#close() in an object that is reading from System.in, you are also closing the underlying input stream. And, once the input stream is closed, you cannot reopen it in the life of the application.
Shouldn't it be fine if the scanner is reset?
Scanner#reset() doesn't do what you think it does. In fact, in this code example it does nothing. This method resets the Locale to US, and the radix back to base 10.
Code Analysis
public String scan() {
String input;
Scanner s = new Scanner(System.in);
if (s.hasNextLine()) {
input = s.nextLine();
} else {
input = "ERROR";
}
s.reset();
s.close(); // BAD IDEA!!!!
return input.toLowerCase();
}
This method will only work one time for the reasons I already stated: "once the input stream is closed, you cannot reopen it in the life of the application." So, what you do instead? If you require to reuse this Scanner object over and over, it might be better to make it a global attribute of the class and you should never close it. This is obviously a bad idea. So, you might be better off using some other type of input stream wrapper.
Maybe this is why other people have told you in the past that reusing the scanner object was a bad idea. It is only speculation. But, judging from this code example, it seems like a very strong possibility.
Related
I wonder is it possible to store the current scan.nextLine() after comparing it to a String variable without creating any new variable? Language: Java
Scanner scan = new Scanner(System.in);
String words = "something";
if(!scan.nextLine().equalsIgnoreCase(words)) {
// replace words with `scan.nextLine();
}
No, you would need to write a new class, and Scanner is final = non-extendable.
It would be not a large effort though. Maybe you can use a BufferedReader instead.
Otherwise use an expression inside which one assigns to a single variable declared in front. The same pattern one sees with BufferedReader.
String line = "";
Scanner scan = new Scanner(System.in);
String words = "something";
if (!(line = scan.nextLine()).equalsIgnoreCase(words)) {
// replace words with `scan.nextLine();
... line ...
}
Not necessarily good taste.
Initially, another variable won't be made but when you access the method it will make a variable and store it but it will go on in the background, you won't have to make a new variable in your main class. Variable made in class don't occupy memory till an object is made out of it.
public class testing {
Scanner sc = new Scanner(System.in);
String str = "something";
String hello(String which_you_want_to_store) {
which_you_want_to_store = sc.nextLine();
if (!which_you_want_to_store.equalsIgnoreCase(this.str)) {
this.str = which_you_want_to_store;
}
return which_you_want_to_store;
}
public static void main(String[] args) {
testing obj = new testing();
obj.hello(null);
System.out.println(obj.str);
}
}
I've been teaching myself Java with http://www.cs.princeton.edu/courses/archive/spr15/cos126/lectures.html as a reference. They have a library called algs4 and it has several classes including StdIn, which I'm trying to implement below.
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class Tired
{
public static void main(String[] args)
{
//I thought this while statement will ask for an input
//and if an input is provided, it would spell out each character
while (!StdIn.hasNextChar()) {
StdOut.print(1); //seeing if it gets past the while conditional
char c = StdIn.readChar();
StdOut.print(c);
}
}
}
//This is from StdIn class. It has a method called hasNextChar() as shown below.
/*
public static boolean hasNextChar() {
scanner.useDelimiter(EMPTY_PATTERN);
boolean result = scanner.hasNext();
scanner.useDelimiter(WHITESPACE_PATTERN);
return result;
}
*/
If i run the code, it does ask for an input, but regardless of what i type in, nothing happens and nothing gets printed out.
I see that even StdOut.print(1); doesnt get printed out, so for some reason, it just gets stuck on while
It looks like the issue is with the condition for your while loop:
!StdIn.hasNextChar()
This says to continue as long as there isn't a next char. But you want to continue while there is one, so get rid of that ! and you should be good.
Here is some alternative code that works similarly. Not the best coding but works.
import java.util.Scanner;
public class test{
static Scanner StdIn = new Scanner(System.in);
static String input;
public static void main(String[] args){
while(true){
if(input.charAt(0) == '!'){ // use ! to break the loop
break;
}else{
input = StdIn.next(); // store your input
System.out.println(input); // look at your input
}
}
}
}
This question already has an answer here:
How to use multiple Scanner objects on System.in?
(1 answer)
Closed 26 days ago.
I have a main function in which I use scanner to read an integer from console.
Inside this main function, we can access another function which also uses scanner to read an integer. So, the program swings between these two functions many times. But, Java.util.scanner throws an exception. Is there any way to overcome this?
import java.util.Scanner;
public class dummy {
public static void main(String[] args) {
int buy;
Scanner sc = new Scanner(System.in);
buy = sc.nextInt();
user = dummy2();
sc.close();
}
static boolean dummy2(){
Scanner sc1 = new Scanner(System.in);
sc1.close();
}
}
First of all, it would make the question much easier to answer if you gave more information, such as the exception and its message, and maybe source code.
If the exception is a NoSuchElementException, the direct problem is that the function is closing the Scanner. When the scanner is closed, it also closes the underlying ImputStream. This makes all other Scanner on that input invalid.
If the exception is InputMismatchException, then the input is not an int.
If the exception is IllegalStateException, then the scanner has been closed, this could happen is the function and the main method are using the scanner, and one closes it.
However, you should not be taking user input in functions. This limits future use, say if you wanted to later add a GUI or make the same calculation based off a number not gotten from the user, then you would need rewrite the function. The function should take a int as a parameter, which the main method should get from the user. Only the main method and other methods directly relating to user input, such as the Scanner's methods, should read user input.
Use the same Scanner object.
import java.util.Scanner;
public class dummy {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int buy;
buy = sc.nextInt();
user = dummy2();
//Do more stuff with the same scanner
//close it when done
}
static boolean dummy2(){
//Scan stuff
int nbr = sc.nextInt();
}
I would suggest something like that:
import java.util.Scanner;
public class dummy {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int buy;
buy = sc.nextInt();
user = dummy2();
sc.close();
}
static boolean dummy2(){
//lets scan a string.
sc.nextLine();
}
}
Reusable objects! Isn't that nice?
I am trying to write this code for a class, but I don't want to use an array (String word[]). How do I change it so I use a regular method with parentheses?
Also, one of my friends helped and I am trying to learn, and I forgot what the alright(s); thing does. I tied to figure it out, but have failed. I think it creates and object for the scan, but I don't really know.
import java.util.Scanner;
public class WordLines{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
alright(s);
}
public static void alright(String s){
String word[]=s.split(" ");
for(int j=0;j<word.length; j++){
System.out.println(word[j]);
}
}
}
Thank you so much for the help!!! :)
One way to achieve similar results without the array is to use an additional instance of the Scanner class to parse the string:
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
Scanner parse = new Scanner(s);
while (parse.hasNext()) {
System.out.println(parse.next());
}
Link: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
I am trying to create some JUnit tests for a method that requires user input. The method under test looks somewhat like the following method:
public static int testUserInput() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Give a number between 1 and 10");
int input = keyboard.nextInt();
while (input < 1 || input > 10) {
System.out.println("Wrong number, try again.");
input = keyboard.nextInt();
}
return input;
}
Is there a possible way to automatically pass the program an int instead of me or someone else doing this manually in the JUnit test method? Like simulating the user input?
You can replace System.in with you own stream by calling System.setIn(InputStream in).
InputStream can be a byte array:
InputStream sysInBackup = System.in; // backup System.in to restore it later
ByteArrayInputStream in = new ByteArrayInputStream("My string".getBytes());
System.setIn(in);
// do your thing
// optionally, reset System.in to its original
System.setIn(sysInBackup);
Different approach can be make this method more testable by passing IN and OUT as parameters:
public static int testUserInput(InputStream in,PrintStream out) {
Scanner keyboard = new Scanner(in);
out.println("Give a number between 1 and 10");
int input = keyboard.nextInt();
while (input < 1 || input > 10) {
out.println("Wrong number, try again.");
input = keyboard.nextInt();
}
return input;
}
To test drive your code, you should create a wrapper for system input/output functions. You can do this using dependency injection, giving us a class that can ask for new integers:
public static class IntegerAsker {
private final Scanner scanner;
private final PrintStream out;
public IntegerAsker(InputStream in, PrintStream out) {
scanner = new Scanner(in);
this.out = out;
}
public int ask(String message) {
out.println(message);
return scanner.nextInt();
}
}
Then you can create tests for your function, using a mock framework (I use Mockito):
#Test
public void getsIntegerWhenWithinBoundsOfOneToTen() throws Exception {
IntegerAsker asker = mock(IntegerAsker.class);
when(asker.ask(anyString())).thenReturn(3);
assertEquals(getBoundIntegerFromUser(asker), 3);
}
#Test
public void asksForNewIntegerWhenOutsideBoundsOfOneToTen() throws Exception {
IntegerAsker asker = mock(IntegerAsker.class);
when(asker.ask("Give a number between 1 and 10")).thenReturn(99);
when(asker.ask("Wrong number, try again.")).thenReturn(3);
getBoundIntegerFromUser(asker);
verify(asker).ask("Wrong number, try again.");
}
Then write your function that passes the tests. The function is much cleaner since you can remove the asking/getting integer duplication and the actual system calls are encapsulated.
public static void main(String[] args) {
getBoundIntegerFromUser(new IntegerAsker(System.in, System.out));
}
public static int getBoundIntegerFromUser(IntegerAsker asker) {
int input = asker.ask("Give a number between 1 and 10");
while (input < 1 || input > 10)
input = asker.ask("Wrong number, try again.");
return input;
}
This may seem like overkill for your small example, but if you are building a larger application developing like this can payoff rather quickly.
One common way to test similar code would be to extract a method that takes in a Scanner and a PrintWriter, similar to this StackOverflow answer, and test that:
public void processUserInput() {
processUserInput(new Scanner(System.in), System.out);
}
/** For testing. Package-private if possible. */
public void processUserInput(Scanner scanner, PrintWriter output) {
output.println("Give a number between 1 and 10");
int input = scanner.nextInt();
while (input < 1 || input > 10) {
output.println("Wrong number, try again.");
input = scanner.nextInt();
}
return input;
}
Do note that you won't be able to read your output until the end, and you'll have to specify all of your input up front:
#Test
public void shouldProcessUserInput() {
StringWriter output = new StringWriter();
String input = "11\n" // "Wrong number, try again."
+ "10\n";
assertEquals(10, systemUnderTest.processUserInput(
new Scanner(input), new PrintWriter(output)));
assertThat(output.toString(), contains("Wrong number, try again.")););
}
Of course, rather than creating an overload method, you could also keep the "scanner" and "output" as mutable fields in your system under test. I tend to like keeping classes as stateless as possible, but that's not a very big concession if it matters to you or your coworkers/instructor.
You might also choose to put your test code in the same Java package as the code under test (even if it's in a different source folder), which allows you to relax the visibility of the two parameter overload to be package-private.
I managed to find a simpler way. However, you have to use external library System.rules by #Stefan Birkner
I just took the example provided there, I think it couldn't have gotten more simpler:
import java.util.Scanner;
public class Summarize {
public static int sumOfNumbersFromSystemIn() {
Scanner scanner = new Scanner(System.in);
int firstSummand = scanner.nextInt();
int secondSummand = scanner.nextInt();
return firstSummand + secondSummand;
}
}
Test
import static org.junit.Assert.*;
import static org.junit.contrib.java.lang.system.TextFromStandardInputStream.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.TextFromStandardInputStream;
public class SummarizeTest {
#Rule
public final TextFromStandardInputStream systemInMock
= emptyStandardInputStream();
#Test
public void summarizesTwoNumbers() {
systemInMock.provideLines("1", "2");
assertEquals(3, Summarize.sumOfNumbersFromSystemIn());
}
}
The problem however in my case my second input has spaces and this makes the whole input stream null!
You might start by extracting out the logic that retrieves the number from the keyboard into its own method. Then you can test the validation logic without worrying about the keyboard. In order to test the keyboard.nextInt() call you may want to consider using a mock object.
I have fixed the problem about read from stdin to simulate a console...
My problems was I'd like try write in JUnit test the console to create a certain object...
The problem is like all you say : How Can I write in the Stdin from JUnit test?
Then at college I learn about redirections like you say System.setIn(InputStream) change the stdin filedescriptor and you can write in then...
But there is one more proble to fix... the JUnit test block waiting read from your new InputStream, so you need create a thread to read from the InputStream and from JUnit test Thread write in the new Stdin... First you have to write in the Stdin because if you write later of create the Thread to read from stdin you likely will have race Conditions... you can write in the InputStream before to read or you can read from InputStream before write...
This is my code, my english skill is bad I hope all you can understand the problem and the solution to simulate write in stdin from JUnit test.
private void readFromConsole(String data) throws InterruptedException {
System.setIn(new ByteArrayInputStream(data.getBytes()));
Thread rC = new Thread() {
#Override
public void run() {
study = new Study();
study.read(System.in);
}
};
rC.start();
rC.join();
}
I've found it helpful to create an interface that defines methods similar to java.io.Console and then use that for reading or writing to the System.out. The real implementation will delegate to System.console() while your JUnit version can be a mock object with canned input and expected responses.
For example, you'd construct a MockConsole that contained the canned input from the user. The mock implementation would pop an input string off the list each time readLine was called. It would also gather all of the output written to a list of responses. At the end of the test, if all went well, then all of your input would have been read and you can assert on the output.