Results 1 to 3 of 3
  1. #1
    vavavoom1 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default 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));

    }

    }

    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default

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

    db

  3. #3
    vavavoom1 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. calculate the angel
    By gedas in forum New To Java
    Replies: 0
    Last Post: 03-28-2011, 06:31 PM
  2. fastest way to calculate the sum of 1 to x
    By imorio in forum New To Java
    Replies: 5
    Last Post: 11-07-2010, 10:47 PM
  3. Calculate Interest
    By hacikho in forum New To Java
    Replies: 4
    Last Post: 10-09-2010, 04:15 AM
  4. calculate fft
    By ram.west in forum Advanced Java
    Replies: 2
    Last Post: 08-27-2008, 03:05 AM
  5. Calculate what e1 and e2 should be
    By Legoland in forum New To Java
    Replies: 11
    Last Post: 07-02-2007, 06:01 PM

Posting Permissions

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