从头到尾将整数数组打印到文本文件

Array Print Integers to Text File from First to Last

所以我在我的程序中的某个点,该方法必须使用对话框调用传递字符串 "Enter output file name: " 的前一个方法,它必须打开具有该名称的输出文本文件,然后它必须将整数从第一个到最后一个打印到文件中,然后关闭文件。不应有 return 值。

main方法是老师提供的,不会改,问题出在最后一个(第三个)方法print上。我收到 "in" 无法解析为变量的错误,因此我的程序不会 运行.

我是否需要创建扫描仪或简单地修改我的 PrintWriter 以获取输出文件?非常感谢您能为我提供的任何帮助。

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.*;

public class Project5 
{
    static final int LIMIT = 25;    // Max ints in array

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

    int [] numbers = new int [LIMIT];
    int count = getInput(numbers);  // Calls getInput Method

    System.out.println (count + " values were read.");
    print (numbers, count);   // prints original list
    sort (numbers, count);    // calls the sort method
    print (numbers, count);   // prints sorted list

    System.out.println("Program complete.");
}   

/**
 * This method gets the input text file name from the user through the use of a dialog box.
 * It then returns the string entered by the user.
 * @param prompt, the dialog box presented to the user requesting the input file name.
 */
public static String getFileName (String prompt)
{
    // Read in a string from a dialog box
    String in = JOptionPane.showInputDialog(prompt);
    return in;
}

/**
 * This method gets the user's input from the previous method, opens the text file and stores the integers into the array.
 * It then returns the count of how many values are read and closes the file.
 * @param numbers, array to be filled with the integers from the input text file
 */
public static int getInput (int[] numbers)
{
    String inputFileName = getFileName("Enter input file name: "); // call getFileName method passing in string "Enter input file name"
    File inputFile = new File(inputFileName);                      // open input text file with that name
    Scanner input = new Scanner(System.in);
    // read integers from file and store into array
    int count = 0;
    while (input.hasNextInt() && count < numbers.length) // integers to read and array is not full
    {
        numbers[count] = input.nextInt(); 
        count++;
    }
    input.close();   
    return count;    
}

/**
 * This method calls the getFileName method, opens the output text file   with the given name,
 * and prints the integers to the file from the first to the last.
 * @param numbers, the array to be counted
 * @param count, the number of integers in array
 */
public static void print (int[] numbers, int count) // array to print and count of how many integers are in the array
{
    String outputFileName = getFileName("Enter output file name: ");   // Call getFileName
    PrintWriter output = new PrintWriter(outputFileName);   // Create output file

    // print integers to file from first to last
    int position = 0;
    while (position < numbers.length)
    {
        output.print(in.nextInt); // THIS IS WHAT DOESN'T WORK
        position++;
    }
    output.close();
}

您已经将输入的所有数字存储在 numbers 中。试图再次阅读它们是没有意义的。您需要做的就是执行数组访问以获取给定 position:

处的每个元素
output.print(numbers[position]);

Java 的数组是 0 索引的,因此给定包含素数的数组,例如,命名为 array:

array[0] -> 2
array[1] -> 3
array[2] -> 5
array[3] -> 7
array[4] -> 11
...

你的循环条件也可能是错误的。您打印数组中的所有数字 (position < numbers.length)

同时,我假设您只想打印 count 个数字:

while (position < count) { 
    // ...

同样重要的是要注意你应该在关闭之前刷新输出。

所以,

output.flush();
output.close();