Java 从另一种方法获取字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16496443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get String From Another Method?
提问by sparklyllama
I have two methods, the first one creates a string, then I want to use that string in the second method.
我有两种方法,第一种方法创建一个字符串,然后我想在第二种方法中使用该字符串。
When I researched this, I came across the option of creating the string outside of the methods, however, this will not work in my case as the first method changes the string in a couple of ways and I need the final product in the second method.
当我对此进行研究时,我遇到了在方法之外创建字符串的选项,但是,这在我的情况下不起作用,因为第一种方法以多种方式更改字符串,而我需要第二种方法中的最终产品.
Code:
代码:
import java.util.Random;
import java.util.Scanner;
public class yaya {
public static void main(String[] args) {
System.out.println("Enter a word:");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
Random ran = new Random();
int ranNum = ran.nextInt(10);
input = input + ranNum;
}
public void change(String[] args) {
//more string things here
}
}
采纳答案by Bohemian
Create an instance variable:
创建一个实例变量:
public class MyClass {
private String str;
public void method1() {
// change str by assigning a new value to it
}
public void method2() {
// the changed value of str is available here
}
}
回答by Juned Ahsan
Pass the modified string in the second method as an argument.
在第二个方法中将修改后的字符串作为参数传递。
回答by Qazi
create a static variable used the same variable in both the method.
创建一个静态变量在这两种方法中都使用了相同的变量。
回答by Ant P
You need to returnthe modified string from the first method and pass it into the second. Suppose the first method replaces all instances or 'r' with 't' in the string (for example):
您需要从第一个方法返回修改后的字符串并将其传递给第二个方法。假设第一个方法将字符串中的所有实例或 'r' 替换为 't'(例如):
public class Program
{
public static String FirstMethod(String input)
{
String newString = input.replace('r', 't');
return newString;
}
public static String SecondMethod(String input)
{
// Do something
}
public static void main(String args[])
{
String test = "Replace some characters!";
test = FirstMethod(test);
test = SecondMethod(test);
}
}
Here, we pass the string into the first method, which gives us back (returns) the modified string. We update the value of the initial string with this new value and then pass that into the second method.
在这里,我们将字符串传递给第一个方法,该方法返回(返回)修改后的字符串。我们用这个新值更新初始字符串的值,然后将它传递给第二个方法。
If the string is strongly tied to the object in question and needs to be passed around and updated a lot within the context of a given object, it makes more sense to make it an instance variable as Bohemian describes.
如果字符串与所讨论的对象紧密相关,并且需要在给定对象的上下文中传递和更新很多,那么将其作为 Bohemian 描述的实例变量更有意义。
回答by luksch
public class MyClass {
public string method1(String inputStr) {
inputStr += " AND I am sooo cool";
return inputStr;
}
public void method2(String inputStr) {
System.out.println(inputStr);
}
public static void main(String[] args){
String firstStr = "I love return";
String manipulatedStr = method1(firstStr);
method2(manipulatedStr);
}
}
回答by Haque1
Since you mentioned that both methods should be able to be called independently, you should try something like this:
既然你提到这两种方法都应该能够独立调用,你应该尝试这样的事情:
public class Strings {
public static String firstMethod() {
String myString = ""; //Manipulate the string however you want
return myString;
}
public static String secondMethod() {
String myStringWhichImGettingFromMyFirstMethod = firstMethod();
//Run whatever operations you want here and afterwards...
return myStringWhichImGettingFromMyFirstMethod;
}
}
Because both of these methods are static, you can call them in main() by their names without creating an object. Btw, can you be more specific about what you're trying to do?
由于这两个方法都是静态的,因此您可以在 main() 中按名称调用它们,而无需创建对象。顺便说一句,你能更具体地说明你想要做什么吗?