所以我在我的程序中的一个点,该方法必须调用前一个方法,使用对话框传入字符串“输入输出文件名:”,它必须打开具有该名称的输出文本文件然后它必须从第一个到最后一个打印整数到文件,然后关闭文件。应该没有回报值。
主要方法由教师提供,不得更改,问题出现在名为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();
}
答案 0 :(得分:0)
您已将输入中的所有数字存储在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();