import java.util.*;public class firstname {
static public Scanner input = new Scanner(System.in);
static public LinkedList<String> fname; public firstname() {
} /**
* @category Write a program that reads in a series of first names and
* stores them in a LinkedList. Do not store duplicate names.
* Allow the user to search for a first name.
*
*/
static String temp; public static void main(String args[]) {
fname = new LinkedList<String>();
while (input.hasNext()) {
temp = input.next();
for (int i = 0; i < fname.size(); i++) {
if (temp.equals(fname.get(i)))
continue; }
fname.add(temp);
}
System.out.println("输入要找的名字");
//input.next();
temp = input.next();
temp = input.next();
for (int i = 0; i < fname.size(); i++) {
if (temp.equals(fname.get(i)))
System.out.printf("The index of %s is %d!", fname.get(i), i);
}
}
}
一直报错 抛出NoSuchElements 异常,求解,怎么改? 为什么错误 用ctrl+z结束就会抛出!!!
static public Scanner input = new Scanner(System.in);
static public LinkedList<String> fname; public firstname() {
} /**
* @category Write a program that reads in a series of first names and
* stores them in a LinkedList. Do not store duplicate names.
* Allow the user to search for a first name.
*
*/
static String temp; public static void main(String args[]) {
fname = new LinkedList<String>();
while (input.hasNext()) {
temp = input.next();
for (int i = 0; i < fname.size(); i++) {
if (temp.equals(fname.get(i)))
continue; }
fname.add(temp);
}
System.out.println("输入要找的名字");
//input.next();
temp = input.next();
temp = input.next();
for (int i = 0; i < fname.size(); i++) {
if (temp.equals(fname.get(i)))
System.out.printf("The index of %s is %d!", fname.get(i), i);
}
}
}
一直报错 抛出NoSuchElements 异常,求解,怎么改? 为什么错误 用ctrl+z结束就会抛出!!!
temp = input.next();
for (int i = 0; i < fname.size(); i++) {
if (temp.equals(fname.get(i)))
continue; }
fname.add(temp);
}while (input.hasNext())就是个死循环啊!这之后的东西都不会执行的!
为什么会死循环啊 不是遇到ctrl+z就会停止么???就为false了啊 那我要怎么在输入ctrl+z之后就停止该循环?