java 从文本文件中读取的两个整数相加并在控制台上显示结果的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16318749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:38:02  来源:igfitidea点击:

Program to add two integer numbers read from a text file and displaying results on console

javafileinputstreamdatainputstream

提问by Salman

Even after lots of trial and error, I am unable to figure out how to write a java program that adds 2 integers (read from a text file) and displays addition result on the console.

即使经过大量的反复试验,我也无法弄清楚如何编写一个添加 2 个整数(从文本文件读取)并在控制台上显示加法结果的 Java 程序。

I tried using FileInputStream, DataInputStream classes ...

我尝试使用 FileInputStream、DataInputStream 类...

Example explaining what I exactly need !

示例解释了我到底需要什么!

Suppose there are 2 integers stored in a text file (sample.txt) .... Let 1 and 2 be the integers.

假设有 2 个整数存储在一个文本文件 (sample.txt) 中......让 1 和 2 为整数。

I would like to read those integers from the file and display their sum (= 3) on the console

我想从文件中读取这些整数并在控制台上显示它们的总和(= 3)

Any help would be appreciated !

任何帮助,将不胜感激 !

P.S: I am a beginner in Java, so please be as simple as you can wrt coding !

PS:我是Java初学者,所以请尽可能简单地编写代码!

回答by Jops

Here's something you could start with:

您可以从以下方面着手:

import java.util.Scanner;
import java.io.*;

public class MyClass {

    public static void main(String[] args) throws IOException {

        Scanner s = new Scanner(new File("sample.txt"));
        int tmp1 = s.nextInt();
        int tmp2 = s.nextInt();
        System.out.println(tmp1 + tmp2);
    }
}

Create the text file directly under the Project root in Eclipse.

在 Eclipse 的项目根目录下直接创建文本文件。

Sample content can be:

示例内容可以是:

1 2

回答by rcbevans

Your question isn't very clear at all, but providing you have the ints stored in a text file i.e

您的问题根本不是很清楚,但是如果您将整数存储在文本文件中,即

//sample.txt
1 2

You can use a Scanner to read the text file into an array of ints

您可以使用扫描仪将文本文件读入一个整数数组

Scanner scanner = new Scanner(new File("sample.txt"));

int [] numbers = new int [5];
int i = 0;
while(scanner.hasNextInt()){
   numbers[i++] = scanner.nextInt();
}

then print the result

然后打印结果

int sum = 0;
for (int i = 0; i < numbers.size(); i++)
    sum += numbers[i];
System.out.println(sum);

(This will work for a text file of numbers up to 5 long) change "new int [5];" to have the number of elements you want as desired i.e new int [2];)

(这适用于最长为 5 的数字文本文件)更改“new int [5];” 拥有所需的元素数量,即 new int [2];)

Hopefully this will be useful

希望这将是有用的

回答by Achintya Jha

Try this:

试试这个:

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("try.txt"));
    String line ="";
    int sum =0;
    while((line = br.readLine())!= null)
        sum = sum + Integer.parseInt(line);
    System.out.println(sum);
}

File try.txt:

文件try.txt:

1
2

Output:

输出:

3