I'm trying to run my program in a loop and it runs perfectly fine the first time, but once it loops over and tries to find the nextLine, it crashes. My only issue with this is that I have a variable that is assigned by scanner.nextLine() then it delimits it. Shouldn't it ask for input every time instead of erroring?
private static List<String> readInput()
{
ArrayList<String> inputtedWords = new ArrayList<String>(); // creates an ArrayList called inputtedWords
Scanner kb = new Scanner(System.in); //creating a scanner kb (keyboard)
System.out.println("Text: "); // user input for Text:
String fullText = kb.nextLine();
Scanner s = new Scanner(fullText).useDelimiter(" "); //parses the fullText string and separates them on spaces
while(s.hasNext()) // loops until their is not another word
{
String tempStr = s.next();
if (!Character.isAlphabetic(tempStr.charAt(tempStr.length()-1)))
tempStr = tempStr.substring(0, tempStr.length()-1);
inputtedWords.add(tempStr);
}
s.close();//closing scanners
kb.close();//closing scanners
return inputtedWords; // returns the inputtedWords list
}
This is the code for the beginning of the loop...
List<String> dictionary = readDictionary(); // creates a List called dictionary and sets it equal to the file in method "readDictionary"
while (done) // Change this to a while loop and negate it. Remember to get another line of input at the end of the loop.
{
List<String> inputtedWords = readInput(); // creates another List for the words inputted into the method "readInput"
if ((inputtedWords.size() == 1) && (inputtedWords.get(0).equals("done")))
{
done = false;
System.out.println("Exiting Program");
continue;
}
for (String newWord : inputtedWords) // newWord = word inputted
{ // beginning of for loop, initializes element as a string then searches loops through the length of inputtedWords
List<String> possibleAnswers = new ArrayList<>(); //creates an ArrayList called "possibleAnswers"
possibleAnswers.add(newWord); // adds the newly created word into the possibleAnswers list
List<String> characters = new ArrayList<String>(); // creates another ArrayList called "chars"
I've been working on a fix for this for a while but cannot seem to figure out why it's erroring on me.