0

Hey everyone I am new to coding and I want to learn more. I have tried different approches from other codes I have seen relating to this problem. The error that displays is something I have no idea how to fix. Below is my code it keeps giving me an error message.

import java.util.Scanner;

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

      while (!inputDone) {
         System.out.print("Enter input string: \n");
         userInput = scnr.nextLine();


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


      return;
   }
}

My inputs are: Golden , Monkey Washington,DC Jill, Allen Enter input string: q

I am sorry if this is an easy fix.

Angel
  • 11
  • 6
  • This is the error code that popped up. 'Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at ParseStrings.main(ParseStrings.java:11)' – Angel Aug 14 '20 at 23:01
  • Please paste the entire stack trace into the question. Not everybody reads the comments. – NomadMaker Aug 14 '20 at 23:20
  • 1
    Does this answer your question? [java.util.NoSuchElementException: No line found](https://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found) – TimTIM Wong Aug 15 '20 at 06:48
  • Please write exactly your input and output inside \```. Are you typing it by hand or redirecting input? – Hawk Aug 15 '20 at 08:01

2 Answers2

0

You're feeding a file or some other limited source into standard in (versus just typing on the console, which is endless, and therefore cannot possibly result in NoSuchElementException on the nextLine() invocation), and you're passing along, e.g:

Golden, Monkey
Washington, DC
Jill, Allen
q

and crucially there is no newline after that q. nextLine will keep going until it sees a newline, and if end-of-stream arrives first, then there is technically no nextLine to give.

Easy solution: don't use nextline, use next() instead, and use scanner.useDelimiter("\r?\n"); to tell scanner that tokens are separated only by newlines (Versus the default, which is any whitespace).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Or even easier: type a newline after q – Hawk Aug 14 '20 at 23:07
  • No, @Hawk, that's not the problem. If you're typing on the console, that exception doesn't occur if you fail to hit enter - the program just seems to hang until you do. – rzwitserloot Aug 15 '20 at 04:20
  • What I meant was ending the redirected input file on an empty line. However, I have just tried the OP's code with your input in a file and it works. Probably OP uses input in a slightly different format. – Hawk Aug 15 '20 at 07:59
-1

in your code you are using String[] userArray = userInput.split(","); which will split the given string wherever it finds a, but if your entered string which doesn't has a , then the array will be empty and when you call it for indexing (userArray[0]) it will show an error since no split took place as no , was detected.

so there are two solutions:

first: don't use split with , use String[] userArray = userInput.split(" "); this will split the line based on spaces and hence the String array won't be empty.

second: put the String[] userArray = userInput.split(","); in try and catch i.e., if the user doesn't enter a , in his input the code won't show an error of empty array also put a suitable code in catch which deals with the userInput when the user input doesn't have a , in it.

Sahil Verma
  • 140
  • 1
  • 8
  • 1
    This is completely wrong. Try it yourself: `System.out.println(Arrays.toString("Hello".split(",")));` - this prints "Hello". splitting on a thing that isn't in the string returns 1 string in the array (containing all of the input). – rzwitserloot Aug 15 '20 at 12:33
  • the split function with a `,` is used to split a string of multiple words when there is a `,` in the string but if you want to separate alphabets of a word try `.split("")` and one more thing don't try doing what you did above in java, it's not python you cant print an array directly, iterate through the elements to print them. – Sahil Verma Sep 02 '20 at 20:52