我在启动此代码时遇到一些问题。它不断给我错误!如果找不到文件,我添加了异常。但它不会工作...非常感谢扫描仪错误运行时间
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;
}
}
当你说它不识别扫描仪时,你是什么意思?你看到一个编译器错误?运行时异常?意外的行为?还有别的吗? –
'FileNotFoundException'是一个检查异常,所以需要明确抛出(或捕获) – Reimeus