problem passing string...

Put your problem here if it does not fit any of the other categories.

problem passing string...

Postby exopolitix » Thu Oct 15, 2009 9:18 pm

Hi,

Im trying to get my program to manipulate a string at the moment. Basically I have a String in one class with the value "". Within this class I call a method of another class using this string as a parameter. This method then changes the value of the string and returns it.

However when I then check the value of this string via logcat it remains unchanged. I dont really understand why. As far as I can tell this should be quite simple. I ran it on the debugger but couldnt figure out the problem. This is what the code is laid out like:

class 1(implements class 2):
private String string = "";

changeString(string);
System.out.println(string); // tried using v.log as well but same result


class 2:

public String changeString(String s){
s = "this string has been changed";
return s;
}

I know im probably missing something stupid but if I move the log check to be just before it returns s then it shows the correct value. So i know its something to do with passing it over.

Any help is much appreciated.
exopolitix
Freshman
Freshman
 
Posts: 8
Joined: Tue Sep 01, 2009 12:56 pm

Top

Postby qlimax » Thu Oct 15, 2009 9:42 pm

from your pseudocode :D

class 1(implements class 2):
private String string = "";

String newString=changeString(string);
System.out.println(newString); // tried using v.log as well but same result


class 2:

public String changeString(String s){
s = "this string has been changed";
return s;
}



I hope that's what you mean...
let me know... cya
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby exopolitix » Thu Oct 15, 2009 9:51 pm

qlimax wrote:from your pseudocode :D

class 1(implements class 2):
private String string = "";

String newString=changeString(string);
System.out.println(newString); // tried using v.log as well but same result


class 2:

public String changeString(String s){
s = "this string has been changed";
return s;
}



I hope that's what you mean...
let me know... cya



Hey man. Cheers for the really quick response. Yeah turns out all I was missing was the String newString = before the method. Cant believe I didnt realise that was the problem lol. Feel a bit silly now. :oops:

You've just saved me a good few hours tearing my hair out at the problem :D Legend
exopolitix
Freshman
Freshman
 
Posts: 8
Joined: Tue Sep 01, 2009 12:56 pm

Postby qlimax » Thu Oct 15, 2009 10:12 pm

exopolitix wrote:Cant believe I didnt realise that was the problem lol. Feel a bit silly now. :oops:
You've just saved me a good few hours tearing my hair out at the problem :D Legend



Do not worry...
I'm glad I helped you

cya! :run:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests