Results 1 to 20 of 20
  1. #1
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Code to calculate mode using array

    Hopefully someone here can help. I have to write a code that does a few simple tasks, but is becoming really tricky. The code has to read an input file made up of integers. These integers range from positive to negative and include zero. Each integer has it's own line. The code must read this file, convert it into an array, and then calculate the "mode" which is the most repeated number. Once that's complete it must display it's output back to the user. I'm stil fairly new to java so I'm having a lot of difficulty even getting started with this program. Any help would be appreciated. Thanks

    -Nox

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

    Default Re: Code to calculate mode using array

    Do it in small steps.
    First write code to read the file and display its contents. See the Scanner class for useful methods.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Code to calculate mode using array

    Whch: "is becoming really tricky" or "having a lot of difficulty even getting started"?

    If the problem is getting started there is no substitute for going back over your notes and textbook to see how what the general structure of Java programs is. On the other hand if you have met with some specific obstacle with program you should post your code and ask a specific question about it.

  4. #4
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Code to calculate mode using array

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Lab0 { public static void main(String[] args) throws FileNotFoundException { String fileName = mode.txt";
    File in = new File(fileName);
    int frequency = countValues(in, 9);
    System.out.println("\"9\" Count: " + frequency);
    }
    static int countValues(File inFile, int value) throws FileNotFoundException {
    int freq = 0;
    Scanner fileScanner = new Scanner(inFile);
    while (fileScanner.hasNextInt()) {
    int input = fileScanner.nextInt();
    if (input == value) {
    freq++; }
    }
    return freq;
    }
    }
    This is the code using the hint given. To my knowledge it tracks the occurrence of a single value. Thus I need to come up with an array that reads all digits in the file and tracks their occurrence, then returns the most frequent value. Hopefully this helps
    Last edited by Norm; 09-06-2011 at 04:12 PM. Reason: added code tags

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

    Default Re: Code to calculate mode using array

    need to come up with an array that reads all digits in the file and tracks their occurrence,
    To use an array, you will need to make it big enough to hold all the numbers that are to be read into it.
    It can be bigger with no problems, say make it with 100 elements or 10000.
    Then you need an index to point into the array at the next empty slot where you can save the data. you will increment the index as you add numbers to the array.

  6. #6
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Code to calculate mode using array

    I see what you mean. Here's where I get stuck. I can explain, in English exactly what has to happen, its writing the code that confuses me because I haven't touched Java in months. I do know that once I make the array that can read the file, I have to create some sort of loop that reads the lines of data. Then find a way to calculate the most used number. I was leaning on making an for loop.

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

    Default Re: Code to calculate mode using array

    once I make the array that can read the file
    Terminology here is incorrect. arrays hold data, code & methods read files

    I have to create some sort of loop that reads the lines of data
    The code in post#4 has a loop. What does that loop do?

  8. #8
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Code to calculate mode using array

    I'm working on an answer to that. I keep getting unclosed string literal in the line
    public class Lab0 { public static void main(String[] args) throws FileNotFoundException { String fileName = mode.txt";

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

    Default Re: Code to calculate mode using array

    unclosed string literal
    You need a " at both ends for the compiler to know what part of the source is in the literal and what is not in the literal.

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

    Default Re: Code to calculate mode using array

    BTW this is poor coding style:
    public class Lab0 { public static void main(String[] args) throws FileNotFoundException { String

    Don't put any more source on a line following a {
    That makes it hard for people trying to read the code to see the { (which is important) .
    Also the ending } should be underneath and in vertical alignment with the start of the line of code with the beginning {.

    Code within matching {}s should be indented 3-4 spaces so it is easy to see matching {}s. Again very important for people trying to read your code.

  11. #11
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Code to calculate mode using array

    Yeah I got it now. I'm about to just start over.... I fixed all the mistakes in the hint and I'm still getting

    Exception in thread "main" java.io.FileNotFoundException: Lab0_input.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.util.Scanner.<init>(Scanner.java:636)
    at Lab0.Lab0_Main_sellio7.countValues(Lab0_Main_selli o7.java:19)
    at Lab0.Lab0_Main_sellio7.main(Lab0_Main_sellio7.java :14)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    Line 14 is
    int frequency = countValues(in, 9);
    Line 19 is
    Scanner fileScanner = new Scanner(inFile);

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

    Default Re: Code to calculate mode using array

    FileNotFoundException: Lab0_input.txt (The system cannot find the file specified)
    That should be self explanatory.

  13. #13
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Code to calculate mode using array

    It makes no sense why it can't find the file. I have in the same package on my Netbeans. It's saved on my computer, and everything is spelled correctly. Am I missing something.

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

    Default Re: Code to calculate mode using array

    Your IDE is confusing your program. I have no idea where your IDE will set the current directory when you execute your program.
    Since I don't know where the program looks the only suggestion I have is to make many copies of the file and put one in every possible place the IDE could set the current directory for when your program executes and hope that you put one where it is looking.

    Another suggestion: Use the FULL path to the file starting with the driver letter vs only using a filename.

  15. #15
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Code to calculate mode using array

    Yeah I ended up doing the same thing. Alright so my output now is
    ....
    "9" Count: 8
    BUILD SUCCESSFUL (total time: 0 seconds)
    So would it be smart to construct a loop that checks all integers and reports their frequencies. If so can I please get some help getting started. Java is like spanish to me lol

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

    Default Re: Code to calculate mode using array

    What does your code do now?

  17. #17
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Code to calculate mode using array

    It reports a single digits frequency. For instance, right now it reports the number 9 occurs 8 times.

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

    Default Re: Code to calculate mode using array

    It reports a single digits frequency.
    What does it do so that it can make that report?
    Does it read the file and store the contents of the file in an array? One number to each element of the array.

  19. #19
    Nox_1031 is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: Code to calculate mode using array

    There is no array created at this point in time
    I do however have another code that was given with ideas on a correct array so I will implement that in to my current code.

    Unfortunately I have to leave for work and won't be back for another 6 hours or so. I really appreciate the help and hopefully I can continue this thread when I return.

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

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2011, 06:43 AM
  2. Replies: 12
    Last Post: 02-08-2011, 02:58 PM
  3. Replies: 1
    Last Post: 02-08-2011, 09:11 AM
  4. Finding the Mode in An Array
    By carlodelmundo in forum New To Java
    Replies: 23
    Last Post: 10-31-2010, 12:44 PM
  5. [SOLVED] Array Mode Problem
    By Banjo in forum New To Java
    Replies: 6
    Last Post: 12-09-2008, 04:35 AM

Posting Permissions

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