0

I am trying to write a program that uses two classes to find the total $ amount from a text file of retail transactions. The first class must read the file, and the second class must perform the calculations. The problem I am having is that in the first class, the ArrayList only seems to get the price of the last item in the file. Here is the input (which is in a text file):

$69.99 3 Shoes

$79.99 1 Pants

$17.99 1 Belt

And here is my first class:

class ReadInputFile {
static ArrayList<Double> priceArray = new ArrayList<>();
static ArrayList<Double> quantityArray = new ArrayList<>();

static String  priceSubstring = new String();
static String quantitySubstring = new String();

public void gatherData () {

String s = "C:\\filepath";   
try {


      FileReader inputFile = new FileReader(s);

      BufferedReader bufferReader = new BufferedReader(inputFile);

      String line;

      String substring = " ";

      while ((line = bufferReader.readLine()) != null)


      substring = line.substring(1, line.lastIndexOf(" ") + 1);



     priceSubstring = substring.substring(0,substring.indexOf(" "));

      quantitySubstring = substring.substring(substring.indexOf(" ") + 1 , substring.lastIndexOf(" ") );


      double price = Double.parseDouble(priceSubstring);
      double quantity = Double.parseDouble(quantitySubstring);

      priceArray.add(price);
      quantityArray.add(quantity);

      System.out.println(priceArray);


} catch (IOException e) {
     e.printStackTrace();
}
}

The output and value of priceArray is [17.99], but the desired output is [69.99,79.99,17.99].

Not sure where the problem is, but thanks in advance for any help!

1 Answer 1

1

Basically what you have is:

while ((line = bufferReader.readLine()) != null) {
    substring = line.substring(1, line.lastIndexOf(" ") + 1);
}

priceSubstring = substring.substring(0,substring.indexOf(" "));

quantitySubstring = substring.substring(substring.indexOf(" ") + 1 , substring.lastIndexOf(" ") );


double price = Double.parseDouble(priceSubstring);
double quantity = Double.parseDouble(quantitySubstring);

priceArray.add(price);
quantityArray.add(quantity);

System.out.println(priceArray);

So all you are doing is creating a substring of the line you just read, then reading the next line, so basically, only the substring of the last will get processed by the remaining code.

Wrap the code in {...} which you want to be executed on each iteration of the loop

For example...

while ((line = bufferReader.readLine()) != null) {
    substring = line.substring(1, line.lastIndexOf(" ") + 1);
    priceSubstring = substring.substring(0,substring.indexOf(" "));

    quantitySubstring = substring.substring(substring.indexOf(" ") + 1 , substring.lastIndexOf(" ") );

    double price = Double.parseDouble(priceSubstring);
    double quantity = Double.parseDouble(quantitySubstring);

    priceArray.add(price);
    quantityArray.add(quantity);

    System.out.println(priceArray);
}

This will execute all the code within the {...} block for each line of the file

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I had a feeling it was something simple but that had me stuck for quite awhile. This worked perfectly, will accept when 10 minutes are up.
I'm glad it could help. Even if I have a single of code to execute, I always wrap it in a {...} block, might sound silly, but, one, I find it easier to read and two, you don't make mistakes like this one ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.