我有一个包含内容的文件:
- 85,70,95,82,75
- 70,78,85,62,47,53,32
- 99,88,75,85,69,72
- 79,84,86,91,84,89,78,82,70,75,82
- 56,68,0,56
- 96,82,91,90,88
我需要使用 java Scanner 类编写代码以按以下顺序读取和输出代码:
- 85,70,95,82,75
- 85 70 95 82 75
- 70,78,85,62,47,53,32
- 70 78 85 62 47 53 32
- 99,88,75,85,69,72
- 99 88 75 85 69 72 等...
但是,我似乎无法让我的代码为我的一生工作,我无法弄清楚我不理解什么。我已经非常接近我的结果,但我已经尝试了很多解决方案,我只是放弃了。这是我拥有的当前代码。这几乎是我得到的最好结果。
import java.io.*;
import java.util.*;
public class FileIO
{
public static void main(String[] args) throws IOException
{
File fileObj2 = new File("Scores.txt");
Scanner scan2 = new Scanner(fileObj2);
String line = "";
String x = "";
Scanner scan3 = null;
scan3 = new Scanner(fileObj2);
scan3.useDelimiter(",");
System.out.println();
System.out.println("Second Files data below \n -------------
---------");
while (scan2.hasNextLine()){
line = scan2.nextLine();
System.out.println(line);
while (scan3.hasNext()){
line2 = scan3.next();
System.out.print(line2 + " " );
}
}
}
}
这给了我输出
85,70,95,82,75
85 70 95 82 75
70 78 85 62 47 53 32
99 88 75 85 69 72
79 84 86 91 84 89 78 82 70 75 82
56 68 0 56
96 82 91 90 88 70,78,85,62,47,53,32
99,88,75,85,69,72
79,84,86,91,84,89,78,82,70,75,82
56,68,0,56
96,82,91,90,88