Home java java.util.NoSuchElementException while processing console input

java.util.NoSuchElementException while processing console input

Author

Date

Category

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine (Unknown Source)
at Factor.main (Factor.java:19)

Here’s the code:

import java.io. *;
import java.util. *;
public class Main {
  public static void main (String [] args) throws IOException {
    Scanner in = new Scanner (System.in);
    String a = in.nextLine ();
    String b = in.nextLine ();
    String c = in.nextLine ();
    String a1 = del (a);
    String b1 = del (b);
    String c1 = del (c);
    int n = in.nextInt ();
    String [] arrStud = new String [n];
    BufferedReader input = new
        BufferedReader (new InputStreamReader (System.in));
    for (int i = 0; i & lt; n; i ++) {
      String s = input.readLine ();
      s = del (s);
      if (s.toLowerCase (). contains (a1.toLowerCase ())
          & amp; & amp; s.toLowerCase (). contains (b1.toLowerCase ())
          & amp; & amp; s.toLowerCase (). contains (c1.toLowerCase ())
          & amp; & amp; s.length () == (a1.length () + b1.length () + c1.length ())) {
        arrStud [i] = "ACC";
      } else {
        arrStud [i] = "WA";
      }
    }
    for (int i = 0; i & lt; n; i ++) {
      System.out.println (arrStud [i]);
    }
  }
  public static String del (String s) {
    String n = new String ();
    for (int i = 0; i & lt; s.length (); i ++) {
      if (s.charAt (i)! = '-'
          & amp; & amp; s.charAt (i)! = '_' & amp; & amp; s.charAt (i)! = ';') {
        n + = s.charAt (i);
      }
    }
    return n;
  }
}

Answer 1, authority 100%

It is completely incomprehensible what kind of strange thing this code is doing.

java.util.NoSuchElementException: No line found

It is quite obvious that this is because the number of lines entered does not match the number that was on the fourth line. If you enter everything neatly, then the program works.

Well, for that matter, you should use StringBuilder in the del function, and your version is horribly inefficient.

In addition, your program will crash if standard input is closed ahead of time (before n lines are transmitted).

And, nevertheless, it would be better if you were better at fleshing out your question.


Answer 2, authority 95%

from your error it is clear what swears that you did not enter the required number of lines in the console.

I think it’s worth updating your code a little:

String s = input.readLine ();
      s = s.replaceAll ("[-_;]", ""));
      if (s.equalsIgnoreCase (a1 + b1 + c1)) {
        arrStud [i] = "ACC";
      } else {
        arrStud [i] = "WA";
      }

or like this:

arrStud [i] = (s.equalsIgnoreCase (a1 + b1 + c1)? "ACC": "WA" );

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions