In my program I\'m reading integers form console many times. Every time, I need to type this line.
new Scanner(System.in).nextInt();
I\'m
Use a utility class and static import.
The utility class:
package my.utils;
public class ScannerUtils {
private ScannerUtils() {}
public static int readInt() {
return new Scanner(System.in).nextInt();
}
}
Using the utility class:
package my.examples;
import static my.utils.ScannerUtils.*;
class Example {
void foo() {
int i = readInt();
}
}
As others have said, you should probably cache your scanner, but that is a separate topic.