1

Feel like I'm missing something really basic here, but my brain is fried right now. Goal is to parse strings with a comma, gets through one loop and throws before the next line. Pretty sure it has something to do with userInput = scan.nextLine();

import java.util.Scanner;

public class ParseStrings
 {
 public static void main(String[] args)
  {
  Scanner scan = new Scanner(System.in);
  String userInput = "";
  boolean finished = false; 

  while (!finished) 
  {
     System.out.print("Enter input string: \n");

     userInput = scan.nextLine();

     if (userInput.equals("q"))
     {
        System.out.println("First word: " + userInput);
        finished = true; 
     } else
     {
        String[] userArray = userInput.split(",");
        System.out.println("First word: " + userArray[0]);
        System.out.println("Second word: " + userArray[1]);
        System.out.println();
     }
  }
  return;
 }
}

Throwing:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at ParseStrings.main(ParseStrings.java:12)

Thank you in advance

  • 1
    Cannot reproduce. How are you running your program? – OH GOD SPIDERS Dec 01 '17 at 16:14
  • Its through Zybooks – Edward Durrett Dec 01 '17 at 16:18
  • Is that an online IDE? Does it support input through scanner and system.in? – OH GOD SPIDERS Dec 01 '17 at 16:22
  • Are you getting this error consistently? Is this all of the code? Seems like input is being closed. You can also add while(!finished || !scan.hasNextLine()) to make sure that scanner is not closed. https://stackoverflow.com/questions/19828015/exception-in-thread-main-java-util-nosuchelementexception-no-line-found-usi – Yan Dec 01 '17 at 16:25
  • Added scan.hasNextLine to the while statement and it throws the same error. Yes it supports input through scanner and system.in – Edward Durrett Dec 01 '17 at 16:29
  • @EdwardDurrett actually it should be while(!finished && scan.hasNextLine()) what's your input? – Yan Dec 01 '17 at 16:52
  • @Yan Examples of input: Jane, Alice Blue,Dog. The problem definitely seems to be with the input closing. The while finishes when you have (!finished && scan.hasNextLine()) – Edward Durrett Dec 01 '17 at 19:32
  • @EdwardDurrett I can't reproduce the issue. Is this the full source code? Here is online copy of your code and it runs as expected https://repl.it/repls/MerryFantasticSpadefoot Just renamed the class to Main to be able to run it – Yan Dec 02 '17 at 03:44

0 Answers0