2014-01-13 91 views
-2

我在启动此代码时遇到一些问题。它不断给我错误!如果找不到文件,我添加了异常。但它不会工作...非常感谢扫描仪错误运行时间

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; 
    } 
} 
+0

当你说它不识别扫描仪时,你是什么意思?你看到一个编译器错误?运行时异常?意外的行为?还有别的吗? –

+1

'FileNotFoundException'是一个检查异常,所以需要明确抛出(或捕获) – Reimeus

回答

-1

您需要处理所有的在你的程序执行过程中可能抛出的异常......在你的情况下,Java编译器尖叫 - “你的代码可能抛出FILENOTFOUNDEXCEPTION,所以处理它一些时间。”因此,你有2个选项...

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.. 
+0

谢谢补充,但它不断给我错误。 – user3190369

+0

@ user3190369 - 扫描程序可能没有正确获取下一个元素。您确定该文件具有正确的元素吗?顺便说一句你在哪里定义 - “Utente”? – TheLostMind

+0

我做了一个utente类 – user3190369

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); 
      } 
    } 
+0

我让你建议我,现在当我运行它时他盯着什么都不做...... – user3190369

+0

你可以尝试添加打印语句。请分享您的文件内容 –

0

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

while (scnr.hasNextLine()) { 

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

String nome = scnr.next(); 

未能获得String令牌并生成您获得的NoSuchElementException

作为一般规则,在致电String whatever = scnr.next()之前,您应该使用scnr.hasNext()而不是scnr.hasNextLine()进行测试。