Java 从文本文件中读取数字字符串并将它们相加
我有一个名为output.txt的.txt文件,这是该文件的内容Java 从文本文件中读取数字字符串并将它们相加,java,file,java.util.scanner,Java,File,Java.util.scanner,我有一个名为output.txt的.txt文件,这是该文件的内容 我想读每个位置的最后一个数字(第四个位置) 把它们加在一起得到一个总数。我主要想知道如何将文本转换成一些可用的变量 import java.io .*; import java.util.*; public class PrintTotalPointsHeld { private int count; private String id; private File inFile; privat
我想读每个位置的最后一个数字(第四个位置)
把它们加在一起得到一个总数。我主要想知道如何将文本转换成一些可用的变量
import java.io .*;
import java.util.*;
public class PrintTotalPointsHeld
{
private int count;
private String id;
private File inFile;
private Scanner input;
private String name;
private File outFile;
private PrintWriter output;
private int total;
public PrintTotalPointsHeld (String name, String id, String inFilename, String outFilename) throws Exception, IOException
{
}
public void processFiles() throws Exception, IOException
{
// Stores every word as a variable so we can do our calculations later
try(BufferedReader br = new BufferedReader(new FileReader(inFile))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String points = sb.toString();
}
output.println("There are " + count + " accounts that together hold " + total + " points. ");
}
}
}使用java 8,以下方法将读取文件,过滤掉并仅处理与“字符串编号”匹配的行,然后对最终的数字列求和
public Optional<Long> sumFinalColumnOfFile(String filePath) throws IOException {
Path path = FileSystems.getDefault().getPath(filePath);
return Files.lines(path)
.filter(s -> s.matches("^\\w+\\s\\w+\\s\\d+\\s\\d+$"))
.map(s -> Long.valueOf(s.split(" ")[3]))
.reduce((p, c) -> p + c);
}
public可选sumFinalColumnOfFile(字符串文件路径)引发IOException{
Path Path=FileSystems.getDefault().getPath(filePath);
返回文件。行(路径)
.filter(s->s.matches(“^\\w+\\s\\w+\\s\\d+\\s\\d+$”)
.map->Long.valueOf(s.split(“”[3]))
.减少((p,c)->p+c);
}
.filter(s->s.matches(“^\\w+\\s\\w+\\s\\d+\\s\\d+$”)
意味着只有以单词开头的行,然后有空格,然后是数字,然后是数字,最后是行尾才会被操作
.map(s->Long.valueOf(s.split(“”[3]))
将拆分从“”上的筛选器接收的任何行,然后获取最终字符串的Long值
.reduce((p,c)->p+c)
将对从地图接收到的每个数字求和
算法(代码)的步骤如下:
1) 打开包含要读取内容的文件
(使用BufferedReader
)
2) 初始化计数器
,该计数器将存储总和
(我使用长
,因为你的总和
可能会非常大
,整数
可能无法容纳这么大的数字
)
3) 逐行读取文件
(使用字符串line=myReader.readLine();
获取下一行,确保在(line!=null){
拆分
每一行时使用空格
作为分隔符,保留每个数组的第四个元素(部分[3]
因为数组从0
)开始,其中包含要添加的编号,并且将此编号添加到上一个总和中
4) 打印账户数量(即行数)
和总和
当文件中没有可读取的行时
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
class myRead{
public static void main(String[] args) throws FileNotFoundException, IOException {
long cnt = 0;
long numberOfLines = 0;
BufferedReader myReader = new BufferedReader(new FileReader("test.txt"));
String line = myReader.readLine();
while(line != null){
numberOfLines++;
String[] parts = line.split(" ");
cnt = cnt + Integer.parseInt(parts[3]);
line = myReader.readLine();
}
System.out.println("There are " + numberOfLines + " accounts that together hold " + cnt + " points.");
}
}
输出:共有3个帐户持有2558543点。
这是一个非常模糊的问题,因此这是一个非常模糊的回答。使用“for”循环逐行迭代.txt文件并存储到数组中只需拆分该行并获取具有适当索引的第四个元素:
FileReader file = new FileReader(new File("test.txt"));
BufferedReader reader = new BufferedReader(file);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line.split(" ")[3]);
}
如果确定要相加的数字始终位于第四位,请使用String类中的split()方法:
BufferedReader br = new BufferedReader(new FileReader(inFile));
String process = br.readLine();
int total = 0;
while(process != null){
String[] columns = process.split(" "); //This array will contain all columns from a single row starting index 0
total = total + Integer.parseInt(columns[3]);
process = br.readLine();
}
一旦你计算了总数,然后才将最后一行写入你的文件。我在上面添加了我的代码,我尝试过与bufferedreader和string Builder混在一起。太棒了,有点混在一起,我的工作也做得很好,解释也很好,我现在更了解算法了。谢谢!还有所有其他答案。