I'm kinda new to java and used to write in Python. So when it comes to getting User Input I'm allways annoyed by how many lines of Code I need to perform such task. So I tried to make my own class that simplifies that process. I wanted to perform something like this:
input("This is written in the console: ")
Inside the Console:
This is written in the console: |
Here is the code so far:
public static String input(String text) {
Scanner scanner = new Scanner(System.in);
System.out.print(text);
String x = scanner.nextLine();
scanner.close();
return x;
}
When I use the class once, everything works just fine, but when I try to use it again, I get an Exception:
public static void main(String[] args) {
input("Input: ");
input("Input 2: ");
}
Output:
Input: blaaa
Input 2: Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at NumberConverter.input(NumberConverter.java:124)
at NumberConverter.main(NumberConverter.java:7)
I really don't know why that keeps happening. Please help me, thanks.