如何在不替换字符串中先前数据的情况下将函数添加到 main 中的字符串?

how to make a function add on to a string in main without replacing the previous data in string?

我正在尝试制作菜单选项,其中选项 a 的函数将允许用户将数据输入到字符串中。当 a 被召回时,我无法在不删除之前的情况下将新用户输入到字符串中。我尝试使用 concat 仅添加到字符串的末尾,但它只是不断用字符串中的新文本替换旧文本。下面是我的主要 class 选项 a.

for(;;)
        {
        menuOptions();
           String[] menuChoice = new String[4];
           menuChoice[0] = choice.next();
           System.out.println("your choice is " + menuChoice[0] + "\n");
        
        if (menuChoice[0].equals("a"))
        {//menu choice a.
            userInput = optionA(_userInput);
        }

下面是我对选项 a 的函数。

        public static String optionA(String _userInput)
        {
            _userInput = "";
            Scanner s = new Scanner(System.in); //Scanner for user input.
            System.out.println("Enter new text: ");
            if (_userInput == "") {//checks if string is empty
                _userInput = s.nextLine();
                System.out.println(_userInput);
            } else 
            {
            String newLine = s.nextLine();

            _userInput = _userInput.concat(newLine);
            }//end else
            return _userInput;

我的第二个菜单选项允许用户查看字符串中存储的内容。现在当我 运行 选项 a 和 b 两次时,会发生这种情况:

option a 1st time: enter text: "All the way!"
option b 1st time: search: "All" output - index 0
option a 2nd time: enter text: "Even more!"
option b 2nd time: search: "All" output - index -1

所以基本上在我再次 运行 第一个菜单选项后,它只是用新输入替换字符串。我怎样才能把它添加到字符串的末尾?

所以在 main class 中执行此操作并将 _userInput 替换为 userInput

String userInput  = "";
Scanner s = new Scanner(System.in);
...
if (menuChoice[0].equals("a"))
        {
            userInput = optionA(userInput,s);
        }

选项A

public static String optionA(String userInput, Scanner s)
{
      System.out.println("Enter new text: ");
      String newLine = s.nextLine();
      userInput = userInput + newLine;
      return userInput;
}

无论字符串是否为空,它都会做同样的事情。