英文:
How to sum a row of integers from a file that contains multiple rows - Java
问题
我正在尝试从文件中逐行累加数字。此外,我希望代码忽略每行的第一个数字。
从这些数字中,我想要打印出9、4和2。
然而,由于我使用这段业余的代码似乎将所有数字相加,所以得到的答案是21:
while(scan.hasNextInt())
{
a = a + scan.nextInt();
}
System.out.println(a);
英文:
I am trying to sum numbers in separate lines from a file. In addition, I would like the code to ignore the first number of every line.
3 3 3 3
1 1 1 1 1
2 2
From these numbers I would like to print out 9, 4 and 2.
Whereas I am getting an answer of 21 as I sum everything apparently with this amateur piece of code:
while(scan.hasNextInt())
{
a = a + scan.nextInt();
}
System.out.println(a);
答案1
得分: 2
String input = "3 3 3 3\n1 1 1 1 1\n2 2";
Scanner scan = new Scanner(input);
while(scan.hasNextLine()) {
Scanner line = new Scanner(scan.nextLine());
int calculation = 0;
for(int i = 0; line.hasNextInt(); i++) {
if(i > 0) {
calculation = calculation + line.nextInt();
} else {
line.nextInt();
}
}
System.out.println(calculation);
}
我能够使用扫描器而不是数组来获取您所需的输出。如果输入来自控制台或System.in
,您需要在scan
变量中进行相应处理。
英文:
String input = "3 3 3 3\n1 1 1 1 1\n2 2";
Scanner scan = new Scanner(input);
while(scan.hasNextLine()) {
Scanner line = new Scanner(scan.nextLine());
int calculation = 0;
for(int i = 0;line.hasNextInt();i++) {
if(i>0) {
calculation = calculation+line.nextInt();
}else {
line.nextInt();
}
}
System.out.println(calculation);
}
I was able to get your required output using scanners instead of an array to save the lines if the input is from the console or System.in
youll have to account for that in the scan
variable
答案2
得分: 1
没有你的其他代码,但我想我理解你想要做的事情。
因此,你可以逐行处理,并从第二个对象开始。以下是代码:
String line;
while ((line = scan.nextLine()) != null) {
String[] lines = line.split(" ");
int total = 0;
for (int i = 1; i < lines.length; i++) {
total += Integer.parseInt(lines[i]);
}
System.out.println(total);
}
英文:
Without your other side of codes but I think i understood what you'r trying to do.
So you can take all the line by line and can start from second object.Her is the code:
String line;
while((line = scan.nextLine() != null)) {
String[] lines = line.split(" ");
int total = 0;
for(int i = 1; i < lines.length; i++) {
total += Integer.parseInt(lines[i]);
}
System.out.println(total);
}
答案3
得分: 1
public static void main(String[] args) throws IOException {
List<String> lines = Files.readAllLines(Paths.get("D:\\data.txt"));
for (String line : lines) {
String[] numbers = line.split("\\s");
int result = 0;
for(int i=1 ; i<numbers.length ; i++) {
result = result + Integer.parseInt(numbers[i]);
}
System.out.println("Line " + line + " : result " + result);
}
}
result
Line 3 3 3 3 : result 9
Line 1 1 1 1 1 : result 4
Line 2 2 : result 2
英文:
public static void main(String[] args) throws IOException {
List<String> lines = Files.readAllLines(Paths.get("D:\\data.txt"));
for (String line : lines) {
String[] numbers = line.split("\\s");
int result = 0;
for(int i=1 ; i<numbers.length ; i++) {
result = result + Integer.parseInt(numbers[i]);
}
System.out.println("Line "+line+" : result "+ result);
}
}
result
Line 3 3 3 3 : result 9
Line 1 1 1 1 1 : result 4
Line 2 2 : result 2
答案4
得分: 1
try {
// 读取行并流式处理
Files.lines(Path.of("f:/source.txt"))
// 在空格上拆分行并流式处理标记
.map(line -> Arrays.stream(line.split("\\s+")))
// 跳过第一个标记
.skip(1)
// 将每个标记转换为整数
.mapToInt(Integer::parseInt)
// 求和
.sum())
// 并打印每个总和
.forEach(System.out::println);
// 捕捉并打印任何异常。
} catch (Exception e) {
e.printStackTrace();
}
英文:
And for completeness, a stream solution.
try {
// read in the lines and stream them
Files.lines(Path.of("f:/source.txt"))
// split the lines on spaces and stream the tokens
.map(line -> Arrays.stream(line.split("\\s+"))
// skip the first one
.skip(1)
// convert each token to an int
.mapToInt(Integer::parseInt)
// sum them
.sum())
// and print each sum
.forEach(System.out::println);
// catch and print any exceptions.
} catch (Exception e) {
e.printStackTrace();
}
</details>
# 答案5
**得分**: 1
以下是翻译好的部分:
```java
Files.readAllLines(Paths.get("D:/data.txt")).stream()
.map(line -> line.split(" "))
.map(Arrays::stream)
.mapToInt(stream -> stream.skip(1).mapToInt(Integer::parseInt),sum())
.forEach(System.out::println);
英文:
Here’s a 1-liner:
Files.readAllLines(Paths.get("D:/data.txt")).stream()
.map(line -> line.split(" "))
.map(Arrays::stream)
.mapToInt(stream -> stream.skip(1).mapToInt(Integer::parseInt),sum())
.forEach(System.out::println);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论