-2

Having this:

static class Girl{
    public String age;
    public String name;
    public String id;
}

static Girl[] girl;

Then i am unable to do this in my main function:

ResultSet r = s.executeQuery("select count(*) from children");   
r.next();
girl_count=r.getInt(1);
girl = new Girl[girl_count];

r = s.executeQuery("select * from children");   
int i = 0;
while(r.next()){
    i = r.getString("id");
    if(girl[i]==null)girl[i]=new Girl();
    girl[i].age=r.getString("age");
    girl[i].name=r.getString("name");
    girl[i].id=r.getString("id");
}

The above code is not working, what i would like to acheive is:

System.out.println(girl[3]);

especially this line:

 girl = new Girl[girl_count];

Can anyone help me correct this code ? - or find out what i'm missing here?

Daniel S
  • 621
  • 3
  • 8
  • 18
  • 8
    What do you expect to happen and what happens instead? – vanza Sep 17 '13 at 19:38
  • Assuming the ids are from 0 to `girl_count-1`, this code is just fine (ignoring the typo's and obvious compile errors). However, when you print a girl object the way you did you will get the default `toString` implementation (which most likely looks to random output to you). Have a look [here](http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java). – Vincent van der Weele Sep 17 '13 at 19:41
  • 3
    You shouldn't use the exact same name for a class and a variable. (If you used proper naming conventions this would not be possible.) – Hot Licks Sep 17 '13 at 19:41
  • 1
    This line ain't fine... `int i = r.getString("id");` – ppeterka Sep 17 '13 at 19:42
  • please read my commed from rolfl's answer – Daniel S Sep 17 '13 at 19:45

2 Answers2

5

Java best practices say that your class names should have a capital letter to start with, and your variable/field names should be lower-case. I believe, in this case, that your code is failing to compile because you have a Class named 'girl' and a varaiable of the same name too.

Change your class to have a capital letter:

static class Girl{ ...}

and go from there. The error messages will be a lot simpler to understand too.

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • what i was planning to do was to create a lot of class girl objects and to call them by girl[id].name or girl[id].age having a predefined list of ids but it seems i cannot do it without `girl = new girl[girl_count];` which limits me because the ids are not sorted, it can be 54,2,13,1,99 etc .. – Daniel S Sep 17 '13 at 19:44
  • @punked start by making the suggested changes, then comment if that doesn't solve your problem – StormeHawke Sep 17 '13 at 19:45
  • ok so i've made my code changes, and indeed the capital letter helped, but now i get `java.lang.ArrayIndexOutOfBoundsException` on `if(girl[i]==null)girl[i]=new Girl();` – Daniel S Sep 17 '13 at 19:52
  • There are some compile errors in your code still, the line that @ppeterka mentions above is broken, this will never compile: `int i = r.getString("id")`. Fix your code, and then also believe that the ID value in the database for each girl has to be less than girls.length., – rolfl Sep 17 '13 at 19:59
0

From the comment: you need a Map for what you want to achieve:

Map<Integer, Girl> girls = new HashMap<Integer, Girl>();

while(r.next()){
    int i = r.getInt("id"); // note the getInt()
    Girl g = girls.get(i); //attempt to get girl from map by ID, returns null if not found

    if(g==null) { // check if null
        g=new Girl(); // create
        girls.put(i,g); //put into map
    }
    g.age=r.getString("age");
    g.name=r.getString("name");
    g.id=r.getString("id");
}

And when you want to access the elements of the map:

Girl found = girls.get(id); //id is an Integer

To iterate over the Girl instances: Wow, did I just wrote what I wrote??? I have a wife!

for(Girl girl: girls.values()) {
   System.out.println(girl.name); //here you can do anything to the Girl, what morality permits...

}

Also, you can iterate over the keys by using the .keySet() method.

Or if you want the IDs too:

for(Map.Entry<Integer,Girl> entry: girls.entrySet()) {

   Integer id = entry.getKey();
   Girl girl = entry.getValue();
   System.out.println(girl.name); //here you can do anything to the Girl, what morality permits...

}

However the behaviour is that when there are more girls with the same id in the resultset (should not be possible anyway) it overwrites the attributes of said Girl, making it not deterministic...

ppeterka
  • 20,583
  • 6
  • 63
  • 78