You need to pass it as a parameter or create a “global variable”
IE you could do either of the following…
public void methodOne(String string){
System.out.println(string);
}
public void methodTwo(){
String string = "This string will be printed by methodOne";
methodOne(string);
}
OR (a better solution)
Create a global variable under the class declaration…
public class ThisIsAClass {
String string; //accessible globally
....
//way down the class
public void methodOne(){
System.out.println(string);
}
public void methodTwo(){
String string = "This string will be printed by methodOne"; //edit the string here
methodOne();
}
Let me know if you have any questions. Of course you will have to change String string;
accordingly but it is the same concept with any variable.
You said that your “sentence” is created in one of these methods and it hasn’t been created when you declare it globally. All you need to do is create it globally, String weatherLocation = null;
and then set it whenever you need to. I think it your example it would be under weatherInfo()
public void WeatherInfo(){
weatherLocation = weatherLoc[1].toString();
}
Instead of creating a new one we just edit the one we created globally.
-Henry
0
solved How to access a string in a void method from another void method? [closed]