我想写的程序有问题。我使用数组来存储字符串,访问字符串并将其与其他数据集一起写入输出文件,从而使它们形成统一的列。我遇到的问题是,当我试图访问数组列表中的数据并增加它时,我得到的只是数字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();} // End main
} // End类
当我输出数据时,它如下所示:
1 100000 10000 20000 70000
当我想让输出说:
唐纳德100000 10000 20000 70000
等等,使用数组列表中的所有字符串。
发布于 2014-05-14 01:37:23
当从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
}发布于 2014-05-14 01:30:26
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++;
//...
}当我试图访问数组列表中的数据并增加它时“
您不能增加Strings。
我得到的只有1号-10号
因为在这里:
outputFile.println(nameCounter + tools.leftPad(...)...);
^^^^^^^^^^^你在打印nameCounter,而不是你想要的名字
您可能要做的是将list.get(nameCounter)的值保存到String中,然后在print语句中使用该变量而不是nameCounter。
https://stackoverflow.com/questions/23644202
复制相似问题