Question

I will try to explain this as much as I can. I am reading scores from a file onto which my form appends lines. The line consists of a date, home team, score, away team, score. The stats I gather is away wins, home wins and draws. The following code works perfectly

JButton viewStatsButton = new JButton(new AbstractAction("VIEW STATS")
    {
        public void actionPerformed( ActionEvent e ) 
        {
            int homeScore = 0;
            int awayScore = 0;
            int homeWins = 0;
            int awayWins = 0;
            int scoreDraw = 0;
            String  line = null;
            String  output;
            String matchDay;
            @SuppressWarnings("unused")
            String  homeTeam;
            @SuppressWarnings("unused")
            String  awayTeam;
            String  file = "scores.dat";
            StringTokenizer tokenizer;

            FileReader fileReader = null;
            try 
            {
                fileReader = new FileReader (file);
            } 
            catch (FileNotFoundException e1) 
            {
                e1.printStackTrace();
            }
            BufferedReader inFile = new BufferedReader (fileReader);
            try 
            {
                line = inFile.readLine();
            } 
            catch (IOException e1) 
            {
                e1.printStackTrace();
            }
            while(line != null)
            {
                tokenizer = new StringTokenizer(line);
                matchDay = tokenizer.nextToken();
                homeTeam = tokenizer.nextToken();
                try
                {
                homeScore = Integer.parseInt(tokenizer.nextToken());
                }
                catch (NumberFormatException exception)
                {
                    System.out.println("Error in input. Line ignored:");
                    System.out.println(line);
                }
                awayTeam = tokenizer.nextToken();
                try
                {
                    awayScore = Integer.parseInt(tokenizer.nextToken());
                }
                catch (NumberFormatException exception)
                {
                    System.out.println("Error in input. Line ignored:");
                    System.out.println(line);
                }

                if(homeScore > awayScore)
                {
                    homeWins++;
                }
                else if(awayScore > homeScore)
                {
                    awayWins++;
                }
                else
                {
                    scoreDraw++;
                }

                try 
                {
                    line = inFile.readLine();
                } 
                catch (IOException e1) 
                {
                    e1.printStackTrace();
                }
            }
            try 
            {
                inFile.close();
            } 
            catch (IOException e1) 
            {
                e1.printStackTrace();
            }
            output = "Home Wins : "+homeWins+"\nAway Wins : "+awayWins+"\nDraws : "+scoreDraw;
            JTextArea textArea = new JTextArea();
            frame.getContentPane().add(textArea, BorderLayout.CENTER);
            textArea.setText(output);
        }
    });
    scorePanel.add(viewStatsButton);    
}

The problem does not come to light until the name of team is made out of two strings i.e.Newcastle United. What I had to do was append the two strings together like NewcastleUnited. I have tried to find out the length of the token and if it's less than 3 then i take it and parse it as integer but it seems that even if the next token reference is in an if statement it still moves to the token after it.

I would appreciate any help and guidance.

Was it helpful?

Solution

Try following

  • Before calling tokenizer.nextToken() check tokenizer.hasMoreTokens() to ensure that there is a token to read
if(tokenizer.hasMoreTokens())
{
    x = tokenizer.nextToken();
}
  • After reading team name(first part) check whether next part is integer if it is, treat it as score, otherwise append it to team name.
homeTeam = tokenizer.nextToken();
String temp = tokenizer.nextToken();
try
{
    homeScore = Integer.parseInt(temp);
}
catch(Exception e)
{
    //Comes here if temp is not an integer, so temp is second part of name
    homeTeam = homeTeam + " "+temp; 
    homeScore = Integer.parseInt(tokenizer.nextToken());
}
//Whatever the case, if we come here, it means both hometeam and score are assigned.
...........      
...........
...........
  • Don't forgot to check tokenizer.hasMoreTokens() if you are not sure whether there is a token.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top