4 Replies - 495 Views - Last Post: 14 November 2008 - 10:02 AM Rate Topic: -----

#1 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2727
  • View blog
  • Posts: 15,804
  • Joined: 24-September 08

What would be the easiest way of going about this?

Posted 14 November 2008 - 09:27 AM

Quote

2. Retrieve the names of the precinct files from reporting.in and as you read each precinct file name, open the corresponding file (precinct#.in) record the votes, and close the file.


I was thinking as I read it line from line, store each letter into an array element, and compare the index of the princinct number with an interger (switch?) and open the coresponding file. Is there an easier way?

reporting.in said:

precinct3
precinct1
precinct4
precinct0

(I have to assume that I do not know that princint2 is not used. It is a general program.

Is This A Good Question/Topic? 0
  • +

Replies To: What would be the easiest way of going about this?

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: What would be the easiest way of going about this?

Posted 14 November 2008 - 09:34 AM

Use a while loop to scan through the data until the end of the file, and store the data in an array...Or if you (theoretically) don't know the amount of precincts in the .in file, then store them in an ArrayList or something.

Create a File object that will be read.

Loop through the array that holds the file names.

File file;
int candidate1 = 0, candidate2 = 0;

for (int x = 0; x < SIZE_HERE; x++)
{
    file = new File(array_or_ArrayList_member_here);

    // read data, compare it to figure out who the vote is for.
}


Sorry if I was unclear at all, I'm in C++ class. :D

Hope this helps! :)

This post has been edited by Locke37: 14 November 2008 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2727
  • View blog
  • Posts: 15,804
  • Joined: 24-September 08

Re: What would be the easiest way of going about this?

Posted 14 November 2008 - 09:45 AM

That looked Chinese to me. I know there's a While (file.hasNext) thing, but I know that there is 5 princints. And your answer didn't solve my problem. Thanks for the help, but...
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: What would be the easiest way of going about this?

Posted 14 November 2008 - 09:58 AM

So you know for a fact that there are always going to be 5 precincts...

I'll do the best I can, since I don't know the format that your precinct files will be in.

public class Votes
{
    public static void main(String args[]) throws IOException
    {
        File inputFile = new File("reporting.in");
        String[] precinctNames = new String[5];
        Scanner file = new Scanner(inputFile);
        
        int count = 0;

        while (file.hasNext() && count < 5)
        {
            precinctNames[count] = file.nextLine();
            count++;
        }

        for (int x = 0; x < precinctNames.length; x++)
        {
            inputFile = new File(precinctNames[x] + ".in");
            file = new Scanner(inputFile);

            // use the Scanner to read in the votes and tally them
            // to the corresponding candidate.  I don't know the format
            // of the file(s), so this is the best I can do.
        }
    }
}


Hope this clears it up! :D
Was This Post Helpful? 1
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2727
  • View blog
  • Posts: 15,804
  • Joined: 24-September 08

Re: What would be the easiest way of going about this?

Posted 14 November 2008 - 10:02 AM

thanks!!!! *stalks*
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1