0

I am taking in txt from a txt file and trying to store it in an arraylist. In the txt file the which mean ballot are not together instead the teacher placed it on a separate line. So we have to get all of the ballots together but i am not able to get them to be all together she placed the first ballot on the line like in example below. And we have to make the rest of them together. I am using a fileinputstream to collect the txt from the textfile.

the text looks like this :

person 1 
person 2 
person 3
<b> 1 2 3 
<b> 
1
3
2

I want it to look like this

person 1
person 2 
person 3
<b> 1 2 3 
<b> 1 3 2 
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name: ");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.next();
File file = new File(fileName);
ArrayList<String> ballot;
 ballot = new ArrayList<String>();  
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream ds = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(ds));
// Pattern p;
// Matcher m;
String strLine;
String inputText = "";
String newline = System.getProperty("line.separator");
while ((strLine = br.readLine()) != null) {
    ballot.add(strLine);    
}
4

1 回答 1

0

您需要使用StringBuilder sb = new StringBuilder();. 每次你有一个令牌String t时,调用sb.append(t.trim()).append(' ');. 完成文件解析后,调用sb.toString();. 如果要在这里和那里添加换行符,请使用sb.append('\n');.

如果您想要一个令牌数组,您应该使用 anArrayList<String> al = new ArrayList<String>;每次您有一组令牌 s 组成一行时,使用al.add(s);完成后,调用String[] result = al.toArray(new String[al.length]);您需要将您的令牌集连接到每行的 s 中。

于 2011-05-27T02:40:19.837 回答