Java 异常 Exception in Thread Main Java.Util.NoSuchElementException: No Line Found
本篇文章介绍如何解决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
}
让我们尝试一下解决方案。
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:
相关文章
如何在 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 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,