0

new to Java and learning how to collect user input from a string. This code is supposed to take user input of two words and divide it up by first word and second word. I repeatedly get the error: Enter input string: Exception in thread "main" java.util.NoSuchElementException, which I do not really understand...

  //Initializations
  Scanner scnr = new Scanner(System.in);
  String inputString = "";
  int commaPlacement = 0;

  String firstWord = "";
  String secondWord = "";

  //Collect user input
  System.out.print("Enter input string: ");
  inputString = scnr.next();
  commaPlacement = inputString.indexOf(",");


  //Create while loop
  while( (!inputString.equals("q")) ) {

     //Recollect input string if there is no comma
     if( (commaPlacement < 0) ) {
        System.out.println("Error: No comma in string");
        System.out.print("Enter input string: ");
        inputString = scnr.next();
     }

     //Continue if there is a comma in input string
     if( (commaPlacement >= 0) ) {
        Scanner inSS = new Scanner(inputString);
        firstWord = inSS.next();
        secondWord = inSS.next();
        System.out.println("First word: " + firstWord);
        System.out.println("Second word: " + secondWord);

        //Collect user input again
        System.out.print("Enter input string: ");
        inputString = scnr.next();
     }

  return;
  }

Is anyone able to help? Thank you in advance.

jkreish
  • 23
  • 4

0 Answers0