I want the user to enter a lower case b and the program recognizes that the b is lower case, and switch it to an upper case B. Since you cannot edit Strings during run time, how can I use a separate method from the main method in order to assign "name" to "name2". Once I try to print out "name2" after the method changes it, the compiler claims name2 hasn't been initialized even though it has been in a separate class.
I've tried initializing name2 as name2="";
so the compiler doesn't say "name2 hasn't been initialized once I try to print name2 to console, however, you cannot redefine a String during run time.
import java.util.Scanner;
public class Runner
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String name;
String name2;
TextArguments test1 = new TextArguments();
System.out.println("Please enter a lower case b");
name = input.nextLine();
test1.test(name,name2);
System.out.println(name2);
}
}
public class TextArguments
{
public String test(String name, String name2)
{
if(name == "b")
{
name2 = "B";
}
else
name2 = name;
return name2;
}
}
Expected results:
Enter a lowercase b. b B
Actual Results:
test1.test(name,name2); ("name2 might have no been initalized")