accessing an arraylist using the get command and inputting into an output file in java

StackOverflow https://stackoverflow.com/questions/23644202

  •  22-07-2023
  •  | 
  •  

Pregunta

I'm having issues with a program I'm trying to write. I'm using an arraylist for storing strings and having issues accessing it and writing it to an output file with other data sets so they form uniform columns. The issue I'm having is when I try to access the data in the arraylist and increment it all I get are numbers 1-10 and not the actual data sets. here's my code and the output file.

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 class

when I output the data it looks something like this:

1 100000 10000 20000 70000

when I want the output to say:

Donald 100000 10000 20000 70000

and so forth using all the strings within the arraylist.

¿Fue útil?

Solución

when you retrieve the value from the list you do not save it to a variable

try

  String name = list.get (nameCounter);
  outputFile.println (name + ....

Also change this code

  // 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);

to

  // 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
    }

Otros consejos

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++;
    //...
}

when I try to access the data in the arraylist and increment it"

You can't increment Strings.

all I get are numbers 1-10

That's because here:

outputFile.println(nameCounter + tools.leftPad(...)...);
                   ^^^^^^^^^^^

You're printing nameCounter, and not the name you want

What you probably want to do is save the value of list.get(nameCounter) into a String, then use that variable instead of nameCounter in your print statement.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top