Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default help...! about reading a text file and finding their average

    i am new to java.. can please help me find the solution of this problem.. i am studying for our removal this oct.21 can you please help me find this out so that.. i have enough(i think) knowledge if i encounter this problem again...

    so sir this is the input of my textfile..

    Jin Kazama 74 75 77 78 79
    Kazuya Mishima 70 72 73 74 75
    Lei Wulong 90 89 73 67 89

    and this is my java code...

    import java.util.Scanner;
    import java.io.*;
    public class ScoreAverager
    {
    //default constructor
    public ScoreAverager implements ()
    {
    }
    //method to read the file, average the grades, and print the new file
    //an IO exception is thrown if the file can't be read or a new file cannot be written to
    public void gradeAvg() throws IOException

    {
    Scanner input = new Scanner(System.in);
    double sum = 0;
    int count = 0;
    double classAverage = 0;
    String currentClass = null;
    File inFile = new File("C:\\Users\\Nemesis\\Desktop\\grade.txt");
    PrintWriter output = new PrintWriter(new File("C:\\Users\\Nemesis\\Desktop\\grades1.txt"));
    try
    {
    input = new Scanner(inFile);
    while(input.hasNext())
    {
    currentClass = input.next();
    int var1 = input.nextInt();
    while(var1 != -9999)
    {
    sum += var1;
    count++;
    var1 = input.nextInt();
    }
    classAverage = sum/count;
    System.out.println(currentClass);
    System.out.println(classAverage);
    output.println("Class : " + currentClass);
    output.println("Class Average: " + classAverage);
    output.println();
    }
    }

    catch (IOException e)
    {
    System.out.println(e);
    System.exit(0);
    }
    finally
    {
    output.close();
    // input.close();
    }
    }
    ;
    }

    error:

    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.

    :confused::confused::confused::confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    so where is the main method?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Main method is the entry point of the Java application like this. So you have to define it and start execution from there.

    Even though without the main method can you compile this code. Before run you have to compile it. I'm wonder how did you get this error. There are lots of errors I can see on your code.
    Last edited by Eranga; 10-17-2008 at 06:13 AM.

  4. #4
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default

    i got it.. i have my new code.. but i still have a new problem.. is how to sort their grades.. btw.. this is my new code

    /**
    * @(#)textFileRead.java
    *
    *
    * @author
    * @version 1.00 2008/10/17
    */

    import java.io.*;
    import java.util.*;
    public class textFileRead {

    static Scanner console = new Scanner(System.in);

    public static void main(String[]args) throws FileNotFoundException
    {

    Scanner inFile = new Scanner(new FileReader("C:\\Users\\Nemesis\\Desktop\\grade.txt "));
    //PrintWriter outFile = new PrintWriter("C:\\Documents and Settings\\elson.KINGJIM\\Desktop\\grade.out");

    int num,count=0;
    double tot=0;
    num = inFile.nextInt();
    double sum = 0;
    double sort[]=new double[num];
    //String First[]=new String[num*2];
    //String Last[]=new String[num*2];
    for(int i=0;i<num;i++)
    /*while(inFile.hasNext())*/
    {

    String frstName =inFile.next();
    String lstName = inFile.next();
    double g1 = inFile.nextInt();
    double g2 = inFile.nextInt();
    double g3 = inFile.nextInt();
    double g4 = inFile.nextInt();
    double g5 = inFile.nextInt();
    tot=g1+g2+g3+g4+g5;
    sum=tot/5;

    if(sum>=75)
    {
    System.out.printf("\n %s %s %.2f %.2f %.2f %.2f %.2f and the average is : %.2f Passed",frstName,lstName,g1,g2,g3,g4,g5,sum);
    sort[count++] = sum;


    //First[count++] = frstName;
    //Last[count++] = lstName;
    }
    else
    {
    System.out.printf("\n %s %s %.2f %.2f %.2f %.2f %.2f and the average is : %.2f Failed",frstName,lstName,g1,g2,g3,g4,g5,sum);
    sort[count++] = sum;


    //First[count++] = frstName;
    //Last[count++] = lstName;
    }
    }
    System.out.println("\n\nSorted Grades");
    System.out.print("-----------------------------\n");



    Arrays.sort(sort);
    for(int i=0;i<sort.length;i++)
    {
    System.out.println(" "+sort[i]);



    }

    System.out.println();
    // outFile.close();
    inFile.close();
    }


    }

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    use code tags. [code][/ code] (without spaces)
    I die a little on the inside...
    Every time I get shot.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I see the sort method being called in your code. Is the results of that correct?
    If not, can you explain.
    Last edited by Norm; 10-17-2008 at 07:06 PM. Reason: correct typo

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You mean sorting the array? Can you explain it more. To sort an array, there are lots of approaches.

  8. #8
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default

    there is no error here.. but sir.. you can see that the code sort their average.. hmm.. how can i sort by their grades? hmm

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    how can i sort by their grades?
    What would be the results of that sort?
    The student(s) with the highest grade first. It would be possible for all of the students to have one of their grades be the highest grade and also the lowest grade. Say they all got 100 on one test and they all got 0 on another. How would they sort?

  10. #10
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default

    ascend sir.. thanks for the time.. ^^

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  12. #12
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default

    yes, i dont know how to sort the grades of each names.. hmm help please

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Before that you have do a lot in your code. Start from the beginning. The way you are work out to read the file is incorrect. Basically input is mismatch there. Better to fix it first.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Are you asking how to sort the grades for a student?
    Where do you store the grades when you read them into the program? If they are in some kind of collection like an array, then you need to have a sort routine to sort the contents of that collection.

  15. #15
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default

    in the console.. do you have any codes?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    in the console..
    What does that mean?

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by nemesis View Post
    in the console.. do you have any codes?
    Can you clearly explain what you really want to know.

  18. #18
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default

    here sir.. i want to sort their different grades.. then compile it in the java console.. and run.. jejE sorry just a beginner in java.. jejE

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    then compile it in the java console
    What does that mean?
    You compile a java source program with the java command.
    The java console is where the browser displays STD output from the jre.
    Are you asking how to output the results of the sort to the console?
    Use the System.out.println() method to put output on the console.

  20. #20
    nemesis is offline Member
    Join Date
    Oct 2008
    Posts
    15
    Rep Power
    0

    Default

    i got it now.. thanks for the dude sir.. and others.. ^^ so this case is closed.. haha

Page 1 of 2 12 LastLast

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 05:45 PM
  2. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  3. Reading a zip/text file present in a MAC Bundle
    By Prasannaa in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2008, 03:05 PM
  4. Reading two text file and sum them up
    By matt_well in forum New To Java
    Replies: 36
    Last Post: 07-22-2008, 02:55 AM
  5. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 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
  •