Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm writing Java code. I have files that looks like:

  1. President and Vice-President ==> Fred Flinestone(P)
  2. US Senate ==> Louise Moon (P)
  3. Proposition #1 ==> Yes
  4. Amendment #1 ==> Decline

I am trying to separate the different contest (President and Vice-President, US Senate, etc.) from the "voter's choice" (Alvin Boone..., Louise, etc) line by line. I want to use the first token to create an array list and the second token to be added to the corresponding array list.

I hope I am not over my head with this as I am a new Java programmer but all help is appreciated. So far I have.

public void go () {
  getContest();
}

void getContest() {
  // reading file and call the addContest() method for each line/
  try {
    String line = null;
    while ((line = reader.readLine()) != null) {
      addContest(line);
    }
  }
}

void addContest (String lineToParse) {
  String[] tokens = lineToParse.split(">");

  // somehow this will add contests to
  // its own array 0 is the first part of split
  contestList.add(tokens[0]);
}

I hope I am not asking for too much but if I can find out how to create the array list I am sure I can put second token in the corresponding array. Thank you!

share|improve this question
    
Is this homework? If so, please add the homework tag –  Jim Garrison Feb 12 '12 at 1:42
    
This is not homework –  user1204524 Feb 12 '12 at 3:00
    
I will accept any help...even if its a website that tells me how to use a for loop with an array..this is not for any grade.. –  user1204524 Feb 12 '12 at 4:24
    
Cool; what's it for? You'd really be better off using a list to avoid having to know, or guess, the number of elements. Or a map keyed by the contest, holding lists of each's "entries". The data you show makes it difficult to guess what you actually need; by telling us what the requirements are, and where they came from, you make it easier for us to guess. –  Dave Newton Feb 12 '12 at 4:50
    
I was always told you learn by doing. I write programs I come up with and I have been on the student government board so...Trying to make a tally machine..which is the reason why I chose using an array list. Then the 2nd token would be used to tally up the votes. I just started this code an ran into this problem. What else could I provide to help? –  user1204524 Feb 12 '12 at 5:04

1 Answer 1

It looks like you would probably be better served by a map. The first element is the key, and the second element the value.

ArrayList<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
    lines.add(line);
}
HashMap<String, String> votes = new HashMap<String, String>();
for(String line: lines) {
    String[] terms = line.split(">");
    votes.put(terms[0], terms[1]);
}
share|improve this answer
    
thank you so much! I will try it out tomorrow. –  user1204524 Feb 13 '12 at 5:10
    
Im still working on this and I am learning to read code as well..so can you tell me..regarding the "lines" in the code above..it needs to will be a local String variable? eclipse wants me to add it as an object. –  user1204524 Feb 15 '12 at 3:26
    
Updated to demonstrate one way to do this. You could also just use the while loop directly and eliminate the lines variable. –  Carl Manaster Feb 15 '12 at 14:13
    
the .put is not working is there anything else to use? –  user1204524 Feb 18 '12 at 22:52
    
String No_Selection; String line = null; String[] anArray = line.split("==>"); line.put(anArray[0], anArray[1]); –  user1204524 Feb 18 '12 at 22:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.