Results 1 to 10 of 10
  1. #1
    kittykat850 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Match Generator Assignment Help / Error Message

    Hiya guys, I've been ill recently and not had time to finish my assignment, my tutor has given me a reaessment and I really can't fail it, so far I've got 30/40 points for the module. The last 10 points are for valiadation which I am stuck on.

    My current assignment is a football match generator, I scan a file and store the data, I then output the data to read football teams scores which is working fine but for the last 10 points I need to be able to validate the data so it read the scores correctly from the fake and real once. I'm new to this forum so not really sure what to do and I'm also new to programming so I'm not that epic at it =/

    Can anyone help me at all? I really can't fail this module!

    Kitty x

    Below is my current code and the error message I have.

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public class requirement1 {
    	public class Teams {
    		
    		String home_team;
    		String away_team;
    		int home_score;
    		int away_score;
    
    		
    		}
    	
    	public static void main(String[] args) throws FileNotFoundException {
    
    // scans the file named in brackets
    		Scanner s = new Scanner(new File("src/results2.txt"));	
    
    		
    		String line;	// stores the each line of text read from the file
    //declaring all values
    		int InvalidCount = 0; // declares all the variables which are set to 0
    		int ValidCount = 0;
    		int ScoreCount = 0;
    
    
    
    		while ( s.hasNext() ) {
    
    			line = s.nextLine();		
    
    
    
    			String [] splitupText = line.split(":");			
    			line = line.trim();
    // trims all the text
    			splitupText[0]=splitupText[0].trim();
    			splitupText[1]=splitupText[0].trim();	
    
    
    			if (splitupText.length != 4)
    			{InvalidCount = InvalidCount+1 ;}
    
    			else if (splitupText[0].isEmpty() ){
    				InvalidCount = InvalidCount+1 ;}
    
    			else if (splitupText[1].isEmpty() ){
    				InvalidCount = InvalidCount+1 ;}
    
    			else {				
    				String hometeamName = splitupText[0].trim();
    				String awayteamName = splitupText[1].trim();
    				String hometeamScore = splitupText[2].trim();
    				String awayteamScore = splitupText[3].trim();
    				try {										// parses all the data from the file
    					int homescore = Integer.parseInt(hometeamScore);	
    					int awayscore = Integer.parseInt(awayteamScore);
    					ScoreCount = ScoreCount + (homescore + awayscore);
    					System.out.println( hometeamName + " [" + hometeamScore + "] " + "| "+ awayteamName + " [" + awayteamScore + "]"); // prints all the data from the .txt file in the given format
    					ValidCount = ValidCount+1 ;
    
    				}
    				catch (NumberFormatException e) {
    
    
    					InvalidCount = InvalidCount+1 ;
    				}	
    			}
    		}
    		System.out.println( ("\n") ); // prints a blank line
    		System.out.println ("Number of Goals: " + ScoreCount);	
    		System.out.println ("Number of invalid entries: " + InvalidCount);	
    		System.out.println ("Number of valid entries: " + ValidCount); 
    	}}
    Error Message
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    	at requirement1.main(requirement1.java:39)
    I've also attached the file that I read the data off.
    Attached Files Attached Files
    Last edited by kittykat850; 04-20-2011 at 01:25 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Trace the thrown exception. It says at line 39 you had an array index out of bounds error. What happens if you find the length of split up text before indexing into it?
    Java Code:
    System.out.println(splitupText.length);
    Also, I am not entirely sure what you are stuck on. Would you mind clarifying? Perhaps posting the exact assignment may be helpful to me and others.

  3. #3
    kittykat850 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Trace the thrown exception. It says at line 39 you had an array index out of bounds error. What happens if you find the length of split up text before indexing into it?
    Java Code:
    System.out.println(splitupText.length);
    Also, I am not entirely sure what you are stuck on. Would you mind clarifying? Perhaps posting the exact assignment may be helpful to me and others.
    I'm not really sure what you mean? I only started this recently so I'm still getting to terms understanding it.

    This is my assignment breif:

    "Requirement 1 : Basic Level (40%-50%) - Threshold pass.

    The Generator program must read in the specified match results file and validate the contents. The results of each valid match should be output to the screen (stdout) in the following format.

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

    At the end of the output statistics should be generated to show the total number of valid and invalid matches that were processed. Also the total number of goals scored should be shown.

    The following is an example of the typical output to be generated.

    Leeds United [1] | Liverpool [10]
    Chelsea [2] | Manchester City [1]
    Manchester United [2] | Luton [0]

    Valid Match count was 3, total goals scored were 16.
    Invalid Match Count was 1"

    Also to mention I used eclipse at school on a windows computer and it was working fine but it seems to diff from when I'm at home working on my mac with a mac version of eclipse.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    First decide on what constitutes a valid line.
    How many colons (:) should it have?
    Then write a method called isValid which takes a line and returns true if it's a valid line.
    In your main code you then only process the valid lines and increment the invalids counter if isValid returned false.

  5. #5
    kittykat850 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    First decide on what constitutes a valid line.
    How many colons (:) should it have?
    Then write a method called isValid which takes a line and returns true if it's a valid line.
    In your main code you then only process the valid lines and increment the invalids counter if isValid returned false.
    "Leeds United : Liverpool : 1: 10
    Chelsea : Manchester City :1
    Hull : :2:3
    : :2:0
    Arsenal Liverpool :2:2"

    This is another of the text files that has the invaild enteries in, how can I read two files btw?

    I understand in theory what your saying but not really sure how to do it in actually code, it's half term atm so my tutor is away and our hand in is tomorrow so im just stressing out atm trying to get this solved

  6. #6
    kittykat850 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Found some notes too that I had

    Need to use ExampleReader, ExampleSplitter, NumberProcessor

    String[]splitText = linesplit (;)
    String hometeam = splitText [0]
    String awayteam = splitText [1]

    Array
    Pass it

    If statement to see if their validated

    If (splitText.length() == 4
    If not 4 then it is not valid

    Hope it helps.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Have you corrected your array out of bounds error? I don't do a lot of string manipulation but I'm pretty sure you're not declaring your array properly with this.

    Java Code:
    String [] splitupText = line.split(":");			
    line = line.trim();
    If I'm correct, that would be why you're getting an index out of bounds error.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    kittykat850 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    Have you corrected your array out of bounds error? I don't do a lot of string manipulation but I'm pretty sure you're not declaring your array properly with this.

    Java Code:
    String [] splitupText = line.split(":");			
    line = line.trim();
    If I'm correct, that would be why you're getting an index out of bounds error.
    How am I suppose to correct in? I'm really new to this and don't fully understand.

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I'm sorry, I was mistaken. I took a look at the API and it does indeed intialize an array.

    I suggest that you print the length of your array to the console before writing splitupText, as sunde suggested. Somewhere your array length is not equaling your indexing.

    Array (Java 2 Platform SE v1.4.2))
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    kittykat850 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    splitupText[0]=splitupText[0].trim();
    splitupText[0]=splitupText[0].trim();
    I switched them to 0 instead of 1 but it doesn't display any data, just tells me 0 goals, I'm really lost at trying to understand any of this :(

Similar Threads

  1. error message
    By tri.yudhanto in forum JDBC
    Replies: 0
    Last Post: 01-13-2011, 01:20 PM
  2. Error Message ..
    By Hamodi18 in forum New To Java
    Replies: 15
    Last Post: 07-11-2010, 03:31 AM
  3. Error Message????
    By Cubba27 in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:46 PM
  4. help with fixing my assignment.please...error
    By Jin-enigma-PL in forum New To Java
    Replies: 4
    Last Post: 02-05-2009, 12:12 AM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •