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 3 of 3

Thread: Football Results

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Football Results

    Right, i'm doing a piece of work for uni and basically i need it to read in a set of results that are contained in a .txt file and split them at the delimiter (which i have managed to do). The problem is i need to take these results and organise them into a table which looks something like this :

    <home_team_name> [<home_team_score>] | <away_team_name> [<away_team_score>]

    Then i need to make sure that all of the scores read in were valid i.e. both names and both scores were there. If a team name or score was missing then it should not be output to the screen.

    Finally at the end i need to output something that will say... "The valid match count was # . Total goals scored were #. Invalid match count was # .

    I'm very new to java this is one of my first pieces of work. Would i need to split each of the names and scores into a seperate class? I'll post what code i have so far. Thanks!!

     
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
     
    public class linebased {
        public static void main(String[] args) throws IOException {
            BufferedReader inputStream = null;
     
            try {
                inputStream = 
                    new BufferedReader(new FileReader("results2.txt"));
     
                String text;
                while ((text = inputStream.readLine()) != null) {
     
                	String [] splitupText = text.split(":");			// split the text into multiple elements
     
        			for ( int i = 0; i < splitupText.length; i++ ) {	// loop over each element
     
        				String nextBit = splitupText[i];				// get the next element (indexed by i)
     
        				nextBit = nextBit.trim();						// use the trim method to remove leading and trailing spaces
     
        				System.out.println(nextBit);					// display the next element	
     
        			}
     
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
            }
        }
    }


  2. #2
    Junior Member
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Football Results

    Right i've been working on the basics of this today and i've totally changed the code to this:
    Keep getting the following:
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at examplereader.main(examplereader.java:32)
    I'm not sure what to do with this exactly its all a mess... i guess in need to parse the integer to a string?
    any help whatsoever would be good thanks.

    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Locale;
    import java.util.Scanner;
     
    public class examplereader {
     
    	/**
    	 * @param args
    	 * @throws FileNotFoundException 
    	 * @throws FileNotFoundException 
    	 * @throws FileNotFoundException when the file cannot be loaded 
    	 */
     
    public static void main(String[] args) throws FileNotFoundException{
     
    		String hteam;
    		String ateam;
    		int hscore;
    		int ascore;
     
    		Scanner s = new Scanner(new BufferedReader(new FileReader("results2.txt"))).useDelimiter("\\s*:\\s*");
    		// create a scanner which scans from a file and splits at each colon
     
    		while ( s.hasNext() ) {
     
    			hteam = s.nextLine();		// read the home team from the file
    			ateam = s.nextLine();		// read the away team from the file
    			hscore = s.nextInt();		//read the home team score from the file
    			ascore = s.nextInt();		//read the away team score from the file
     
    			System.out.print(hteam);	// output the line of text to the console
    			System.out.print(hscore);
    			System.out.print(ateam);
    			System.out.println(ascore);
    		}
    		System.out.println("\nEOF");	// Output and End Of File message.
     
    			}
     
    }

  3. #3
    Moderators helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Football Results

    Java's telling you that Scanner was looking for an integer, but received something like "Hello" (some text that can't be parsed for the next integer).