I'm doing a project for school, and my code throws java.util.NoSuchElementException at line 10 in the excerpt, even though I put TWO layers of hasNextInt (not necessary I know), so I'm not sure why it thinks there are no more elements in the string I'm scanning. I only made one scanner in the class, and somehow it says both that it has a nextInt and that it doesnt.
public String decode()
{
Scanner scanner = new Scanner(code);
String output = new String("");
while (scanner.hasNextInt())
{
if (scanner.hasNextInt())
{
int digits = String.valueOf(scanner.nextInt()).length();
if (code.indexOf(scanner.nextInt()) < key.length() - 3)
{
output += key.substring(scanner.nextInt());
}
else
{
System.out.println("key.length = " + key.length());
System.out.println("Current index: " + key.indexOf(scanner.nextInt()));
output += key.substring(scanner.nextInt(), scanner.nextInt() + digits);
code = code.substring(code.indexOf(scanner.nextInt() + digits));
}
}
}
return output;
}
public class Runner
{
public static void main(String[] args)
{
SecretCode coolCode = new SecretCode("six perfect quality black jewels amazed the
governor", "35 10 10 33 9 24 3 17 41 8 3 20 51 16 38 44 47 32 33 10 19 38 35 28 49");
coolCode.decode();
}
}