Results 1 to 20 of 20
- 09-06-2011, 01:53 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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
- 09-06-2011, 03:45 PM #2
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.
- 09-06-2011, 04:01 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 09-06-2011, 04:10 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
Re: Code to calculate mode using array
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 helpsJava 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; } }Last edited by Norm; 09-06-2011 at 04:12 PM. Reason: added code tags
- 09-06-2011, 04:16 PM #5
Re: Code to calculate mode using array
To use an array, you will need to make it big enough to hold all the numbers that are to be read into it.need to come up with an array that reads all digits in the file and tracks their occurrence,
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.
- 09-06-2011, 04:23 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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.
- 09-06-2011, 04:27 PM #7
Re: Code to calculate mode using array
Terminology here is incorrect. arrays hold data, code & methods read filesonce I make the array that can read the file
The code in post#4 has a loop. What does that loop do?I have to create some sort of loop that reads the lines of data
- 09-06-2011, 04:37 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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";
- 09-06-2011, 04:40 PM #9
Re: Code to calculate mode using array
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.unclosed string literal
- 09-06-2011, 04:44 PM #10
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.
- 09-06-2011, 04:45 PM #11
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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);
- 09-06-2011, 04:48 PM #12
Re: Code to calculate mode using array
That should be self explanatory.FileNotFoundException: Lab0_input.txt (The system cannot find the file specified)
- 09-06-2011, 04:49 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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.
- 09-06-2011, 04:52 PM #14
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.
- 09-06-2011, 04:56 PM #15
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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
- 09-06-2011, 04:58 PM #16
Re: Code to calculate mode using array
What does your code do now?
- 09-06-2011, 04:59 PM #17
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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.
- 09-06-2011, 05:01 PM #18
Re: Code to calculate mode using array
What does it do so that it can make that report?It reports a single digits frequency.
Does it read the file and store the contents of the file in an array? One number to each element of the array.
- 09-06-2011, 05:04 PM #19
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
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.
- 09-06-2011, 05:48 PM #20
Re: Code to calculate mode using array
Cross posted at Program to read file and calculate array - Java
Similar Threads
-
I want to know how to calculate and display the sum of each column in 2D array
By Javanoobs in forum New To JavaReplies: 1Last Post: 03-03-2011, 06:43 AM -
How to write a program to calculate and display sum in two dimensional array?
By Javanoobs in forum New To JavaReplies: 12Last Post: 02-08-2011, 02:58 PM -
How to write a program to calculate and display sum in two dimensional array
By Javanoobs in forum Advanced JavaReplies: 1Last Post: 02-08-2011, 09:11 AM -
Finding the Mode in An Array
By carlodelmundo in forum New To JavaReplies: 23Last Post: 10-31-2010, 12:44 PM -
[SOLVED] Array Mode Problem
By Banjo in forum New To JavaReplies: 6Last Post: 12-09-2008, 04:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks