Author |
Message |
cool dude
![](http://www.taylorstrategicmarketing.com/images/king.jpg)
|
Posted: Sun Oct 29, 2006 8:00 pm Post subject: classes and clients |
|
|
i am making a class definition where i define all the characteristics of my object in that class and then make a client where i would just set the parameters and get all the information from my class. that said i am having a small problem.
basically i'm randomizing 3 numbers and storing that in an array. then when i go to my method and say the following i'm getting a small problem:
code: |
public int getCombination()
{
int factor =0;
for (int i = 0; i <combination.length; i++)
{
factor = this.combination[i];
}
return factor;
} |
now this executes and compiles the problem though is that in my program the value of the 3 randomized numbers will be the same. the reasoning it lets say the 3 numbers are 1,2,3. the first time through the for loop it will set factor = 1, the second time through the for loop it will set factor = 2 and the third time through the for loop it will set factor = 3. as you can see the the last time it sets factor = 3 it returns the value 3 and all the other numbers are lost. so then i tried making factor as an array to store all 3 numbers but i'm getting an error when i try to do my return statement. any help would be appreciated. thank you |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HellblazerX
![](http://www.plamania.co.kr/shopimages/plmtest/2910040000213.jpg)
|
Posted: Sun Oct 29, 2006 8:08 pm Post subject: (No subject) |
|
|
Maybe you forgot to set the return value as array as well? I don't see why else your program would give an error. |
|
|
|
|
![](images/spacer.gif) |
cool dude
![](http://www.taylorstrategicmarketing.com/images/king.jpg)
|
Posted: Sun Oct 29, 2006 10:53 pm Post subject: (No subject) |
|
|
HellblazerX wrote: Maybe you forgot to set the return value as array as well? I don't see why else your program would give an error.
Not sure what you mean? my program doesn't give me an error. it just gives me the wrong result since its taking the last value of my for loop and returning only that value. i can't put my return statement inside my for loop because obviously that won't work or else it will never technically finish the for loop. i can't say code: | return this.combination[i]; | because it will not recognize the variable i from my for loop which is obvious. i'm out of ideas. |
|
|
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Mon Oct 30, 2006 4:52 pm Post subject: (No subject) |
|
|
It's astonishing how some people doesn't full read the original post and then tries to "help" (not pointing fingers or anything just in general)
return a reference to ArrayList object.
return new ArrayList(combination) |
|
|
|
|
![](images/spacer.gif) |
cool dude
![](http://www.taylorstrategicmarketing.com/images/king.jpg)
|
Posted: Mon Oct 30, 2006 11:08 pm Post subject: (No subject) |
|
|
bugzpodder wrote: It's astonishing how some people doesn't full read the original post and then tries to "help" (not pointing fingers or anything just in general)
return a reference to ArrayList object.
return new ArrayList(combination)
hmm i'm not exactly sure how thats supposed to work. sorry but i hadn't learned what ArrayList means yet so i don't know how to use it. i'll check the java API for now but some clarification will be appreciated. thanks |
|
|
|
|
![](images/spacer.gif) |
bugzpodder
![](http://www.vbforums.com/avatar.php?userid=31755&dateline=1038631511)
|
Posted: Tue Oct 31, 2006 8:09 am Post subject: (No subject) |
|
|
the problem with java is that you can't return mutiple values, without putting them in a single class and return an instance of it. Instead of creating your own class, stick with ArrayList. Or simply generate the random numbers in the same method as you use them, or generate them one at a time. |
|
|
|
|
![](images/spacer.gif) |
cool dude
![](http://www.taylorstrategicmarketing.com/images/king.jpg)
|
Posted: Wed Nov 01, 2006 4:03 pm Post subject: (No subject) |
|
|
k i seemed to fix this problem by doing the following:
code: |
public int[] getCombination()
{
return combination;
} |
now the problem i'm getting is that when i'm comparing if the number is equal to combination i get an error stating that the operator cannot be applied to int, int[].
Here is my comparing statement
code: |
if (combo2[i] == combination2.getCombination()) |
so my question is how would i compare them? |
|
|
|
|
![](images/spacer.gif) |
profchen
|
Posted: Thu Nov 02, 2006 2:03 pm Post subject: you are comparing an int to an array of ints |
|
|
on the left you have an int -- on the right you have a method that returns an int[] (an array of ints)
you have to say which element of the array you are comparing -- e.g. combination2.getCombination()[1] |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
cool dude
![](http://www.taylorstrategicmarketing.com/images/king.jpg)
|
Posted: Thu Nov 02, 2006 3:21 pm Post subject: Re: you are comparing an int to an array of ints |
|
|
profchen wrote: on the left you have an int -- on the right you have a method that returns an int[] (an array of ints)
you have to say which element of the array you are comparing -- e.g. combination2.getCombination()[1]
Wow thanks! i actually was doing the same thing except putting the element after combination2 like so:
code: |
combination2[i].getCombination()[ |
p.s. this is offtopic but i noticed you go to york university. are you in computer science/engineering? if so can i ask you questions about it because i'm thinking of going to york since it is close to my house. |
|
|
|
|
![](images/spacer.gif) |
|