Results 1 to 14 of 14
  1. #1
    kozik is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default Java Array object problem

    Hi,

    i'm going to come right out and say im working on a project for my college course and i need some help with a specific problem i am having. I don't expect anyone to do my work for me but just possibly tell me why i am getting this error.

    so i'm working on a betting program, very simple dulled down one and the class i'm having problems with is a class that reads from a text file that has the names of the teams inside of it and simply puts those names into an array. simple enough but somehow is giving me a problem.

    so here is my code:

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Team {
       
       String[] team = new String[3];
       int a = 0;
       FileReader stats = new FileReader("C:/team.txt");
       Scanner statss = new Scanner(stats);
       
       public Team() throws Exception{
           do{
           team[a] = statss.nextLine();
           System.out.println(team[a]);
           a++;
            }while (statss.hasNext());
        }
    }
    so i have it printing just so i can see what is happening, and i have my main class where i call this team object and just tell it to print. my text file just simply has in this order 4 team names:

    ManUtd
    Liverpool
    Chelsea
    Arsenal

    there are no extra spaces or anything in the text file.

    so when i run my main class which is just:

    Java Code:
    public class Main {
        public static void main(String[] arg) throws Exception {       
            Team test = new Team();
            System.out.println(test);
        }
    }
    this is the output i get:

    Java Code:
    run:
    Man Utd
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    Liverpool
    chelsea
            at betprog.Team.<init>(Team.java:21)
            at betprog.Main.main(Main.java:19)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)
    if i change my array to size 4 this is what i get:

    Java Code:
    run:
    Man Utd
    Liverpool
    chelsea
    arsenal
    betprog.Team@b66cc
    BUILD SUCCESSFUL (total time: 2 seconds)
    i know this has to be a simple logical error i'm making somewhere and i'm guessing it has to do with my loop, i've tried both while and do while and both give the exact same result.

    but yea im kind of stumped and i know i'm a noob but any help would be greatly appreciated.

    Thanks

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    You want:

    Java Code:
    String[] team = new String[3];
    to read:

    Java Code:
    String[] team = new String[4];
    Remember when you initalize your array you are setting the size... In your case you want your array to have 4 spots...

    Now when you search through your array you will start a 0 and end at 3...
    Who Cares... As Long As It Works...

  3. #3
    kozik is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    if i change it to

    Java Code:
    String[] team = new String[4];
    i get

    Java Code:
    run:
    Man Utd
    Liverpool
    chelsea
    arsenal
    [COLOR="Red"]betprog.Team@b66cc[/COLOR]
    BUILD SUCCESSFUL (total time: 2 seconds)
    notice it ends up printing an area where it's stored in memory... it looks like when i change it to 4 its adding 1 extra array index, this index doesnt get filled with anything because i only have 4 strings and thus it prints a memory address.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    no, this happens because you are printing your team object.

  5. #5
    kozik is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    oh i see... so really the way i'm going about creating this object in my Team class is all wrong and i need set and get methods to return the value otherwise im just going to get that betprog.Team@b66cc right?

  6. #6
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Java Code:
    public class Main {
        public static void main(String[] arg) throws Exception {       
            Team test = new Team();
            System.out.println(test);
        }
    }
    Remove System.out.println(test); and you will be ok...
    Who Cares... As Long As It Works...

  7. #7
    kozik is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    but lets say i for whatever reason wanted to print my Team object i would have to have a method that returns the array in my Team class right?

  8. #8
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    You are printing it...

    Java Code:
    public Team() throws Exception{
           do{
           team[a] = statss.nextLine();
           System.out.println(team[a]);
           a++;
            }while (statss.hasNext());
        }

    You already have System.out.println(team[a]);

    But if you want to print it at any time instead of time of creation, then create a getTeam() method and loop through it when you want to...
    Who Cares... As Long As It Works...

  9. #9
    kozik is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    im horrible with set() and get() methods so any chance you could help me with how i would format that?

    im also trying to make get() methods basically for every team i have now so that in another class that i have that holds their stats i can reference the specific team to the specific stats and create a toString method i guess so that i could print the team and the stats together when i call that toString() method in my main class

  10. #10
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Sure... Lets see what you have...
    Who Cares... As Long As It Works...

  11. #11
    kozik is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    well right now im just working still on that Team class trying to get each team into its own method with set and get and yea i'm pretty lost already because i wrote my set and get and i didnt really expect it to work but im really just going off of trial and error to see what would work so i tried for the first team in my text which is manUtd and this is what i wrote (also you'll probably notice i don't exactly know when to use the this. operator :S)

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Team {
       
       String[] team = new String[4];
       String manUtd;
       int a = 0;
       FileReader stats = new FileReader("C:/team.txt");
       Scanner statss = new Scanner(stats);
       
       public Team() throws Exception{
           while (statss.hasNext()){
           team[a] = statss.nextLine();
           //System.out.println(team[a]);
           a++;
            }
       }
    
        public void setManUtd(String manUtd){
            team[0] = statss.nextLine();
              this.manUtd = team[0];
        }
        public String getManUtd(){
            return this.manUtd;
        }
    }
    so when i go to my main and print(test.getmanUtd);

    i just get null which is kind of expected since im pretty sure my set method has no idea what team[0] is. so how would i get it to know what the team array has?

    thanks for all your help btw

  12. #12
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    The reason you get null is because of this:team[0] = statss.nextLine(); in your get method... stratss is already done... It did its thing in your constructor...


    Get and Set methods are generally for setting and getting whole variables... You dont want to get into team[0], team[1], etc... You just want to get into return team;...

    So This:

    Java Code:
        public void setManUtd(String manUtd){
            team[0] = statss.nextLine();
              this.manUtd = team[0];
        }
        public String getManUtd(){
            return this.manUtd;
        }

    Should be:


    Java Code:
        public void setManUtd(String manUtd){
           this.manUtd = manUtd;
        }
        public String getManUtd(){
            return this.manUtd;
        }

    So if you need to setManUtd you can do:

    Java Code:
         setManUtd(team[0]);
    Here are some good links:

    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Who Cares... As Long As It Works...

  13. #13
    kozik is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    thanks so much for all your help

    i have another class for schedule and basically what this is going to do is ill have a text file with the games that these two teams have played against each other, and their scores, it then has to relate the team with the score and then notice who won and who lost and add up all the wins and losses between the two teams that the user selects and then basically just tells the percentage of chance that the teams are going to have of winning...

    now i dont expect you to program this for me but i was just wondering how you would go about doing something like this, like i'm finding it hard to get my head around how i'm going to relate everything to each other... i was thinking about just dumbing that whole thing down and having multiple text files for lets say manUtd vs Chelsea so when user selects those two it reads that text file and all that's in the text file is how many times manUtd has won and lost against them and then it gives the percentage based on that... i know that way would be easier but i don't know if my prof would like it being like that...

    again thanks for all your help

  14. #14
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    This is quite the project... Do you have to write to any of these files...

    Also I would highly suggest starting small and doing one little piece at a time...

    Make Google and Your book your friend...
    Who Cares... As Long As It Works...

Similar Threads

  1. Java array problem
    By cassysumandak in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 03:21 AM
  2. array of Object
    By amit_m04 in forum New To Java
    Replies: 8
    Last Post: 03-09-2009, 03:50 AM
  3. initialising Array of object??
    By Somitesh Chakraborty in forum New To Java
    Replies: 4
    Last Post: 08-27-2008, 09:00 PM
  4. Array of Object
    By haiforhussain in forum New To Java
    Replies: 1
    Last Post: 06-25-2008, 11:24 AM
  5. Object array query
    By bugger in forum New To Java
    Replies: 5
    Last Post: 01-07-2008, 05:58 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
  •