# Thread: Need help to calculate avg

1. Member Join Date
Apr 2011
Posts
2
Rep Power
0

## Need help to calculate avg

So I am extremely new to Java, and I do not know how to find the average. So basically want this code does is read lines from a text file and performs simple calculator operations depending on what is written in the text file. SO for example, the one line of the text file could read + 5 6, and the code is set up to read and print out + 5 6 = 11. Unfortunately I cannot figure out how to do the average. (The average on the text line would look like:
average 11.2 -73.1 12)

Can somebody please help me?? Here is my code:

import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.*;

public class abc
{
public static void main (String[ ] args) throws IOException
{
DecimalFormat fmt = new DecimalFormat("#0.000");
String line,command;
double answer,a,b;
int numop;
Scanner fileScan, lineScan;
fileScan = new Scanner (new File("abctext.txt"));

line = fileScan.nextLine();
lineScan = new Scanner(line);
numop = lineScan.nextInt();

for(int count=1; count <= numop; count++)
{
line = fileScan.nextLine();
lineScan = new Scanner(line);
command = lineScan.next();

if(command.equals("+"))
{
a=lineScan.nextDouble();
b=lineScan.nextDouble();
answer = a+b;
System.out.println(line+" = "+ fmt.format(answer));
}

if(command.equals("average"))
{
int count2=0;
double sum=0;
double average=sum/count2;
answer= average;
while(lineScan.hasNext())
{
a=lineScan.nextDouble();
sum= a;
count2++;

}
System.out.println(line +"= "+ fmt.format(answer));

}

}

}

}  Reply With Quote

2. ## Can somebody please help me?
Only if you ask a specific question. Recommended reading: How to ask questions the smart way

db  Reply With Quote

3. Member Join Date
Apr 2011
Posts
2
Rep Power
0

## nevermind I got it.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•