スレッド メイン Java.Util.NoSuchElementException の例外: 行が見つかりません

Sheeraz Gul 2023年10月12日
スレッド メイン Java.Util.NoSuchElementException の例外: 行が見つかりません

このチュートリアルでは、Java で Exception in thread "main" java.util.NoSuchElementException: No line found を解決する方法を示します。

スレッドメインでの例外 java.util.NoSuchElementException: 行が見つかりません

java.util.NoSuchElementException は実行時のチェックされない例外です。 JVM は、next()nextElement()、イテレータ、またはメソッドや列挙などのメソッドを使用すると、この例外を発生させます。

エラー Exception in thread "main" java.util.NoSuchElementException: No line found は、Scanner を使用して nextLine() のようなメソッドでユーザー入力を取得しているときに発生します。 境界なしでメソッドを使用しようとすると、エラーが発生します。

このエラーを示す例を試してみましょう。

package delftstack;

import java.util.Scanner;

public class Example {
  static boolean[][] Articles;
  public static void main(String[] args) {
    // This initiates all array values to be false
    Articles = new boolean[4][4];
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        Articles[i][j] = false;
      }

      // Welcome message
      System.out.println("-------------------------");
      System.out.println("Welcome to Delftstack.com.");
      System.out.println("-------------------------\n");

      // Starts program
      Programstart();
    }
  }

  public static void Programstart() {
    // to read users' input
    Scanner sc = new Scanner(System.in);

    // user input
    String Requested_Lanuguage;
    String Requested_Article;

    // Counters for articles array
    int Count_Language = 0;
    int Count_Artciles = 0;

    // User to select their choice of Programming Language
    System.out.print("Please type 1 for Java or 2 for Python: ");

    // Language preference
    Requested_Lanuguage = sc.nextLine();

    switch (Requested_Lanuguage) {
      case "1":
        // User selects Java
        System.out.println(">>> You have selected Java. \n");
        break;

      case "2":
        // User selects Python
        System.out.println(">>> You have selected Python. \n");
        break;

      default:
        // User has not selected a valid Programming Language
        System.out.println(">>> You have not selected a valid choice. Please try again. \n");
        Programstart();
        break;
    }

    // user to select their choice of article
    System.out.print("Please type 1 for Web and 2 for App: ");

    // Article preference
    Requested_Article = sc.nextLine();

    switch (Requested_Article) {
      case "1":
        // User selects Web Articles
        System.out.println(">>> You have selected Web Articles. \n");
        break;

      case "2":
        // User selects App Articles
        System.out.println(">>> You have selected App Articles. \n");
        break;

      default:
        // User has not selected a valid article
        System.out.println(">>> You have not selected a choice. Please try again. \n");
        Programstart();
        break;
    }

    // Closes Scanner
    sc.close();
  }
}

上記のコードの出力は次のとおりです。

-------------------------
Welcome to Delftstack.com.
-------------------------

Please type 1 for Java or 2 for Python: 1
>>> You have selected Java.

Please type 1 for Web and 2 for App: 1
>>> You have selected Web Articles.

Exception in thread "main" -------------------------
Welcome to Delftstack.com.
-------------------------

Please type 1 for Java or 2 for Python: java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at delftstack.Example.Programstart(Example.java:45)
    at delftstack.Example.main(Example.java:24)

メソッド nextLine() を境界なしで使用しているため、エラーが発生します。 この問題を解決するには、コード Requested_Article = sc.nextLine(); を置き換える必要があります。 次のコードに。

while (sc.hasNextLine()) {
  Requested_Article = sc.nextLine();
  // Switch condition here
}

解決策を試してみましょう。

package delftstack;

import java.util.Scanner;

public class Example {
  static boolean[][] Articles;
  public static void main(String[] args) {
    // This initiates all array values to be false
    Articles = new boolean[4][4];
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        Articles[i][j] = false;
      }

      // Welcome message
      System.out.println("-------------------------");
      System.out.println("Welcome to Delftstack.com.");
      System.out.println("-------------------------\n");

      // Starts program
      Programstart();
    }
  }

  public static void Programstart() {
    // to read users' input
    Scanner sc = new Scanner(System.in);

    // user input
    String Requested_Lanuguage;
    String Requested_Article;

    // Counters for articles array
    int Count_Language = 0;
    int Count_Artciles = 0;

    // User to select their choice of Programming Language
    System.out.print("Please type 1 for Java or 2 for Python: ");

    // Language preference
    Requested_Lanuguage = sc.nextLine();

    switch (Requested_Lanuguage) {
      case "1":
        // User selects Java
        System.out.println(">>> You have selected Java. \n");
        break;

      case "2":
        // User selects Python
        System.out.println(">>> You have selected Python. \n");
        break;

      default:
        // User has not selected a valid Programming Language
        System.out.println(">>> You have not selected a valid choice. Please try again. \n");
        Programstart();
        break;
    }

    // user to select their choice of article
    System.out.print("Please type 1 for Web and 2 for App: ");

    // Article preference
    while (sc.hasNextLine()) {
      Requested_Article = sc.nextLine();

      switch (Requested_Article) {
        case "1":
          // User selects Web Articles
          System.out.println(">>> You have selected Web Articles. \n");
          break;

        case "2":
          // User selects App Articles
          System.out.println(">>> You have selected App Articles. \n");
          break;

        default:
          // User has not selected a valid article
          System.out.println(">>> You have not selected a choice. Please try again. \n");
          Programstart();
          break;
      }
    }
    // Closes Scanner
    sc.close();
  }
}

上記のコードは、Exception in thread "main" java.util.NoSuchElementException: No line foundをスローしなくなりました。 出力を参照してください。

-------------------------
Welcome to Delftstack.com.
-------------------------

Please type 1 for Java or 2 for Python: 1
>>> You have selected Java.

Please type 1 for Web and 2 for App: 1
>>> You have selected Web Articles.

1
>>> You have selected Web Articles.


>>> You have not selected a choice. Please try again.

Please type 1 for Java or 2 for Python:
著者: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

関連記事 - Java Exception