I've been given a task that I have to create a Shopping List program. I've done this in Python, and it was relatively straight forward. However, in Java I've hit a bit of a roadblock.
These are my variables, I am aware of the issues with using statics in this way and that it would be best to avoid doing it.
private static Scanner input = new Scanner(System.in);
private static String list_add = "string"; //"string" is just a place holder
private static ArrayList listFull = new ArrayList();
private static ArrayList listPos = new ArrayList();
private static int userIn = 1; //1 is also being used as place holder
Which I use in:
private static void userInput() {
boolean isValid = false;
while (!isValid) {
isValid = true;
try {
userIn=Integer.parseInt(input.next());
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number!");
isValid = false;
}
}
}
The reasoning behind doing it this way is the less one needs to type the quicker the task can be completed. Which was working as nice philosophy up until I tried this. My previous attempt to solve this problem gave an infinite loop, and the second solution that came to mind returned a StackOverflowError
. When I asked about avoiding the infinite loop, I was directed to another question (Endless loop while using "try and catch " block inside a "while loop") which I did not believe helpful to begin with, however found that it was (Thank you whoever marked that). I didn't get this solution to work, however, and I cannot see where I went wrong.
After trying different inputs to see if their was one specific type that killed it, these were the errors I received:
Test Case "strin":
Exception in thread "main" java.lang.NumberFormatException: For input string: "strin"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DebuggingMethods.AllIn(DebuggingMethods.java:17)
at DebuggingMethods.Menu(DebuggingMethods.java:33)
at DebuggingMethods.main(DebuggingMethods.java:58)
Test Case "Ten":
Exception in thread "main" java.lang.NumberFormatException: For input string: "Ten"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DebuggingMethods.AllIn(DebuggingMethods.java:17)
at DebuggingMethods.Menu(DebuggingMethods.java:33)
at DebuggingMethods.main(DebuggingMethods.java:58)
Test Case int(10):
Which ran with the anticipated outcome.
I had thought that I was missing a module, so I did import java.lang.*;
which did not change the error. If someone has a solution, please help me out. I can't find a question that already posted that explains what I am doing wrong. When I pulled it out of the Try-Catch it was working kind of.
Full Piece
import java.util.InputMismatchException;
import java.util.*;
import java.util.Scanner;
import java.lang.*;
public class TestOne {
private static Scanner input = new Scanner(System.in);
private static String list_add = "string";
private static ArrayList listFull = new ArrayList();
private static ArrayList listPos = new ArrayList();
private static int userIn = 1;
private static void userInput() {
boolean isValid = false;
while (!isValid) {
isValid = true;
try {
userIn=Integer.parseInt(input.next());
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number!");
isValid = false;
}
}
}
public static void main(String[] args) {
//titleMain();
userInput(); // For the sake of demonstration
}
}
See Question&Answers more detail:
os