helpful1
helpful1

Reputation: 1

Processing a string of information

I have a text file that holds baseball teams as YEAR:TEAM1:POINTS1:TEAM2:POINTS2 on each line.

How can I process it so that I wind up with the year, 1st team's name, and if they won or not?

I know I should use delimiter \n and : to separate the data, but how can I actually keep track of the info that I need?

Upvotes: 0

Views: 75

Answers (6)

Giannis
Giannis

Reputation: 5526

Here is an example found at java-examples.com

String str = "one-two-three";
  String[] temp;

  /* delimiter */
  String delimiter = "-";
  /* given string will be split by the argument delimiter provided. */
  temp = str.split(delimiter);
  /* print substrings */
  for(int i =0; i < temp.length ; i++)
    System.out.println(temp[i]);//prints one two three on different lines

Now for reading the input you can use BufferedReader and FileReader check examples for that on google.

Upvotes: 0

Fseee
Fseee

Reputation: 2631

try something like this

public static void readTeams() throws IOException{
    try {
        fstream = new FileInputStream("yourPath");
        in = new DataInputStream(fstream);
        br = new BufferedReader(new InputStreamReader(in));
        String s = br.readLine();
        String[] tokens = s.split(":");
        while(s!=null){               
        for (String t : tokens){
            System.out.println(t);
            }
        }
        in.close();
    } catch (FileNotFoundException ex) {    
        Logger.getLogger(YourClass.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Upvotes: 0

Joseph Lust
Joseph Lust

Reputation: 19985

How about a healthy serving of Regex?

Upvotes: 0

Barry
Barry

Reputation: 2063

Have a look at the String class's split method.

Upvotes: 2

anon
anon

Reputation:

To split the text you can use the methods String#indexOf(), String#lastIndexOf() and String#subString.

Then to compare which team has one, I would convert the String into an int and then compare the two values.

Upvotes: 0

Daniel
Daniel

Reputation: 28074

Since this is homework, here is not the solution, but just some hints:

  • Have a look at the class StringTokenizer to split the line.
  • Have a look at InputStreamReader and FileInputStream to read the file.

Upvotes: 3

Related Questions