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结束就会抛出!!!

解决方案 »

  1.   

    while (input.hasNext()) {
                temp = input.next();
                for (int i = 0; i < fname.size(); i++) {
                    if (temp.equals(fname.get(i)))
                        continue;            }
                fname.add(temp);
            }while (input.hasNext())就是个死循环啊!这之后的东西都不会执行的!
      

  2.   

    while (input.hasNext())这样当然会一直让输入了!while()里的循环条件你要根据你的情况修改
      

  3.   


    为什么会死循环啊 不是遇到ctrl+z就会停止么???就为false了啊  那我要怎么在输入ctrl+z之后就停止该循环?
      

  4.   

    在循环里加入判断,获取的是ctrl+z的时候中断(break)循环
      

  5.   

    额……  这个Ctrl+z怎么判断……