读取文件中一行中的一对数字
Reading A Pair Of Numbers On A Line In A File
我目前正在开发一个程序,该程序获取用户输入的文件,在每一行添加两个数字,然后将答案打印到用户制作的输出文件和 Netbeans 中的 运行 屏幕中。我似乎只能做第一行,打印时,该行的第二个数字甚至都没有显示。
代码:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Sums {
public static void main(String[] args) throws IOException {
// Scanner for user input
Scanner user = new Scanner(System.in);
String inputFileName, outputFileName;
// prepare the input file
System.out.print("Input File Name: ");
inputFileName = user.nextLine().trim();
File input = new File(inputFileName);
Scanner scan = new Scanner(input);
// prepare the output file
System.out.print("Output File Name: ");
outputFileName = user.nextLine().trim();
// processing loop
try (PrintWriter output = new PrintWriter(outputFileName)) {
// processing loop
{
while (scan.hasNext()) {
int Numbers = scan.nextInt();
double sum = 0;
for (int i = 0; i < Numbers; i++) {
sum += scan.nextDouble();
}
double total = (((sum + 1) * sum) / (2));
output.println("Sum From " + Numbers + "To " + Numbers + "is " + total);
}
}
}
}
}
我试过你的代码,它似乎对我有用。
这是我的输入文件内容:
2 1 2
2 3 4
这里是输出文件内容:
Sum From 2To 2is 6.0
Sum From 2To 2is 28.0
我不太确定这是不是你想要的。
添加:
代码片段:
while (scan.hasNext()) {
int Numbers = scan.nextInt();
String numberStr = "" + Numbers;
double sum = 0;
for (int i = 0; i < Numbers; i++) {
double d = scan.nextDouble();
sum += d;
numberStr += ", " + d;
}
double total = (((sum + 1) * sum) / (2));
output.println("Sum From " + numberStr +" is " + total);
}
输入为:
2 1 2
输出为:
Sum From 2, 1.0, 2.0 is 6.0
您可以创建一个 PrintWriter 来打印到用户创建的输出文件。然后在 scanner.hasNextInt() 期间搜索原始文件。添加接下来的两个整数并使用 "println()"
将它们打印到文件中
public static void main(String[] args) throws FileNotFoundException {
PrintWriter printwriter = new PrintWriter("newfile.txt");
File file = new File("file.txt");
Scanner scanner = new Scanner(file);
int sum = 0;
System.out.println(scanner.hasNextInt());
while(scanner.hasNextInt()){
printwriter.println(scanner.nextInt() + scanner.nextInt());
}
printwriter.close();
}
我的输入文件看起来像这样...
1 2
4 2
2 4
6 4
6 7
2 3
我的输出文件看起来像这样...
3
6
6
10
13
5
我目前正在开发一个程序,该程序获取用户输入的文件,在每一行添加两个数字,然后将答案打印到用户制作的输出文件和 Netbeans 中的 运行 屏幕中。我似乎只能做第一行,打印时,该行的第二个数字甚至都没有显示。
代码:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Sums {
public static void main(String[] args) throws IOException {
// Scanner for user input
Scanner user = new Scanner(System.in);
String inputFileName, outputFileName;
// prepare the input file
System.out.print("Input File Name: ");
inputFileName = user.nextLine().trim();
File input = new File(inputFileName);
Scanner scan = new Scanner(input);
// prepare the output file
System.out.print("Output File Name: ");
outputFileName = user.nextLine().trim();
// processing loop
try (PrintWriter output = new PrintWriter(outputFileName)) {
// processing loop
{
while (scan.hasNext()) {
int Numbers = scan.nextInt();
double sum = 0;
for (int i = 0; i < Numbers; i++) {
sum += scan.nextDouble();
}
double total = (((sum + 1) * sum) / (2));
output.println("Sum From " + Numbers + "To " + Numbers + "is " + total);
}
}
}
}
}
我试过你的代码,它似乎对我有用。
这是我的输入文件内容:
2 1 2
2 3 4
这里是输出文件内容:
Sum From 2To 2is 6.0
Sum From 2To 2is 28.0
我不太确定这是不是你想要的。
添加:
代码片段:
while (scan.hasNext()) {
int Numbers = scan.nextInt();
String numberStr = "" + Numbers;
double sum = 0;
for (int i = 0; i < Numbers; i++) {
double d = scan.nextDouble();
sum += d;
numberStr += ", " + d;
}
double total = (((sum + 1) * sum) / (2));
output.println("Sum From " + numberStr +" is " + total);
}
输入为:
2 1 2
输出为:
Sum From 2, 1.0, 2.0 is 6.0
您可以创建一个 PrintWriter 来打印到用户创建的输出文件。然后在 scanner.hasNextInt() 期间搜索原始文件。添加接下来的两个整数并使用 "println()"
将它们打印到文件中public static void main(String[] args) throws FileNotFoundException {
PrintWriter printwriter = new PrintWriter("newfile.txt");
File file = new File("file.txt");
Scanner scanner = new Scanner(file);
int sum = 0;
System.out.println(scanner.hasNextInt());
while(scanner.hasNextInt()){
printwriter.println(scanner.nextInt() + scanner.nextInt());
}
printwriter.close();
}
我的输入文件看起来像这样...
1 2
4 2
2 4
6 4
6 7
2 3
我的输出文件看起来像这样...
3
6
6
10
13
5