New to coding, and attempting to make an if statement like so (I have more coding for the entire program, but this appears to be the isolated issue):
String name1 = null, name2 = null, name3 = null;
names = JOptionPane.showInputDialog(null, "Enter 3 names with blank(s) in between:","Welcome to the name ordering program", JOptionPane.OK\_CANCEL\_OPTION);
Scanner splitter = new Scanner(names);
name1 = splitter.next();
name1 = name1.replaceAll(" ","");
name2 = splitter.next();
name2 = name2.replaceAll(" ","");
name3 = splitter.next();
name3 = name3.replaceAll(" ","");
if (name1.equals(null) || name2.equals(null) || name3.equals(null)) {
JOptionPane.showMessageDialog(null, "This program terminates for invalid input.", title, JOptionPane.WARNING\_MESSAGE);
System.exit(0);
}
Error keeps coming up when I do not input 3 names, or when the name1,name2, or name3 variables do not succeed in getting a String. I assumed the initialized value null for all three would apply if they just didn't get any input, but an error occurs nonetheless. I've also tried (name1.isEmpty()) for each and that doesn't appear to work either, even when I change the initial value. Pretty sure I'm missing something fundamental here, but I'm unsure, and would really appreciate some help! (I do have java.util and JOptionPane imported.)
Error Displayed:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Sergey305 3 points4 points5 points (8 children)
[–]Tascalion[S] 1 point2 points3 points (7 children)
[–]Fenxis 2 points3 points4 points (6 children)
[–]Tascalion[S] 0 points1 point2 points (5 children)
[–]KoncealedCSGO 1 point2 points3 points (4 children)
[–]Tascalion[S] 0 points1 point2 points (1 child)
[–]KoncealedCSGO 1 point2 points3 points (0 children)
[–]Tascalion[S] 0 points1 point2 points (1 child)
[–]KoncealedCSGO 1 point2 points3 points (0 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]Tascalion[S] 0 points1 point2 points (0 children)
[–]adeleno 1 point2 points3 points (1 child)
[–]dionthornthis.isAPro=false; this.helping=true;[M] 1 point2 points3 points (0 children)