[英]I'm trying to read a text file and then split the lines so that i get two different sets of names
The first group should contain odd lines and second should create even but its not working? 第一组应包含奇数行,第二组应创建偶数行,但不起作用? (in java)
(在Java中)
code: 码:
import java.io.*;
class InArray2 {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("names.txt");
BufferedReader br = new BufferedReader(fr);
BufferedReader gr = new BufferedReader(fr);
String register1;
String register2;
int counter = 0;
System.out.println("Coursework A will be done by :");
while ((register1 = br.readLine()) != null ) {
counter++;
if (counter == 1 || counter == 3 || counter == 5 || counter == 7 || counter == 9 )
System.out.println(register1);
}
System.out.println("Coursework B will be done by :");
while ((register2 = gr.readLine()) ! = null ) {
counter++;
if (counter == 2 || counter == 4 || counter == 6 || counter == 8 || counter == 10 )
System.out.println(register2);
}
br.close();
} catch (IOException e) {
System.out.println("file not found");
}
}
}
Change your buffered reader to : 将缓冲的阅读器更改为:
BufferedReader br = new BufferedReader(new FileReader("names.txt"));
BufferedReader gr = new BufferedReader(new FileReader("names.txt"));
I think the other solution don't do an copy but an reference streamed. 我认为其他解决方案不复制,而是流式传输参考。
But I thinks the good way is to store the data into an two array with one loop and print the result after. 但是我认为一个好方法是将数据存储到一个循环的两个数组中,然后打印结果。 Normaly you need only one buffer for read a file.
通常,您只需要一个缓冲区即可读取文件。 And only one fileReader for one file :) Check your algo
一个文件只有一个fileReader :)检查您的算法
Your BufferredReader
's share the same FileReader
instance. 您的
BufferredReader
共享相同的FileReader
实例。
It seems that changing code to: 似乎将代码更改为:
FileReader fr = new FileReader("names.txt");
FileReader fr1 = new FileReader("names.txt");
BufferedReader br = new BufferedReader(fr);
BufferedReader gr = new BufferedReader(fr1);
will work. 将工作。
But it won't work. 但这是行不通的。 You need to reset counter to 0 before printing the second group:
您需要在打印第二组之前将计数器重置为0:
counter = 0;
System.out.println("Coursework B will be done by :");
One last thing: use counter % 2 == 0
to check if the counter value is even 最后一件事:使用
counter % 2 == 0
检查计数器值是否为偶数
And another last thing: you have massive code duplication here. 最后一件事:您在这里有大量的代码重复。 It's better to create a method that'll print even or odd lines.
最好创建一个可以打印偶数行或奇数行的方法。
public static void printOnlyModulo(int modulo, String header) {
System.out.println(header);
try {
int counter = 0;
BufferedReader reader = new BufferedReader(new FileReader("names.txt"));
String line;
while ((line = reader.readLine()) != null) {
counter++;
if (counter % 2 == modulo) {
System.out.println(line);
}
}
reader.close();
}
catch (IOException e) {
System.out.println("file not found");
}
}
And then call this method from main
: 然后从
main
调用此方法:
public static void main(String[] args) {
printOnlyModulo(1, "Coursework A will be done by :");
printOnlyModulo(0, "Coursework B will be done by :");
}
It's cleaner. 比较干净
You only need to read the file one time. 您只需要读取一次文件。
... blah.
int lineNumber = 1;
List<String> theAList = new LinkedList<String>();
List<String> theBList = new LinkedList<String>();
while ((line = reader.readLine()) != null)
{
if (lineNumber % 2) // even are on the b list.
{
theBList.add(line);
}
else // odd are on the a list.
{
theAList.add(line);
}
}
System.out.println("A list");
for (String current : theAList)
{
System.out.println(current);
}
System.out.println("B list");
for String current : theBList)
{
System.out.println(current);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.