简体   繁体   English

如何从带有int和String元素的txt文件中读取一行?

[英]How to read a line from a txt file with int and String elements?

I'm trying to create a List by reading a txt file. 我正在尝试通过阅读txt文件来创建列表。 For example: 例如:

12345; Mary Jane ; 20
12365; Annabelle Gibson; NA

Each line contains: number; 每行包含:数字; name; 名称; Grade (grade can be a String or a int) 等级(等级可以是字符串或整数)

I created the following method; 我创建了以下方法;

public static  List <Pauta>leFicheiro( String nomeF) throws       FileNotFoundException{
    List<Pauta> pautaT = new LinkedList<Pauta>();
    try {
    Scanner s = new Scanner(new File (nomeF));
        try{
            List<Pauta> pautaS =pautaT;
            while ( s.hasNextLine()){
                String line = s.nextLine();
                String tokens[]= line.split(";");
                if(s.hasNextInt()) {
                    System.out.println ("next int = " + s.nextInt());
            }
                pautaS.add(new Pauta (s.nextInt(), tokens[1],tokens[2]);
            }
            pautaT = pautaS;
        }
    finally {
        s.close();
    }
    }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }
        catch (ParseException e){
            e.printStackTrace();
        }       
    return pautaT;
}

Pauta is a class that receives as arguments ( int, String, String), I thought Grade as a String to make it more simple, but if you have ideas on how to still create a List (main goal) by having it String or int I would be thankful. Pauta是一类作为参数(int,String,String)接收的类,我认为将Grade作为一个String使其更简单,但是如果您对如何通过使用String或int来创建列表(主要目标)有想法我会很感激。

Now the problem is: The part s.nextInt() is returning error : InputMismatchException 现在的问题是:部分s.nextInt()返回错误:InputMismatchException

So I put this following code: 所以我把下面的代码:

catch (InputMismatchException e) {
System.out.print(e.getMessage());}

which says it returns a null. 表示返回null。

How can I solve this? 我该如何解决?

Instead of using the s.nextInt(), parse the first token to an Integer. 代替使用s.nextInt(),将第一个标记解析为Integer。

See the example below. 请参见下面的示例。

public static void main(String... args) throws FileNotFoundException {
        List<Pauta> pautaT = new LinkedList<Pauta>();
        try {
            Scanner s = new Scanner(new File("test.txt"));
            try{
                List<Pauta> pautaS =pautaT;
                while ( s.hasNextLine()){
                    String line = s.nextLine();
                    String tokens[]= line.split(";");
                    pautaS.add(new Pauta (Integer.parseInt(tokens[0]), tokens[1],tokens[2]));
                }
                pautaT = pautaS;
            }
            finally {
                s.close();
            }
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }

    }

This results in the list to be populated. 这将导致列表被填充。

您可能会使用StringTokenizer,并将同一行的每个标记保存到变量中,然后根据需要使用它们。

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

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