我正在尝试写一个程序的问题。我正在使用一个arraylist来存储字符串,并且在访问它时遇到问题并将其写入带有其他数据集的输出文件,以便它们形成统一的列。我遇到的问题是当我尝试访问arraylist中的数据并将其增加时,我得到的是数字1-10而不是实际的数据集。这是我的代码和输出文件。
import java.util.ArrayList;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.*;
public class RobertGardner_6_12
{
public static void main(String[] args) throws IOException
{
// Declare variables
// File names
final String INPUT_FILE_NAME = "RobertGardner_6_12_Input.txt";
final String OUTPUT_FILE_NAME = "RobertGardner_6_12 _Output.txt";
// Append current file boolean variable
final boolean APPEND_INDICATOR = false; // Create a new file
double savings = 0; // The sum of the numbers
double debt = 0; // 20% towards debt
double moneyForYou = 0; // 70% for spending
double oneNumber = 0; // A single number read from the file
String processPhrase; // Indicates appending or creating a file
// Access the input and output files
try
{
File inputDataFile = new File(INPUT_FILE_NAME);
Scanner inputScanner = new Scanner(inputDataFile);
}
catch( FileNotFoundException e)
{
System.err.println( "Error opening file.");
}
File inputDataFile = new File(INPUT_FILE_NAME); //Access input
Scanner inputScanner = new Scanner(inputDataFile);
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE_NAME,
APPEND_INDICATOR);
PrintWriter outputFile = new PrintWriter(outputDataFile);
// Access the Toolkit using the variable 'tools'
Toolkit_General tools = new Toolkit_General();
// Format numeric output
DecimalFormat formatter = new DecimalFormat("#,##0.00");
if(APPEND_INDICATOR)
processPhrase = "Appending";
else
processPhrase = "Creating";
// Display file information on console
System.out.println("Reading file " + INPUT_FILE_NAME + "\n"
+ processPhrase + " file " + OUTPUT_FILE_NAME + "\n");
// Display heading in output file
if (inputScanner.hasNext())
{
outputFile.println("Yearly Income Report");
outputFile.println("-----------------------");
}
outputFile.println("Name Income 10% saved " +
"20% to debt Yours to spend");
outputFile.println("------------------------------------------" +
"----------------------------------");
ArrayList<String> list = new ArrayList<String>();
list.add("Donald");
list.add("Jean");
list.add("Christopher");
list.add("Martin");
list.add("Thomas");
list.add("Samuel");
list.add("George");
list.add("Quentin");
list.add("Magaret");
list.add("Toby");
int nameCounter = 0; //
// Read the input file and sum the numbers
while (inputScanner.hasNext())
{
oneNumber = inputScanner.nextDouble();
savings = oneNumber * .1;
debt = oneNumber * .2;
moneyForYou = oneNumber *.7;
list.get(nameCounter);
nameCounter++;
outputFile.println(nameCounter + tools.leftPad(oneNumber, 18, "#0", " ") +
tools.leftPad(savings, 18, "#0", " ")
+ tools.leftPad(debt, 18, "#0", " ") + tools.leftPad(moneyForYou, 18, "#0", " "));
}
// Close files
outputFile.close();
inputScanner.close();
} //结束主要
} //结束课
当我输出数据时,它看起来像这样:
1 100000 10000 20000 70000
当我想要输出时说:
唐纳德100000 10000 20000 70000
使用arraylist中的所有字符串等等。
答案 0 :(得分:0)
int nameCounter = 0;
while (inputScanner.hasNext()) {
//...
list.get(nameCounter); //<-- This line returns something.
// But you never keep a reference to it!
// So how do you expect to do anything with it?
nameCounter++;
//...
}
当我尝试访问arraylist中的数据并将其递增时“
您无法增加Strings
。
我得到的只是数字1-10
那是因为在这里:
outputFile.println(nameCounter + tools.leftPad(...)...);
^^^^^^^^^^^
您正在打印nameCounter
,而不是您想要的名称
您可能要做的是将list.get(nameCounter)
的值保存到String
,然后在打印语句中使用该变量而不是nameCounter
。
答案 1 :(得分:0)
从list
检索值时,不将其保存到变量
尝试
String name = list.get (nameCounter);
outputFile.println (name + ....
同时更改此代码
// Access the input and output files
try {
File inputDataFile = new File(INPUT_FILE_NAME);
Scanner inputScanner = new Scanner(inputDataFile);
} catch (FileNotFoundException e) {
System.err.println("Error opening file.");
}
File inputDataFile = new File(INPUT_FILE_NAME); //Access input
Scanner inputScanner = new Scanner(inputDataFile);
到
// Access the input and output files
Scanner inputScanner = null;
try {
File inputDataFile = new File(INPUT_FILE_NAME);
inputScanner = new Scanner(inputDataFile);
} catch (FileNotFoundException e) {
System.err.println("Error opening file.");
e.printStackTrace();
return; // no point in continuing
}