LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2004, 07:57 AM   #1
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Rep: Reputation: 30
value is lost from a return statement! what??


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).
 
Old 05-30-2004, 08:18 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Remove the red word.

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!
}
}
By having "String" in "String username = user.readLine()" you are creating another string-object named "username". This different string is only valid within "try {" ... "}". After that is gone, and if you then refer to username again, the global one will be referred to, which is not assgned a value yet.

So by remove "String" from the line "String username = user.readLine()" the identifier "username" inside the "try {" ... "}" will also refer to the global one, because now there is no other object named "username".
 
Old 05-30-2004, 01:21 PM   #3
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Quote:
Originally posted by Hko
Remove the red word.

Code:
	String username = user.readLine(); //get the input
Alternatively you could leave the String in, and instead capture the return value of newUsername:

Code:
public static void addNewUser() {  
   username = 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.
}

Last edited by eric.r.turner; 05-30-2004 at 01:23 PM.
 
Old 05-30-2004, 08:32 PM   #4
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Original Poster
Rep: Reputation: 30
Thankyou, that worked, I should have got that... but it was late last night!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault on return statement crosseyedalien Programming 9 06-17-2005 12:23 PM
return statement in functions pantera Programming 2 12-06-2004 06:21 PM
For Statement bendeco13 Linux - General 6 10-31-2004 05:31 PM
If Statement junjem0702 Programming 4 08-23-2004 09:49 AM
I am lost, cant use if statement properly GridX Linux - Newbie 2 10-08-2003 05:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration