扫描程序错误运行时

时间:2014-01-13 13:38:22

标签: java java.util.scanner

启动此代码时遇到一些问题。它一直给我错误!如果找不到文件,我添加了例外。但它不会起作用......非常感谢

import java.util.ArrayList;
import java.io.File;
import java.util.Random;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class GestoreLotteria {
    public static ArrayList<Utente> listaUtenti = new ArrayList<>();
    public static Random rnd = new Random();

    public static void inserisciUtenti() throws FileNotFoundException{
        Scanner scnr = new Scanner(new File("utenti.txt"));
        while (scnr.hasNextLine()){
            String nome = scnr.next();
            String cognome = scnr.next();
            String city = scnr.next();
            int giorno = rnd.nextInt(28);
            int mese = rnd.nextInt(12);
            int anno = 1996 - rnd.nextInt(72);
            String eta = giorno + " " + mese + " " + anno;
            Utente utente = new Utente(nome, cognome, city, eta);
            listaUtenti.add(utente);
        }
    }


    public static void main(String[] args) throws FileNotFoundException{
        inserisciUtenti();
        System.out.print(listaUtenti);
    } 
}

错误就是这个

~/Desktop/TestEsameLPI/lotteri> java GestoreLotteria
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at GestoreLotteria.inserisciUtenti(GestoreLotteria.java:14)
    at GestoreLotteria.main(GestoreLotteria.java:28)

这是utente.java类

public class Utente {
    public String nome;
    public String cognome;
    public String city;
    public String eta;

    public Utente(String nome, String cognome, String city, String eta) {
            this.nome = nome;
            this.cognome = cognome;
            this.city = city;
            this.eta = eta;
    }

    public String getNome(){
        return nome; 
    }

    public String getCognome(){
        return cognome;
    }

    public String getCity(){
        return city;
    }

    public String getEta(){
        return eta;
    }
}

3 个答案:

答案 0 :(得分:0)

请修改您的问题。它不是编译时错误。在将变量赋值给变量之前,U是否存在scnr.next()。

public static void inserisciUtenti() throws FileNotFoundException{
        Scanner scnr = new Scanner(new File("testfile.txt"));
        String nome = "";
        String cognome = "";
        String city = "";


        while (scnr.hasNextLine()){
            if(scnr.hasNext())
                 nome = scnr.next().toString();
            if(scnr.hasNext())
             cognome = scnr.next().toString();
            if(scnr.hasNext())
             city = scnr.next().toString();
            int giorno = rnd.nextInt(28);
            int mese = rnd.nextInt(12);
            int anno = 1996 - rnd.nextInt(72);`enter code here`
            String eta = giorno + " " + mese + " " + anno;
            Utente utente = new Utente(nome, cognome, city, eta);
            listaUtenti.add(utente);
            }
    } 

答案 1 :(得分:0)

你的文件“utenti.txt”是否以空行开头? 如果是这样,那么就在这个测试之后

while (scnr.hasNextLine()) {

你唯一知道的是文件中有,所以下一个命令

String nome = scnr.next();

无法获得String令牌并生成您获得的NoSuchElementException

作为一般规则,在致电String whatever = scnr.next()之前,您应该使用scnr.hasNext()代替scnr.hasNextLine()

答案 2 :(得分:-1)

你需要处理在程序执行过程中“可能被抛出”的所有异常。在你的情况下,Java编译器正在尖叫 - “你的代码可能会发生一些错误的解决方案,因此处理它的某些事情。期间”所以,你有两个选择...

1. Say that your method "throws" FileNotFoundEception by adding it to the method's signature..
                     OR
2. put a "try-catch" block around the code which might throw this exception..