Results 1 to 3 of 3
Thread: Need help to calculate avg
- 04-02-2011, 07:14 PM #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));
}
}
}
}
- 04-02-2011, 07:20 PM #2Can somebody please help me?
db
- 04-02-2011, 07:42 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
calculate the angel
By gedas in forum New To JavaReplies: 0Last Post: 03-28-2011, 06:31 PM -
fastest way to calculate the sum of 1 to x
By imorio in forum New To JavaReplies: 5Last Post: 11-07-2010, 10:47 PM -
Calculate Interest
By hacikho in forum New To JavaReplies: 4Last Post: 10-09-2010, 04:15 AM -
calculate fft
By ram.west in forum Advanced JavaReplies: 2Last Post: 08-27-2008, 03:05 AM -
Calculate what e1 and e2 should be
By Legoland in forum New To JavaReplies: 11Last Post: 07-02-2007, 06:01 PM
Bookmarks