![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 21
Rep Power: 0
![]() |
Getting variables created in one method to work in others?
This might be a silly question (in fact it probably is), but lets say I have the code:
abc()
{
int x
x = x++;
}
def()
{
int y = x;
}IT may seam silly in the above code, but I created an array within one method, and I need to send it to another method, so I need to know how its done. Thanks anyone who might help ![]() |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 136
Rep Power: 0
![]() |
First of all, there is not array in the above code.
[PHP] public class Test { public void someMethod() { int array[] = new int[10]; passArray(array); } public void passArray(int array2[]) { //do whatever to the array } } [/PHP] You pass an array through a method via arguments. What ever you do to that array2, array 's value takes that same effect as if it was the array itself. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 21
Rep Power: 0
![]() |
yeah, I know there wasn't an array, I was too lazy to copy and paste the code, so I just showed an example
![]() so, I think I understand, basically you have, passArray(array); , and this can also be applied with other variables too right? So I can, for arguments sake, int x = 5, then put down passArray(x);, and in the next method (or any other within the same class provided the method's name is the same as the pass I give), I can say passArray(int y); and whatever y is x is as well? I have one more little problem. I'm working on a little project that swaps letters with '1337' letters, that is for instance, A becomes 4 or M becomes /\/\, I am having slight difficulty with some code. What I'm trying to do is step through a string and using charAt(x) to see if they are equivalent or not. However I get a compiler error that char and String are incompatible types. So is there another way I can find individual letters within a string? |
|
|
|
|
|
#4 | |||
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 136
Rep Power: 0
![]() |
Quote:
Quote:
Quote:
[PHP]char characters[] = someString.toCharArray();[/PHP] |
|||
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 21
Rep Power: 0
![]() |
public class Test
{
public void someMethod()
{
int x = 32;
passArray(array);
}
public void passArray(int y)
{
}
}and thank you for the char characters[] = someString.toCharArray(); idea, I'll give it a shot ![]() |
|
|
|
|
|
#6 | |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
Quote:
String someSentence = "this is a sentence"; String letter = someSentence.subString(0,1); So at this point letter equals "t", there are however much better ways of doing this. But it would be easy enough to create an array of type string. use a while loop, and assign each letter of your sentence to the string array. |
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Mar 2006
Posts: 21
Rep Power: 0
![]() |
thanks NSchnarr, however I done it another way, rather crude compared to your plan...
what I did was use ASCII values to create an array using a while loop to store all the alphabet, and I proceeded to handtype the '1337' letters, I finished it fine and it works right now. However, I seem to have a bit of a wierd problem now, my code is working almost 100% (I get array out of bounds exception when I have spaces), however I don't know how it could be working... What I have is an if condition, that if array1 == array2 it does something. However array1 is a variable of type int and array2 is a variable of type char, however even without typecasting it (that is without saying (char)array1 == array2), the damn thing works... So java does automatic typecasting when it can? Or am I missing something due to lack of sleep... Also, whenever there is a space in the sentance the thing explodes on me with an array index out of bounds exception, I tried to rectify this by making both the 1337 array and the alphabet array one higher adding <space> for them both so they can simply space themselves out when needed. However I still get the same error... I'm working on debugging it now, I think I can solve it, but then again, I would really appreciate it if someone helped out with some advice ![]() |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 136
Rep Power: 0
![]() |
I would read about Recursion in Java. x was declared within a method, it 's value stays within that method unless passed through another method
[PHP] public void methodOne() { int x = someValue; } public int methodTwo() { int y = x; //syntax error, there is not x variable } [/PHP] There was no x within methodTwo so, x doesn't exist. Pass x thru the methodTwo. [PHP] public void methodOne() { int x = someValue; methodTwo(x) } public int methodTwo(int num) { int y = num; //now y received x's value } [/PHP] |
|
|
|
|
|
#9 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
Yes, java does automatic type casting in some cases.
Your index out of bounds problem is happening because when its a space you do nothing to it im guessing. Counter doesint increase.. hard to say cuz i dont see your code. BUt whatever the case, if there is a space it doesnt accounts for it and then goes past your sentence array length. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Mar 2006
Posts: 21
Rep Power: 0
![]() |
yeah, I just added an extra index to both arrays so it accounts for spaces.
What it was doing before when it reached spaces was incrementing them until it broke the index bounds since there was no condition that would work within the loop to stop it from doing that... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|