Hi all, having a major problem with this code.
Basically I am asking a value to be returned from a method, but it is lost!
When the addNewUser() method is called, it then calls a bunch of different methods ot get values etc and then they will be collated into a profile/user account of that user.
The actual capture of the user input works, but it is lost on return! how come?
my class
Code:
import java.io.*;
class userUtility {
static String username;
public static void addNewUser() {
newUserUsername(); //get the username from the other method
(other calls to methods to go in here later)
System.out.println(username); //THIS PRINTS NULL. argh.
}
public static String newUserUsername() {
System.out.println(" \n\t\t+==============================+");
System.out.println("\t\t| SLMS: Register as a new user |");
System.out.println(" \t\t+==============================+\n");
try{
BufferedReader user = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\t\t Login name for new user or 'quit' to return to the main menu:\n");
String username = user.readLine(); //get the input
username = username.trim(); //get rid of any leading-trailing whitespace
System.out.println(username); //THIS WORKS FINE!
}
catch(IOException ioe){
System.out.println("Invalid username! Please try again!");
newUserUsername(); //reload the method.
}
return username; //BUT AFTER THIS THE VALUE IS LOST!
}
}
Any pointers would be appreciated because I have a different class which does pretty much the same this and the code is exactly the same (except the other class has the 'main' method).