【问题标题】:Import integers from a file, multiply them, then export to a new file从文件中导入整数,将它们相乘,然后导出到新文件
【发布时间】:2016-03-02 20:59:31
【问题描述】:

我正在为编程原理 1 做家庭作业,但我正在努力弄明白。

作业状态:

如果文件不存在,则编写一个程序来创建一个新文件(例如,名为 scaled.txt)(如果该文件存在,则终止您的程序而不做任何事情。)。将现有文件中的所有数字与整数(例如 original.txt)乘以 10,并将所有新数字保存在新文件中(例如 scaled.txt)。

例如,如果现有文件(original.txt)是:

26
12
4
89
54
65
12
65
74
3

那么新文件(scaled.txt)应该是:

260
120
40
890
540
650
120
650
740
30

这是我目前写的:

        //Bronson Lane 11/28/15
        //This program reads a file, multiplies the data in the file by           10, then exports that new data
        //to a new file
        package hw12;
        import java.io.File;
        import java.io.PrintWriter;
        import java.util.Scanner;
        public class HW12Part2 {
           public static void main(String[] args) throws Exception{
              File file1 = new   File("/Users/bronsonlane/Documents/workspace/hw12/original.rtf");
              if (!file1.exists()) {
                 System.out.println("File does not exist");
                 System.exit(0);
              }
              int newNum = 0;
              Scanner input = new Scanner(file1);
              while (input.hasNextInt()) {
                 int num = input.nextInt();
                 newNum = num * 10;
              }

              PrintWriter output;
              File file2 = new File("/Users/bronsonlane/Documents/workspace/hw12/scaled.rtf");
              if (file2.exists()) {
                 System.exit(0);
              }
              output = new PrintWriter(file2);
              while (input.hasNextInt()) {
                    int num = input.nextInt();
                    newNum = num * 10;
                    output.println(newNum);
              }
              output.close();
        }
    }

我现在的问题是控制台正在输出:

文件不存在

不管文件在那里。

我认为这是让它打印到新文件的最佳方法,但显然不是,因为它根本不起作用。

我知道我缺少一些完成该计划的关键要素,但我就是想不通。

*编辑 1:更新代码和新问题 *编辑2:Doh!我愚蠢地把错误的文件路径。一切正常,程序按预期运行。

【问题讨论】:

  • 您应该说明您遇到的具体问题。它在输出什么吗?输出有问题吗?等
  • @Pushkin 啊,是的。谢谢你。我现在将编辑帖子。
  • 调用nextInt()时,你应该使用hasNextInt(),而不是hasNextLine()

标签: java java.util.scanner java-io printwriter


【解决方案1】:

对您的答案进行以下更改,

PrintWriter output;
File file2 = new File("scores.txt");
if (file2.exists()) {
                System.exit(0);
}
output = new PrintWriter(file2);
 while (input.hasNextLine()) {
                int num = input.nextInt();
                int newNum = num * 10;
               output.print(newNum);
            }

【讨论】:

  • 应该是 output.println();或者所有整数在一行中一个接一个地相加。
  • 所有整数一一读取并添加到file2中,因此在while循环结束时file2将准备就绪。请注意,您希望如何将数据放入 file2,它在您手中,我只是给您提示。所以意味着你想要逗号或空格的地方你可以在 output.print(...) 处进行。
  • 我只评论了这一点,因为有问题的请求输出每行显示一个数字。
【解决方案2】:

您可以在读取中打开文件,在读取文件行 y 行时,您可以直接将更新的行输出到新文件中。请参见下面的代码示例,该示例使用整数打开文件并更新整数,然后将它们添加到另一个文件中:

public static void main(String[] args) throws FileNotFoundException {
        //open file containing integers
        File file = new File("C:\\test_java\\readnumbers.txt"); 

        //instruct Scanner to read from file source
        Scanner scanner = new Scanner(file); 

        //create file object for output
        File outFile = new File("C:\\test_java\\incremented_numbers.txt");

        //print writer with source as file 
        PrintWriter writer = new PrintWriter(outFile); 

        //for each line in input file 
        while(scanner.hasNext()) {
            //get the integer in the line 
            int number = scanner.nextInt(); 
            number *= 10; 

            //write the updated integer in the output file
            writer.println(number);
        }

        //close both streams
        scanner.close();
        writer.close();
    }

【讨论】:

    【解决方案3】:

    您已经在 while 块中声明了 newNum,即,您将 int newNum... 放在了该 while 块的大括号中。这意味着该变量仅在该块内有意义。在该块之外声明变量,因此它具有整个方法的范围,例如int newNum = 0; 然后在 while 块中使用它(不带 int),然后它将在以后可用。

    我注意到,在查看它时,您也只有一个写入新文件的语句。也许你已经走到了这一步,还没有写出来……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多