PDA

View Full Version : can someone help me start this?



NeoTifa
11-04-2008, 06:42 PM
i dunno what to do *cries*

Balzac
11-04-2008, 07:16 PM
The pdf is broken.

Try uploading it instead of using attachments.

rubah
11-04-2008, 07:53 PM
It opened for me, but I don't know how to help you xD

NeoTifa
11-04-2008, 08:12 PM
i thought thats what i was? XD

o_O
11-06-2008, 02:31 AM
Ok, there are a bunch of things you need to to here. Your first task is to open the "ballotSetup.in" file for reading so that you can get the necessary data for tabulation. Assuming it's a simple text file, you should use a BufferedReader object. You also need to read and parse the data from the ballotSetup.in file. They've made it fairly simple for you to pull the candidates from the file, and left you to determine how you want to store them. I'd personally use a hash table object:


BufferedReader in = new BufferedReader(new FileReader("ballotSetup.in"));
int numCandidates = Integer.parseInt(in.readLine().trim());

String[] candidates = new String[numCandidates]; // This will store the names of candidates with the array index corresponding to the candidate number.
Hashtable votes = new Hashtable(numCandidates); // This will store the candidate numbers as keys pointing to the number of votes.

for (int i = 0; i < numCandidates; i++) {
String[] cData = in.readLine().trim().split(" ", 2);
String cNum = cData[0]; // Get the candidate number
String cName = cData[1]; // and candidate name

candidates[i] = cName;
votes.put(cNum + "", 0); // Add the candidate number to the hash table with an initial vote count of 0.
}

in.close(); // You have to close the reader or the data is never flushed.


That should be enough to get you started. After that you're gonna need to open reporting.in and suss out the precinct files; then open each precinct file and tally up the votes (in the hash table from the code above). I'd do that in the same way as the ballot setup processing. Tabulating the data will be a lot easier for you if you remember that "\t" expands to a tab character.

For the bonus credit, it'd be very simple to write an implementation of insertion sort that takes an array of names since you can use the Comparable interface to compare the lexicographical ordering strings.

NeoTifa
11-06-2008, 03:27 PM
O.o .... uhh.... cant i just use scanner? XD you are my hero

o_O
11-06-2008, 11:30 PM
You mean like a hardware scanner? :p

One more thing to be careful of - be careful about data types when comparing candidate numbers/votes that you read from the files. BufferedReaders and FileReaders automatically read strings from files, even if the string only contains a number. If you want to be dealing with numbers specifically, then you need to explicitly convert to an integer by either casting "(int)" or by calling the instance method Integer.parseInt(). The lines/characters that are read may also end in a "\n" newline character, so you should call the trim() method on them to avoid this.

NeoTifa
11-07-2008, 02:34 AM
no, you could just use a scanner like so

import java.util.Scanner;

FileReader meatsack = new FileReader ("fileofgheyness.in");
Scanner cockandballs = new Scanner (meatsack);

int nuts = cockandballs.nextInt();

:monster:

i just wanted someone to translate it into lamemans terms :p

o_O
11-07-2008, 11:57 PM
You could do it that way if you want - there's never one set way to achieve something with programming. :p

Just make sure you're scanning for the right data types and the right number of bits of data and you should be fine. I do tend to do a little bit more work for things, to get that extra bit of control, which is why I've always used BufferedReaders to read from files in Java. :p

NeoTifa
11-09-2008, 05:12 PM
scanner is just easier though.