仅删除某些地方的空格,而不是所有地方

发布于 2024-11-10 04:01:29 字数 1083 浏览 0 评论 0原文

我正在从 txt 文件中获取 txt 并尝试将其存储在数组列表中。在txt文件中,这意味着选票不在一起,而是老师将其放在单独的行上。因此,我们必须将所有选票放在一起,但我无法将它们放在一起,她将第一张选票放在了线上,如下例所示。我们必须把剩下的放在一起。我正在使用 fileinputstream 从文本文件中收集 txt。

文本如下所示:

<前><代码>人 1 人 2 人 3 1 2 3 1 3 2

我希望它看起来像这样

<前><代码>人 1 人 2 人 3 1 2 3 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);    
}

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);    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-11-17 04:01:29

您需要使用StringBuilder sb = new StringBuilder();。每次您拥有令牌 String t 时,请调用 sb.append(t.trim()).append(' ');。解析完文件后,调用 sb.toString();。如果您想在各处添加换行符,请使用sb.append('\n');

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

You need to use a StringBuilder sb = new StringBuilder();. Each time you have a token String t, call sb.append(t.trim()).append(' ');. When done with parsing your file, call sb.toString();. If you want to add newlines here and there, use sb.append('\n');.

If you want an array of your tokens, you should use an ArrayList<String> al = new ArrayList<String>; Each time you have a set of token s making a line, use al.add(s); When done, call String[] result = al.toArray(new String[al.length]); You will need to concatenate your set of tokens into s for each line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文