java 异常 exception in thread main java.util.nosuchelementexception: no line found-ag捕鱼王app官网

java 异常 exception in thread main java.util.nosuchelementexception: no line found

作者:迹忆客 最近更新:2023/07/16 浏览次数:

本篇文章介绍如何解决java中的异常 exception in thread "main" java.util.nosuchelementexception: no line found


java 异常 exception in thread "main" java.util.nosuchelementexception: no line found

java.util.nosuchelementexception 是运行时未经检查的异常。 当我们使用 next()、nextelement()、迭代器、方法或枚举等方法时,jvm 会引发此异常。

当我们使用扫描仪通过 nextline(); 等方法获取用户输入时,会出现错误 exception in thread "main" java.util.nosuchelementexception: no line found 。 当我们尝试使用没有任何边界的方法时,就会发生错误。

让我们尝试一个演示此错误的示例。

package jiyik;
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 jiyik.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 jiyik.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 jiyik.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 jiyik.example.programstart(example.java:45)
    at jiyik.example.main(example.java:24)

发生错误是因为我们使用没有任何边界的方法 nextline() 。 为了解决这个问题,我们需要替换代码 requested_article = sc.nextline(); 到以下代码。

while(sc.hasnextline()){
    requested_article = sc.nextline();
    // switch condition here
}

让我们尝试一下ag捕鱼王app官网的解决方案。

package jiyik;
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 jiyik.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 now。 查看输出:

-------------------------
welcome to jiyik.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:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:java

本篇文章主要介绍如何在 java 中制造程序延迟。本教程介绍了如何在 java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 java 中把 hashmap 转换为 json 对象

发布时间:2023/12/17 浏览次数:187 分类:java

它描述了允许我们将哈希图转换为简单的 json 对象的方法。本文介绍了在 java 中把 hashmap 转换为 json 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 json 对象的详细例子。

如何在 java 中按值排序 map

发布时间:2023/12/17 浏览次数:171 分类:java

本文介绍了如何在 java 中按值对 map 进行排序。本教程介绍了如何在 java 中按值对 map 进行排序,并列出了一些示例代码来理解它。

如何在 java 中打印 hashmap

发布时间:2023/12/17 浏览次数:192 分类:java

本帖介绍了如何在 java 中打印 hashmap。本教程介绍了如何在 java 中打印 hashmap 元素,还列举了一些示例代码来理解这个主题。

在 java 中更新 hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:java

本文介绍了如何在 java 中更新 hashmap 中的一个值。本文介绍了如何在 java 中使用 hashmap 类中包含的两个方法-put() 和 replace() 更新 hashmap 中的值。

java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:java

本文介绍了 java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 java 中 map 和 hashmap 之间的主要区别。在 java 中,map 是用于以键值对存储数据的接口,

在 java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:java

这篇文章向你展示了如何在 java 中获取用户主目录。本教程介绍了如何在 java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:java

这篇文章教你如何知道 java 中大小和长度之间的区别。本教程介绍了 java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:java

了解有关 java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图