读一对在一条线的数字在文件

时间:2017-03-11 05:02:38

标签: java java-io

我目前正在开发一个获取用户输入文件的程序,在每一行上添加两个数字,然后将答案输出到用户自制的输出文件和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 个答案:

答案 0 :(得分:0)

我尝试了你的代码,它似乎对我有用。

这是我的输入文件内容:

  

2 1 2

     

2 3 4

这是输出文件内容:

  

从2到2的总和是6.0

     

总和从2到2是28.0

我不太确定这是不是你想要的。

ADD:

代码段:

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

输出结果为:

  

总和从2,1.0,2.0是6.0

答案 1 :(得分:0)

您可以创建PrintWriter以打印到用户输出的文件。然后在scanner.hasNextInt()中搜索原始文件。添加下两个整数并使用“println()”

将它们打印到文件中
function msgRequest() {
  return $.post('/admin/notify/messages', {
    "_token": "{{ csrf_token() }}"
  }).promise()
}

function readAll(messages) {
  return $.when.apply(null, $.map(messages, function(index, message) {
    return $.post('/admin/notify/read', {
      notify_id: message.id,
      "_token": "{{ csrf_token() }}"
    }).promise()
  }));
}

function getUnread() {
  return $.post('/admin/notify/unread', {
    "_token": "{{ csrf_token() }}"
  }, function(unread) {
    $('#unread').text(unread);
    return unread
  });
}

$('#readall').click(function() {
  msgRequest().then(readAll).then(getUnread)
});

我的输入文件看起来像这样......

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