简体   繁体   English

输出到控制台时出现 Java StringTokenizer 错误

[英]Java StringTokenizer Error when outputting to console

I'm trying to use the class StringTokenizer to split a character string from a text file, but when I'm running the app, the compiler prints in the Netbeans console the words that I'm splitting but also shows an exception.我正在尝试使用StringTokenizer类从文本文件中拆分字符串,但是当我运行应用程序时,编译器在 Netbeans 控制台中打印我正在拆分的单词,但也显示异常。

This is my code:这是我的代码:

package Calqfunny; 

import java.io.BufferedReader; 
import java.io.FileReader;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;

public class Files {

public String direccion;

public Files(){

    direccion = " ";
}

public Files(String direccion){

    this.direccion = direccion;
}




public String leerTxt(String direccion){

    String auxiliar = " ";

    try{

        BufferedReader br = new BufferedReader(new FileReader(direccion));
        String temp = " "; //Aqui guardamos el texto del archivo temporalmente
        String banana; //aqui almacenamos 


        while((banana = br.readLine())!=null){
            //se realiza el ciclo mientras que el archivo tenga datos.

           temp = temp + banana; 

        }
        auxiliar = temp;

    }catch(Exception e){

    JOptionPane.showMessageDialog(null,"\"¿Cómo vas a pedir un archivo que no existe? :v\"");

    }

    String nombre = null, apellido = null, edad = null, bday = null;
   StringTokenizer tokens = new StringTokenizer (auxiliar, ";");

    System.out.println("Nombre\tApellido Edad\tFecha de Nac.");
    while(tokens.hasMoreTokens()){


          nombre = tokens.nextToken();
          apellido = tokens.nextToken();
          edad = tokens.nextToken();
          bday = tokens.nextToken();


          System.out.println(nombre+"\t"+apellido+"\t"+edad+"\t"+bday);

    }

        return auxiliar;
  } 
}

This is the output from my app这是我的应用程序的输出

Nombre  Apellido  Edad  Fecha de Nac.
David    Villa     31        1985
Andrea   Pirlo     36        1980
Lionel   Messi     29        1987
Tomas    Rincon    27        1989

And this is the exception that the compiler throws这是编译器抛出的异常

Exception in thread "main" java.util.NoSuchElementException

at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)

at Calqfunny.Files.leerTxt(Files.java:69)

at Calqfunny.Mein.main(Mein.java:14)

C:\Documents and Settings\Goyo\Configuración local\Datos de    
programa\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned:1

BUILD FAILED (total time: 0 seconds)

What can I do to solve it?我能做些什么来解决它?

Your while loop is testing if the StringTokenizer has ONE more token.您的 while 循环正在测试StringTokenizer是否还有一个令牌。 But then you are reading FOUR more tokens.但是,您正在阅读另外四个令牌。 This is going to crash when the number of tokens is not divisible by four.当令牌的数量不能被四整除时,这将崩溃。

What you can safely do is:您可以安全地做的是:

StringTokenizer tokens = new StringTokenizer(auxiliar, ";");

while(tokens.hasMoreTokens()){
    token = tokens.nextToken();
    System.out.println(token);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM