gpt4 book ai didi

java - ideone 上线程 "main"java.util.NoSuchElementException 中的异常

转载 作者:行者123 更新时间:2023-11-29 04:50:47 24 4
gpt4 key购买 nike

这是我在 Ideone 上测试时使用 java 编写的第一个代码。它显示:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Hw2_p4.main(Main.java:22)

我搜索了答案,但没有找到问题的原因或解决方法。代码在eclipse上正常运行

这是代码

import java.util.Scanner;

class Hw2_p4 {

static void swap(String[] A, int a, int b) {
String temp = A[a];
A[a] = A[b];
A[b] = temp;
}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt(), m = sc.nextInt();
int i, j, k, l, counter = 0;

String[] name = new String[16];
String[][] notalong = new String[120][2];
String[] temp = new String[120];
boolean[][] A = new boolean[120][2];

for (i = 0; i < n; i++) {
name[i] = sc.next();
}

for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
notalong[i][j] = sc.next();
}
}

int flag = 0;

for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
flag = 0;
for (k = i + 1; k < m; k++) {
for (l = 0; l < 2; l++) {
if (notalong[i][j].compareToIgnoreCase(notalong[k][l]) == 0 && A[i][j] == false && A[k][l] == false) {
A[k][l] = true;
flag = 1;
}
}
}

if (flag == 1) {
A[i][j] = true;
counter++;
} else if (flag == 0 && A[i][0] == false && A[i][1] == false) {
A[i][j] = true;
counter++;
}
}

}

System.out.println(n - counter);
int x = 0;

for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
if (A[i][j] == false) {
temp[x++] = notalong[i][j];
A[i][j] = true;

for (k = i + 1; k < m; k++) {
for (l = 0; l < 2; l++) {
if (notalong[i][j].compareToIgnoreCase(notalong[k][l]) == 0 && A[k][l] == false) {
A[k][l] = true;
}
}
}
}
}
}

//compare not along with names
int found = 1;

for (i = 0; i < n; i++) {
found = 0;
for (j = 0; j < m; j++) {
for (k = 0; k < 2; k++) {
if (name[i].compareToIgnoreCase(notalong[j][k]) == 0) {
found = 1;
}
}
}

if (found == 0) {
temp[x++] = name[i];
}
}

//sorting lexicographically
boolean swapp = true;
for (i = 0; i < x && swapp; i++) {
swapp = false;
for (j = 0; j < x - i - 1; j++) {
if (temp[j].compareToIgnoreCase(temp[j + 1]) > 0) {
swap(temp, j, j + 1);
swapp = true;
}
}
}

for (i = 0; i < x; i++) {
System.out.println(temp[i]);
}
}
}

最佳答案

Ideone 不是交互式的。在运行应用程序之前,您必须单击 Specify input 并在其中输入所有输入。您看到的是异常情况,因为 System.in 处于“文件结束”状态。

关于java - ideone 上线程 "main"java.util.NoSuchElementException 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com