Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java StreamTokenizer help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java StreamTokenizer help

    Hello im trying to get my head around a program and not getting anywhere. I need to access a external txt file that contains football score in the format below.

    leeds united : liverpool : 3 : 1
    manchester united : arsenal : 1 : 5


    I then need to display the values in the correct order like below;

    leeds united 3 Liverpool 1
    manchester united 1 arsenal 5

    Im trying to get it so i have each value seperate and i know if its a string or a int. can anyone help my code is below.
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.StreamTokenizer;
     
     
    public class temp {
     
      public static void main(String[] a) throws IOException {
          sumfile("C:\\Documents and Settings\\Nicky\\My Documents\\write.txt");
      }
     
      static void sumfile(String filename) throws IOException {
        Reader r = new BufferedReader(new FileReader(filename));
        StreamTokenizer stok = new StreamTokenizer(r);
        stok.parseNumbers();
        double sum = 0;
        stok.nextToken();
        while (stok.ttype != StreamTokenizer.TT_EOF) {
          if (stok.ttype == StreamTokenizer.TT_NUMBER)
            sum += stok.nval;
          else
            System.out.println(stok.sval);
          stok.nextToken();
        }
        System.out.println("The number of goals is " + sum);
      }
     
    }
    Last edited by helloworld922; October 20th, 2009 at 06:58 PM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Java StreamTokenizer help

    I figured you might want to use the bufferedreader as it is and actually read each line and parse it as you go along.

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;
     
    public class FilesTest1 {
     
        private static class Match {
     
            private String homeTeam;
     
            private int homeScore;
     
            private String awayTeam;
     
            private int awayScore;
     
            public String getHomeTeam() {
                return homeTeam;
            }
     
            public void setHomeTeam(String homeTeam) {
                this.homeTeam = homeTeam;
            }
     
            public int getHomeScore() {
                return homeScore;
            }
     
            public void setHomeScore(int homeScore) {
                this.homeScore = homeScore;
            }
     
            public String getAwayTeam() {
                return awayTeam;
            }
     
            public void setAwayTeam(String awayTeam) {
                this.awayTeam = awayTeam;
            }
     
            public int getAwayScore() {
                return awayScore;
            }
     
            public void setAwayScore(int awayScore) {
                this.awayScore = awayScore;
            }
     
            @Override
            public String toString() {
                final StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(this.homeTeam);
                stringBuilder.append(" ");
                stringBuilder.append(this.homeScore);
                stringBuilder.append(" ");
                stringBuilder.append(this.awayTeam);
                stringBuilder.append(" ");
                stringBuilder.append(this.awayScore);
     
                return stringBuilder.toString();
            }
        }
     
        public static void main(String[] args) throws Exception {
            final List<Match> matches = new ArrayList<Match>();
     
            final BufferedReader bufferedReader = new BufferedReader(new FileReader("My/Path/To/Scores/File"));
     
            if (bufferedReader != null) {
                String line;
     
                while ((line = bufferedReader.readLine()) != null) {
                    final String[] split = line.split(":");
     
                    if (split.length >= 4) {
                        final Match match = new Match();
                        match.setHomeTeam(split[0].trim());
                        match.setAwayTeam(split[1].trim());
                        match.setHomeScore(Integer.parseInt(split[2].trim()));
                        match.setAwayScore(Integer.parseInt(split[3].trim()));
                        matches.add(match);
                    }
                }
     
                bufferedReader.close();
            }
     
            if (!matches.isEmpty()) {
                for (final Match match : matches) {
                    System.out.println(match.toString());
                }
            }
        }
    }

    Enjoy!

    // Json

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java StreamTokenizer help

    Thanks for the help mate but it seems to be erroring on the part below due to the <> do you know why this would be. I thinks its down to java not supporting the symbols.

    public static void main(String[] args) throws Exception {
    final List<Match> matches = new ArrayList<Match>();


    Thanks

    Nick

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java StreamTokenizer help

    Java does support those symbols, they're called generics. (since SE 4 or 5)

    I'm not sure what kind of error you're getting, it compiles perfectly fine for me.

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java StreamTokenizer help

    Got it working thanks a lot much appreciated.