0

I'm trying to figure out how to read data from a file, using an array. The data in the file is listed like this:

Clarkson 80000
Seacrest 100000
Dunkleman 75000
...

I want to store that information using an array. Currently I have something like this to read the data and use it:

            String name1 = in1.next();
            int vote1 = in1.nextInt();


            //System.out.println(name1 +" " + vote1);

            String name2 = in1.next();
            int vote2 = in1.nextInt();

            //System.out.println(name2 +" " + vote2);

            String name3 = in1.next();
            int vote3 = in1.nextInt();
              ...
              //for all names

Problem is, the way I'm doing it means I can never manipulate the file data for more contestants or whatnot. While I can use this way and handle all the math within different methods and get the expected output...its really inefficient I think.

Output expected:

American Idol Fake Results for 2099
Idol Name        Votes Received    % of Total Votes
__________________________________________________      
Clarkson            80,000            14.4%
Seacrest            100,000           18.0%
Dunkleman           75,000            13.5%
Cowell              110,000           19.7%
Abdul               125,000           22.4%
Jackson             67,000            12.0%

Total Votes         557,000

The winner is Abdul!

I figure reading input file data into arrays is likely easy using java.io.BufferedReader is there a way not to use that? I looked at this: Java: How to read a text file but I'm stuck thinking this is a different implementation.

I want to try to process all the information through understandable arrays and maybe at least 2-3 methods (in addition to the main method that reads and stores all data for runtime). But say I want to use that data and find percentages and stuff (like the output). Figure out the winner...and maybe even alphabetize the results!

I want to try something and learn how the code works to get a feel of the concept at hand. ;c

Community
  • 1
  • 1

2 Answers2

1
int i=0  
while (in.hasNextLine()) {  

  name = in.nextLine();
  vote = in.nextInt();


  //Do whatever here: print, save name and vote, etc..
  //f.e: create an array and save info there. Assuming both name and vote are 
  //string, create a 2d String array.
  array[i][0]=name;
  array[i][1]=vote;

  //if you want to individually store name and votes, create two arrays.
  nameArray[i] = name;
  voteArray[i] = vote;
  i++;

}

This will loop until he automatically finds you don't have any more lines to read. Inside the loop, you can do anything you want (Print name and votes, etc..). In this case, you save all the values into the array[][].

array[][] will be this:

array[0][0]= Clarkson 

array[0][1]= 80,000  

array[1][0]= Seacrest

array[1][1]= 100,000 

...and so on.

Also, I can see that you have to do some maths. So, if you save it as a String, you should convert it to double this way:

double votesInDouble= Double.parseDouble(array[linePosition][1]);
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
aran
  • 10,978
  • 5
  • 39
  • 69
  • Wouldn't that constantly replace name/vote with the next line/int until it reaches the end of the document? – Inspiring Wisdomhammer Apr 25 '13 at 22:49
  • No, if you print it. If you want to save it, you can also do it. I'll edit the answer. – aran Apr 25 '13 at 22:50
  • Okay, i see your edit. If I'm looking at it correctly: it does all that until all data in the file is read. Say `name2` is printed with `vote2` is that how the array will name it? Or how would I use that information later to manipulate – Inspiring Wisdomhammer Apr 25 '13 at 22:58
  • When the loop finishes, everything will be saved inside the array[][]. The second position of the array just states that you want : 0 -->name , and 1---> vote. Changing the first position of the array will give you the line you want to search from. – aran Apr 25 '13 at 23:00
  • so I'd print it like this: `System.out.println(array[0][1])` to get the first line? Or if I want to put it in a chart or something – Inspiring Wisdomhammer Apr 25 '13 at 23:03
  • That will give you the first vote. System.out.println(array[0][0]) will give you the first name. – aran Apr 25 '13 at 23:05
  • Can I store the array values into something that makes them easy to call and automatically incremented? Like `vote1 = array[0][1];` but the `1` tied to the word `vote` would be automatic and not hardcoded (so later when I make a chart out of everything, it can sort of easily be done)? – Inspiring Wisdomhammer Apr 25 '13 at 23:10
  • Yes. Look at the edit. There you store the info in two arrays. You can easily check their names and votes. F.e: nameArray[1]=Seacrest; voteArray[0]=80,000 – aran Apr 25 '13 at 23:12
1

You have several options:

  1. create a Class to represent your File data, then have an array of those Objects

  2. maintain two arrays in parallel, one of the names and the other of the votes

  3. Use a Map, where the name of the person is the key and the number of votes is the value

    a) gives you direct access like an array

    b) you don't need to create a class

Option 1:

public class Idol 
{
   private String name;
   private int    votes; 

   public Idol(String name, int votes) 
   {
       // ...
   }

}

int index = 0;
Idol[] idols = new Idol[SIZE];

// read from file
String name1 = in1.next();
int vote1 = in1.nextInt();

//create Idol
Idol i = new Idol(name1, vote1);

// insert into array, increment index
idols[index++] = i;

Option 2:

int index = 0;
String[] names = new String[SIZE];
int[] votes    = new int[SIZE];

// read from file
String name1 = in1.next();
int vote1 = in1.nextInt();


// insert into arrays
names[index] = name1;
votes[index++] = vote1;

Option 3:

// create Map
Map<String, Integer> idolMap = new HashMap<>();

// read from file
String name1 = in1.next();
int vote1 = in1.nextInt();

// insert into Map
idolMap.put(name1, vote1);

Now you can go back any manipulate the data to your hearts content.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • I'm thinking one class with several methods to handle: method to reading file (saving everything within, names and votes separate), method to format all the data and print it, method to manipulate the data (like finding overall percentage)...stuff like that ------Edit: Noticing you are editing your post, I'll refresh if anything new pops up – Inspiring Wisdomhammer Apr 25 '13 at 22:54
  • @InspiringWisdomhammer I think that is probably the cleanest solution, I posted the others because I wasn't really sure what you wanted. – Hunter McMillen Apr 25 '13 at 22:56
  • So, looking at option 1, that reads all the data and saves it? like say later I want to print `name2` will that be nonexistent? – Inspiring Wisdomhammer Apr 25 '13 at 23:01
  • @InspiringWisdomhammer You would have to loop through the array of `Idol` objects until you found one with the name that you wanted. – Hunter McMillen Apr 25 '13 at 23:08