This site requires JavaScript, please enable it in your browser!
Greenfoot back
someone1
someone1 wrote ...

2015/9/25

Displaying values in an arraylist from a txt file

someone1 someone1

2015/9/25

#
This should be a pretty simple question So i created an array list(which is a float) and i have a text file that has 4 lines. This is what is stored on the txt file(im focusing on the last numbers which are data type float)- Mark Fitts 10 78.80 Harry Potts 15 60.89 Nancy Welling 13 56.55 Phil Wallberg 23 30.30 my array list name is called sumOfLastValue, now i already have a variable called lastVal which represents the the float values. How would i go about trying to store all the float values and then adding them and displaying the result to the user Right now this is what i have(This is not all of my code
1
2
3
4
5
6
7
8
FileReader fr = new FileReader("clients.txt");
   Scanner keyboard = new Scanner(fr);
 
ArrayList<Float> sumOfLastValue = new ArrayList<Float>();
   while(keyboard.hasNextLine()) {
     sumOfLastValue.add(lastVal);
}
System.out.println(sumOfLastValue);
All this code shows is the first float value on the first line 78.80 But i want to be able to sum all the float values, how would i do this Thanks
davmac davmac

2015/9/25

#
It's hard to know exactly without seeing more of your code. But at the most basic level:
1
ArrayList<Float> sumOfLastValue = new ArrayList<Float>();
You're declaring a variable called 'sumOfLastValue' but the variable is a a list type (specifically ArrayList). A sum is normally a number, not a list. It looks to me as if you are adding numbers (as separate entries) to a list, when you should be adding them to a numeric sum (eg a variable declared as type "float").
danpost danpost

2015/9/25

#
I do not see any need in creating an array for the float values. I would use an int counter field to count the number of floats and a float sum field for each running sum of the floats. Increment the count each time you add a float to the running sums and determine the average by divding the sums by the count when the total sums have been arrived at.
duckbread duckbread

2015/9/26

#
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void main(String[] args) throws FileNotFoundException {
     
    //Try to open the FileReader
    FileReader fr = null;
    fr = new FileReader("clients.txt");
     
    //Create Scanner
    Scanner keyboard = new Scanner(fr);
     
    //Add Elements to ArrayList
    ArrayList<Float> sumOfLastValue = new ArrayList<Float>();
    while(keyboard.hasNextFloat())
    {
        sumOfLastValue.add(keyboard.nextFloat());
    }
     
    //Demonstrate the Program Works
    System.out.println(sumArrayList(sumOfLastValue));
     
    //Close the keyboard
    keyboard.close();
}
 
//Sums the ArrayList
public static float sumArrayList(ArrayList<Float> al)
{
    float sumVal = 0.0f;
    for(float fl : al)
    {
        sumVal += fl;
    }
     
    return sumVal;
}
I was going to do this last night but didn't have the time. Your code never actually sums the elements, the sumArrayList(ArrayList<Float> al) method should do that for you. Conversely, you could use a float to just store the value. By method is less overhead and you don't have to worry about accidentally forgetting an element. By float is faster because the machine doesn't have to compute the value each time you need it. Typically when you print an ArrayList like you did it'll print out something like
1
[78.8, 60.89, 56.55, 30.3]
so I'm a little perplexed as to how you only got the one value. edit: I should clarify that the f after the 0.0 is not a variable. I changed the f in the iterable for loop to fl to avoid confusion because that is a variable.
You need to login to post a reply.