public class HelloWorld{
public static void main(String [] args){
boolean found = false;
inputInfo = "do";
String temp = "yes/no/do/you";
Scanner scan = new Scanner(temp); // caseS scanner class
scan.useDelimiter("/"); // Delimiter
String[] caseArray = new String[100];
while (scan.hasNext()) {
for (int i = 0; i < caseArray.length; i++) {
caseArray[i] = scan.next();
}
for(int j = 0; j< caseArray.length; j++) {
if(caseArray[j].equals(inputInfo)) {
found = true;
break;
}
}
if(found){
System.out.print("product found");
} else{
System.out.print("product not found");
}
}
}
}
I have a variable called temp which stores a String value, which is separated by a Delimiter("/"). I would like to go through all the elements in the String and print "product found" if the element exists and print "product not found" if the elements doesn't exists.
While running the program I got an error like:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
Could someone tell as to why this error occurs and how to correct it?