Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2006, 6:07 AM   #1
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
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
chillypacman is offline   Reply With Quote
Old May 25th, 2006, 7:13 AM   #2
Toro
Hobbyist Programmer
 
Toro's Avatar
 
Join Date: Apr 2006
Posts: 136
Rep Power: 0 Toro is an unknown quantity at this point
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.
Toro is offline   Reply With Quote
Old May 25th, 2006, 7:23 AM   #3
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
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?
chillypacman is offline   Reply With Quote
Old May 25th, 2006, 7:33 AM   #4
Toro
Hobbyist Programmer
 
Toro's Avatar
 
Join Date: Apr 2006
Posts: 136
Rep Power: 0 Toro is an unknown quantity at this point
Quote:
Originally Posted by chillypacman
so, I think I understand, basically you have, passArray(array); , and this can also be applied with other variables too right?
Yes, this concept can be used with variables also.

Quote:
Originally Posted by chillypacman
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?
Can you explain the a little better?
Quote:
Originally Posted by chillypacman
So is there another way I can find individual letters within a string?
yes, you can make a char array using the method from the String class
[PHP]char characters[] = someString.toCharArray();[/PHP]
Toro is offline   Reply With Quote
Old May 25th, 2006, 8:48 AM   #5
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
 public class Test
{
    public void someMethod()
    {
        int x = 32;
        passArray(array);
    }
    public void passArray(int y)
    {
        
    }
}
for the above code, does int y become int x, and whatever happens to y also happens to x?

and thank you for the char characters[] = someString.toCharArray(); idea, I'll give it a shot
chillypacman is offline   Reply With Quote
Old May 25th, 2006, 9:36 AM   #6
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
Quote:
So is there another way I can find individual letters within a string?
You could always use the substring method.

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.
NSchnarr is offline   Reply With Quote
Old May 25th, 2006, 9:45 AM   #7
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
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
chillypacman is offline   Reply With Quote
Old May 25th, 2006, 9:51 AM   #8
Toro
Hobbyist Programmer
 
Toro's Avatar
 
Join Date: Apr 2006
Posts: 136
Rep Power: 0 Toro is an unknown quantity at this point
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]
Toro is offline   Reply With Quote
Old May 25th, 2006, 9:58 AM   #9
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
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.
NSchnarr is offline   Reply With Quote
Old May 25th, 2006, 10:35 AM   #10
chillypacman
Newbie
 
Join Date: Mar 2006
Posts: 21
Rep Power: 0 chillypacman is on a distinguished road
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...
chillypacman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC