当前位置: 代码迷 >> J2SE >> JAVA Scanner 重复输入的有关问题
  详细解决方案

JAVA Scanner 重复输入的有关问题

热度:384   发布时间:2016-04-24 01:15:58.0
JAVA Scanner 重复输入的问题
Java code
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结束就会抛出!!!

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