Results 1 to 7 of 7
  1. #1
    RSYR is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Question Whats with my code??

    This is the code i have been given... thinking i shouldn't have much more to do to it...
    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class generator {
    	
    	private static class Match {
    
    		public static final int HomeScore = 0;
    		public static final int AwayScore = 0;
    		public static final String HomeTeam = null;
    		public static final String AwayTeam = null;
    		public void setAwayScore(int parseInt) {
    			// TODO Auto-generated method stub
    		}
    		public void setHomeScore(int parseInt) {
    			// TODO Auto-generated method stub
    			
    		}
    		public void setAwayTeam(String trim) {
    			// TODO Auto-generated method stub
    			
    		}
    		public void setHomeTeam(String trim) {
    			// TODO Auto-generated method stub
    			
    		}
    
    	}
    
    	public static void main(String[] args) throws IOException{
    	
    	final List<Match> matches = new ArrayList<Match>();
    		
    		BufferedReader br = new BufferedReader(new BufferedReader(new FileReader("results2.txt")));
    		int CountValid = 0;
    		int CountInvalid = 0;
    		int CountGoals = 0;
    		
    		if (br != null){
    			String text;
    			
    			while((text = br.readLine()) != null ){
    				final String[] split = text.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]));
    					match.setAwayScore(Integer.parseInt(split[3]));
    					matches.add(match);
    					CountValid++;
    					CountGoals+= Match.HomeScore + Match.AwayScore;
    					
    				}
    				if (split.length <= 3) {
    					CountInvalid++;
    				}
    				br.close();
    			}
    			if (!matches.isEmpty()) {
    				for (final Match match : matches){
    					
    					if(Match.HomeTeam.length()==0){
    						CountInvalid++; 
    						CountValid--;
    						CountGoals = CountGoals -(Match.HomeScore + Match.AwayScore);
    					}
    						
    						else if(Match.AwayTeam.length()==0){
    							CountInvalid++; 
    							CountValid--;
    							CountGoals = CountGoals -(Match.HomeScore + Match.AwayScore);
    						}
    							else {
    								System.out.println(match.toString());
    							}
    					}
    				System.out.println("The total number of goals was" +CountGoals);
    				System.out.println("The total number of valid games were" +CountValid);
    				System.out.println("The total number of invalid games were" +CountInvalid);
    				}
    			}
    		}
    	}
    It's supposed to read in my set of results sort them into order. And validate them... these are the errors i get:

    Exception in thread "main" java.lang.NumberFormatException: For input string: " 1 "
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at generator.main(generator.java:51)

    Thanks For the help!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The error tells you what is wrong: you're trying to parse " 1" as a number. This would work except for the space before the one. Knowing this, you should be able to figure out a fix, I believe. Much luck.

  3. #3
    RSYR is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Thanks for the quick reply i dealt with that by trimming the whitespace from the HomeScore & AwayScore... but i'm getting problems with the reader now?
    Exception in thread "main" java.io.IOException: Stream closed
    at java.io.BufferedReader.ensureOpen(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at generator.main(generator.java:45)
    If anyone can help... i'm new to Java thats why i'm not sure about what its throwing...
    Thanks

  4. #4
    RSYR is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Sorry to keep posting after myself... i solved that problem by removing the following:
    BufferedReader.close();
    and now im getting this:
    Exception in thread "main" java.lang.NullPointerException
    at generator.main(generator.java:66)
    which is pointing at:

    if(Match.HomeTeam.length()==0){
    CountInvalid++;
    CountValid--;
    CountGoals = CountGoals -(Match.HomeScore + Match.AwayScore);
    }

    thanks

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The NPE tells you that you're trying to use a null object. Which object? the one on the line.

    You call this method:
    Java Code:
    match.setHomeScore(Integer.parseInt(split[2]));
    But it does nothing since the method still hasn't been fleshed out:
    Java Code:
    		public void setHomeTeam(String trim) {
    			// TODO Auto-generated method stub
    			
    		}
    so home team defaults to whatever you set as its default:
    Java Code:
    public static final String HomeTeam = null;

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by RSYR View Post
    This is the code i have been given... thinking i shouldn't have much more to do to it...
    Number one on the to-do list: Flesh out all the method stubs.

    Much luck!

  7. #7
    RSYR is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

Similar Threads

  1. IT Jobs: Whats it like?
    By MuslimCoder in forum Jobs Wanted
    Replies: 2
    Last Post: 09-19-2009, 06:46 AM
  2. Whats wrong with this code?
    By bbtgirl in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 03:51 AM
  3. whats my next step
    By thirumurugan.sethu in forum New To Java
    Replies: 4
    Last Post: 10-02-2008, 09:03 PM
  4. whats the code that we need to connect oracle10g with the netbeans
    By katko0ota in forum Enterprise JavaBeans (EJB)
    Replies: 6
    Last Post: 07-15-2008, 05:52 PM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 12:54 PM

Posting Permissions

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