Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading a text file where there may be one or two rows starting with the word "Final". I need to grab the amount from that row and total them and grab the price and total them. I needs these values stored in variables that I can then access outside of my for loops.
....
Final amount="12" price="10.00"
......
Final amount="3" price="130.00"
.....
So far I can print all the values that I need but I can't get access to them outside of my for loops. code:

Java
static ArrayList<String> lines = new ArrayList<String>();
static String[] FinalEntries;

FinalEntries = null;
for(String line: lines) {   
  if (String.valueOf(line).length() > 15 && line.contains("Final")) {
    FinalEntries = line.split("\"");

    for(int i=0; i < FinalEntries.length; i++) {
      System.out.println(FinalEntries [i]);   
    }     
}


What I have tried:

I have tried adding the items from finalEntries to another array
I have tried accessing the finalEntries[i] outside the for loop
Posted
Updated 2-May-18 9:32am
v2
Comments
AmitGajjar 2-May-18 15:16pm    
What is not working ? store in an array and at the end of loop read that array again.
Kornfeld Eliyahu Peter 2-May-18 15:33pm    
Pay attention to the fact that while FinalEntries is at global level i does not exists outside the loop!!!
Richard MacCutchan 3-May-18 4:19am    
Define two variables outside of the loop, each with value 0. When you read the line containing "Final", extract the relevant numbers and add them to your variables. After the loop ends print the values in the variables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900