jamess
jamess

Reputation: 158

String Parsing in Java - how can I do this?

I have the following data:

Maths abc10 4 def08 6 ;  
English hrd45 3 ngh05 10 ; .  

The word at the beginning is a keyword which is also in an enum, the following data are username and login pairs (there can be an unlimited number of these. The data for each keyword is terminated by ';' and the data is terminated by '.'

I'm using the scanner class but I can't get it to loop so that it can produce the following:

Maths abc10 4   
Maths def08 6   
English hrd45 3  
English ngh05 10

Any Ideas? Thanks!

Upvotes: 1

Views: 186

Answers (2)

Dr G
Dr G

Reputation: 4017

You could have a look at the regular expressions java.util.regex, if it's homework you'll learn about those as well which, trust me, it doesn't hurt. For things this simple, it's not worth going the parser way IMHO.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

Store it all in a Map<String, Map<String, Integer> >. Iterate over the lines, grabbing the subject and then the name. (Loop starts here). Check if the name is ; and exit if it is. Grab the score, then add it to a Map<String, Integer>. Then grab the next name. (Loop ends here). Add the current Map<String, Integer> to the big Map<>, then do it all over again.

Upvotes: 3

Related Questions