Results 1 to 17 of 17
  1. #1
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Problems with average

    Hello, im trying to write a program which reads info from a file and counts average. Then sort it in different ways: alpha orderd and merit order. Im trying to use hashmap, but I got errors:S
    My text file looks like this:
    Simon 4
    Anna 10
    Simon 4
    Anna 9
    Anna 5
    Edward 10

    Java Code:
    import java.io.FileReader;
    import java.util.*;
    import java.io.IOException;
    
    public class StudentMarks {
        public static void main(String[] args) throws IOException {
            String file ="path.txt";
            Scanner sc = new Scanner(new FileReader(file));
            Map<String, Integer> map = new HashMap<String, Integer>();
            StringTokenizer stok = null;
            while(sc.hasNextLine()){
                stok = new StringTokenizer(sc.nextLine(), " ");
                map.put(stok.nextToken(), Integer.parseInt(stok.nextToken()));
            }
            sc.close();
            Set<Map.Entry<String,Integer>> s = map.entrySet();
            Iterator<Map.Entry<String,Integer>> itr = s.iterator();
            while(itr.hasNext())
                System.out.println(itr.next());
        }
    }
    Last edited by JosAH; 03-19-2012 at 02:57 PM. Reason: fixed [code] ... [/code] tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Problems with average

    What errors are you getting?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problems with average

    Exception in thread "main" java.io.FileNotFoundException: path.txt (The system cannot find the file specified). But i got this file in the same folder where program exists.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Problems with average

    What is your exact directory setup?

    What is the absolute path of that File Object?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Problems with average

    Quote Originally Posted by Lexaras View Post
    Exception in thread "main" java.io.FileNotFoundException: path.txt (The system cannot find the file specified). But i got this file in the same folder where program exists.
    hi,
    what editor u're using?
    i can help if it is eclipse
    open the project working folder, open the folder with project name,paste the 'path'txt' into it. [ the "src" folder contains the actual codings, but it should not contain 'path.txt' rather the project folder should contain it.
    example, the project directory is :"project dir1"
    project name inside it is :"project_average"
    java file is present in:"src" folder
    paste the file 'path.txt' into "project_average"


    regards
    dhilip

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Problems with average

    The editor should not matter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problems with average

    Thank u dhilip, now it reads text file in your way. I got results but the program ignores duplicates. My results from this program was:
    Simon=4
    Edward=10
    Anna=5
    It doesnt count the average but gives only the last marks.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Problems with average

    Where is the code that contains the logic for calculating the average?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problems with average

    It doesnt exist in this program :S Really I dont know how to count all marks for one name using Hashmap..

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Problems with average

    Why are you only storing a single Integer (grade) for each String (name)? Don't you want to store a list of grades for each name?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problems with average

    Map<String, Integer> map = new HashMap<String, Integer>(); So I should chage this line? Integer to list?

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Problems with average

    Quote Originally Posted by Lexaras View Post
    Map<String, Integer> map = new HashMap<String, Integer>(); So I should chage this line? Integer to list?
    What do you think? What happened when you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problems with average

    I got this error after I had changed all integer to list:
    error: cannot find symbol method parseInt(String) from this line map.put(stok.nextToken(), List.parseInt(stok.nextToken())); (parseInt was marked in red color)

  14. #14
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default Re: Problems with average

    Quote Originally Posted by Lexaras
    List.parseInt(
    When you checked the List API, did you find a parseInt() method in there?

  15. #15
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problems with average

    Nop, I cant find it :S

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Problems with average

    That's the point. That method doesn't exist, so you're getting an error when you try to call it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    Lexaras is offline Member
    Join Date
    Feb 2012
    Posts
    22
    Rep Power
    0

    Default Re: Problems with average

    XML Code:
    package Pack_A;
    
    import java.io.FileReader;
    import java.util.*;
    import java.io.IOException;
    
    public class StudentMarks {
        public static void main(String[] args) throws IOException {
            String file ="path.txt";
            Scanner sc = new Scanner(new FileReader(file));
            HashMap<String, List> map = new HashMap<String, List>();
            StringTokenizer stok;
            while(sc.hasNextLine()){
                stok = new StringTokenizer(sc.nextLine(), " ");
                map.put(stok.nextToken(), listIterator(stok.nextToken()));
            }
            sc.close();
            Set<Map.Entry<String,List>> s = map.entrySet();
            Iterator<Map.Entry<String,List>> itr = s.iterator();
            while(itr.hasNext())
                System.out.println(itr.next());
        }
    
    
        private static int listIterator(Integer s) {
            return s++;
        }
    }
    Ive changed that to listIterator, I think its suitable method. It created private static function which, on my opinion counts marks for different people, but it doesnt work and I got this error:
    error: method listIterator in class StudentMarks cannot be applied to given types;
    required: Integer
    found: String
    reason: actual argument String cannot be converted to Integer by method invocation conversion

Similar Threads

  1. Average of an array. Please help
    By vika in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 10:09 PM
  2. average
    By swrta in forum New To Java
    Replies: 3
    Last Post: 03-16-2011, 10:52 AM
  3. average
    By anjigadu in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 10:52 PM
  4. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-13-2010, 12:59 AM
  5. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 12:09 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
  •