1

Hi I have to write a program that takes in number of candidate and how many votes they got from the best to the worst. And I wrote two different classes to do that using a fileInputStream and another that uses the scanner class and storing it in the arraylist but the way the teacher has it in the text file is that some of the ballots are on different line so they will go like on one line while the ballots are on the other line. so its like this:

//this is how the text appears in the text file and I was wondering if I could get all 
//the "votes" to look like the first one. 
<v> 5 4 3 2 1  
<v> 1 2 3 4 5 
<v>
1
2
3
5
<v>
5
2
4
1
3
Waldheinz
  • 10,399
  • 3
  • 31
  • 61
chuck finley
  • 459
  • 3
  • 8
  • 16

2 Answers2

1

I think your teacher has done it deliberately. The trick here is to realize that the votes are not delimited by a new line, instead they are delimited by a "V". You can use this information along with regex(Pattern) to derive a solution.

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • The problem is that there are different number of candidate in each test file and it has to go through all of them and check if it has equal number of candidate to votes ratio and if I could remove the text on different lines I could just match the char at each array to the number of candidates but i can't have spaces in it. is there any way i can remove them ? – chuck finley May 30 '11 at 08:05
  • chandran We haven't been introduced to regex pattern is there another way ? – chuck finley May 30 '11 at 08:13
  • @sibghatuk, thats why i asked to use regex pattern. It will solve this. – Suraj Chandran May 30 '11 at 08:14
  • @sibhatuk, if not pattern then u will have to write your own parsing, which i think maybe a little bounce fo ryou at this stage:) – Suraj Chandran May 30 '11 at 08:14
  • Can you show me how to get started setting up the regex(pattern) please. – chuck finley May 30 '11 at 08:26
0

If you don't want to use a regex, you could use Scanner.

if (scanner.hasNext("<v>")
  scanner.next();//ignore the "<v>"
if (scanner.hasNextInt())
  int vote1 = scanner.nextInt();//first vote
if (scanner.hasNextInt())
  int vote2 = scanner.nextInt();//second vote

This would read for example:

<v> 1
4

I don't want to spoil the 'fun' of making your homework, so that's all. If you have more questions, just ask.

Ishtar
  • 11,542
  • 1
  • 25
  • 31