Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文本文件读取数据时出错_Java - Fatal编程技术网

Java 从文本文件读取数据时出错

Java 从文本文件读取数据时出错,java,Java,我有一个应用程序,假设从包含学生详细信息(student.txt)的文本文件中读取数据,例如我有一个studentNo、StudentName、Marks等 以下是文本文件中的数据示例: 20405587 "ZULU,B M" 65 67 20407388 "JUGGERNATH,N" 66 63 20408427 "KHATHI,P X" 60 60 20409821 "SINGH,T" 62 59 20410422 "NKOMO,N N"

我有一个应用程序,假设从包含学生详细信息(student.txt)的文本文件中读取数据,例如我有一个studentNo、StudentName、Marks等

以下是文本文件中的数据示例:

20405587    "ZULU,B M"  65  67
20407388    "JUGGERNATH,N"  66  63
20408427    "KHATHI,P X"    60  60
20409821    "SINGH,T"   62  59
20410422    "NKOMO,N N" 58  60
我正在使用扫描仪读取文件,这是到目前为止我的代码。这给了我一个错误

try
{
   BufferedReader br = new   BufferedReader(new FileReader("student.txt"));
   String line = br.readLine();

   while (line!=null)
   {
        Scanner scan = new Scanner(line);   
        scan.useDelimiter(" ");
        String dummystudent=scan.next();
        int studentNo= Integer.parseInt(dummystudent);
        String dummyname1 = scan.next();
        String dummyname2 = scan.next();
        String studentName = dummyname1+dummyname2;
        String dummytest1 = scan.next();
        int test1= Integer.parseInt(dummytest1);
        String dummytest2 = scan.next();
        int test2= Integer.parseInt(dummytest2);
        tad1.setText(tad1.getText()+"Student Number: " + studentNo + '\n' + "Student Name :" + studentName );

        line = br.readLine();
    } 
    br.close();
}
catch(Exception b)
{
    JOptionPane.showMessageDialog(null,b.getMessage());
}

您应该使用分隔符
“?+”?
,分隔符是一个正则表达式,并且您的字符串有多个空格来分隔字段,您还需要考虑字符串字段周围的引号。我还没有学会如何用空格解字符串字段


使用正则表达式,您应该能够使用下面的正则表达式字符串,并通过


将分隔符设置为单个空格。这就是问题所在
next
将多次返回空字符串,因为您的这些行有多个连续空格

相反,您想说一个或多个空格:

sc.useDelimiter(" +");
<>代码> Zulu,B M“中间有一个空格和<代码>”Jugnnthn,n“不,但是我将留给你们去理解。也许:

sc.useDelimiter("\"");
在中间的某个地方。


+
与正则表达式有关,请参阅,以了解更多一般性内容和更多特定于Java的内容。

您的解析似乎有些过火

考虑使用Scanner读取该行,并使用StringUtils.split()解析该行

下面是一些代码:

public static void main(String[] args) { int index = 1; // Just for printing. for (String current : input) { String[] split1; // result: number, name, numbers String[] split2; String studentName1; String studentName2; String studentNumber; String testScore1; String testScore2; split1 = StringUtils.split(current, '"'); studentNumber = StringUtils.trim(split1[0]); split2 = StringUtils.split(split1[1], ','); studentName1 = StringUtils.trim(split2[0]); studentName2 = StringUtils.trim(split2[1]); split2 = StringUtils.split(split1[2]); // default seperator is whitespace. testScore1 = StringUtils.trim(split2[0]); testScore2 = StringUtils.trim(split2[1]); System.out.println( index + ":" + " Number: " + ">" + studentNumber + "" + studentName1 + "" + studentName2 + "" + testScore1 + "" + testScore2 + " 公共静态void main(字符串[]args) { int index=1;//仅用于打印。 用于(字符串电流:输入) { 字符串[]split1;//结果:编号、名称、编号 字符串[]split2; 字符串studentName1; 字符串studentName2; 字符串studentNumber; 字符串testScore1; 字符串testScore2; split1=StringUtils.split(当前“,”); studentNumber=StringUtils.trim(split1[0]); split2=StringUtils.split(split1[1],','); studentName1=StringUtils.trim(split2[0]); studentName2=StringUtils.trim(split2[1]); split2=StringUtils.split(split1[2]);//默认分隔符为空白。 testScore1=StringUtils.trim(split2[0]); testScore2=StringUtils.trim(split2[1]); System.out.println( 索引+“:”+ 数字:“+”>“+studentNumber+”+studentName1+“+studentName2+”+testScore1+“+testScore2+”
注:StringUtils是从

开始的,最简单的结束方式是逐行进行

Pattern filePatten = Pattern.compile("\\s*(\\d+)\\s+(\"[^\"]+\")\\s+(\\d+)\\s+(\\d+)\\s*");
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    Matcher matcher = filePattern.matcher(line);
    if (matcher.matches()) {
       String id = matcher.group(1);
       String name = matcher.group(2);

       //etc
    } else {
       //Warn : Fragile Regex
    }
}

正则表达式中的每个组捕获一部分行。第二组捕获带有引号的名称。您可能希望将其删除。

您在哪一行上遇到了什么错误?错误是什么?什么不起作用?请不要降级,让他修复1stdesn
扫描仪
获取正则表达式,因此应该有
\\s
而不是
“”
?不要捕获异常。或者至少,如果捕获到异常,请将b.printStackTrace()添加到catch块中,然后……奇迹般地,您将得到一个详细的报告,告诉您异常是什么,它的消息是什么,以及它在源代码中的确切发生位置。这是至关重要的信息。我确实将代码扫描。使用分隔符(“+”)和if(scan.hasNext()),它只显示:学生编号20405587学生姓名:“祖鲁,BM”,但不读取其他行或显示文本文件中的其余数据…仅读取一行时,我会出现一个错误,称为悬挂元字符?接近索引0?+我认为这与分隔符和空格有关,因为其他学生有两个首字母,而其他学生只有一个,我必须跟踪包括在内的整个名称g initialsHow上的空白部分是关于您使用正则表达式提取部分的?更确切地说,使用Apache有点过分,因为
String
已经有
split
trim
functions.String.split有一些Apache的util可以使用的讨厌的边缘情况。我必须导入一些东西才能使用“模式”吗?@oupamcarthy
import java.util.regex.*;
Pattern filePatten = Pattern.compile("\\s*(\\d+)\\s+(\"[^\"]+\")\\s+(\\d+)\\s+(\\d+)\\s*");
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    Matcher matcher = filePattern.matcher(line);
    if (matcher.matches()) {
       String id = matcher.group(1);
       String name = matcher.group(2);

       //etc
    } else {
       //Warn : Fragile Regex
    }
}