3

我创建了一个扫描器类来读取文本文件并获得我想要的值。假设我有一个文本文件包含。

人员名单:长度3

1 : Fnjiei : ID 7868860 : 18 岁

2 : Oipuiieerb : ID 334134 : 39 岁

3 : Enekaree : ID 6106274 : 31 岁

我正在尝试获取姓名和身份证号码和年龄,但每次我尝试运行我的代码时,它都会给我一个例外。这是我的代码。java 大师有什么建议吗?:) 它能够读取一行.......但不超过一行文本。

    public void readFile(String fileName)throws IOException{
    Scanner input = null;
    input = new Scanner(new BufferedReader(new FileReader(fileName)));
    try {
        while (input.hasNextLine()){
            int howMany = 3;
            System.out.println(howMany);
            String userInput = input.nextLine();
            String name = "";
            String idS = "";    
            String ageS = "";
            int id;
            int age;
            int count=0; 
            for (int j = 0; j <= howMany; j++){
                for (int i=0; i < userInput.length(); i++){
                    if(count < 2){ // for name
                        if(Character.isLetter(userInput.charAt(i))){
                            name+=userInput.charAt(i); // store the name
                        }else if(userInput.charAt(i)==':'){
                            count++;
                            i++;
                        }
                    }else if(count == 2){ // for id
                        if(Character.isDigit(userInput.charAt(i))){
                            idS+=userInput.charAt(i); // store the id
                        }
                        else if(userInput.charAt(i)==':'){
                            count++;
                            i++;
                        }
                    }else if(count == 3){ // for age
                        if(Character.isDigit(userInput.charAt(i))){
                            ageS+=userInput.charAt(i); // store the age
                        }
                    }
                    id = Integer.parseInt(idS); // convert id to integer
                    age = Integer.parseInt(ageS); // convert age to integer
                    Fighters newFighters = new Fighters(id, name, age);
                    fighterList.add(newFighters);
                }
                userInput = input.nextLine();
            }
        }
    }finally{
        if (input != null){
            input.close();
        }
    }
}

如果我的代码需要更改,我会道歉。

编辑它给了我一个数字格式异常!!!我不知道这些值之间会有多少空白。

4

3 回答 3

4

这是一个仅使用ScannerAPI 的解决方案,重要的是findInLine. 它可以处理输入格式中的细微语法变化,但它的可读性很强,不需要花哨的正则表达式或魔法数组索引。

    String text =
        "List  of @#%^$ people : length  3  !@%# \n" + 
        "1 :   Fnjiei   : ID 7868860 ::: Age 18\n" +
        "   2: Oipuiieerb : ID 334134 : Age 39 (old, lol!) \r\n" + 
        " 3 : Enekaree : ID 6106274 => Age 31\n";
    Scanner sc = new Scanner(text);

    sc.findInLine("length");
    final int N = sc.nextInt();

    for (int i = 0; i < N; i++) {
        sc.nextLine();
        sc.findInLine(":");
        String name = sc.next();
        sc.findInLine("ID");
        long id = sc.nextLong();
        sc.findInLine("Age");
        int age = sc.nextInt();
        System.out.printf("[%s] %s (%s)%n", id, name, age);
    }

这打印:

[7868860] Fnjiei (18)
[334134] Oipuiieerb (39)
[6106274] Enekaree (31)

API 链接

于 2010-06-07T12:15:20.737 回答
3

这似乎更短:

public void readFile(String fileName)throws IOException
    {
        Scanner input = null;
        input = new Scanner(new BufferedReader(new FileReader(fileName)));
        String userInput;
        try
        {
            while (input.hasNextLine())
            {
                userInput = input.nextLine().trim();
                if (userInput.length() > 0)
                {
                    String[] userInfo = userInput.split(":");
                    int count = Integer.parseInt(userInfo[0].trim());
                    String name = userInfo[1].trim();
                    int id = Integer.parseInt(userInfo[2].trim().split("\\s+")[1].trim());
                    int age = Integer.parseInt(userInfo[3].trim().split("\\s+")[1].trim());

                    System.out.println("Count: " + count + " Name: " + name + " ID:" + id + " Age:" + age);
                }
                Fighters newFighters = new Fighters(id, name, age);
                fighterList.add(newFighters);
            }


        }
        finally
        {
            if (input != null)
            {
                input.close();
            }
        }
    }

对于您的输入,它会打印以下内容:

人数:1 姓名:Fnjiei ID:7868860 年龄:18

人数:2 姓名:Oipuiieerb ID:334134 年龄:39

计数:3 姓名:Enekaree ID:6106274 年龄:31

有关拆分方法的更多信息,请参见此处。我基本上首先使用:as 分隔符拆分行,然后使用 再次拆分\\s+,它基本上拆分一个字符串并返回一个包含由空格分隔的单词的数组。

于 2010-06-07T10:42:46.903 回答
2
Scanner input = null;
input = new Scanner(new BufferedReader(new FileReader("filename")));

    try{
       while(input.hasNextLine()){
         String userInput = input.nextLine();
         String[] data = userInput.split(":");
         System.out.println("Name: "+data[1]+" ID:"+data[2].split("\\s+")[2]+
            "  Age:"+data[3].split("\\s+")[2]);

            }
        }finally{

            if(input != null) 
             input.close();
     }

上面的片段显示了基本思想。另外请记住,这可能不是最佳解决方案。

于 2010-06-07T10:36:56.373 回答