Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    return statement in java

    hi all, i need help in using a java "return statement" to return: a fullName, a firstName, lastName and initials with lastName.

    what i have done so far:

    public class ReturnAllValues
    {
    public static void main(String [] args)
    {
    String name = "Faith Hill";
    }
    public String getName()
    {
    return name;
    }

    }

    the program compiles fine but does not return the value of "name". what am i doing wrong?

    what must i do to return all the values?
    your assitance is great appreciated. thank you!

  • #2
    Regular Coder DELOCH's Avatar
    Join Date
    Apr 2006
    Location
    Canada
    Posts
    537
    Thanks
    4
    Thanked 2 Times in 2 Posts
    i'm not quite sure how it is suppose to return anything when you don't even call the function...

    Code:
    public class ReturnAllValues
    {
        private String name = "Faith Hill";
    
        public static void main(String [] args)
        { 
            String myName = getName();
            System.out.println(myName);
        }
        public String getName()
        {
            return name;
        }
    }
    .... that is an example of a code that would 'return' the name

    and what do you mean by 'return all the values'?

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much Deloch. I meant values such as first initials and lastname. for example: F. Hill.

    Also, how can I create a string to hold multiple values in a single parameter and call those values individually with a return statement?

    for example: String name = new String ("Frank Herbert", "Mary Kolly", "Mike Polisi");
    then call each name with a return statement. Again, thank you very much!


    Deloch, i ran the code in jCreator and it coughed up the following error message:

    "non-static method getName() cannot be referenced from a static context"

    The strange thing is that getName() method is under main so i don't understand why it's coughing up this error message.
    Last edited by moist; 01-31-2009 at 07:12 PM.

  • #4
    New Coder
    Join Date
    Jul 2008
    Posts
    71
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by moist View Post
    Also, how can I create a string to hold multiple values in a single parameter and call those values individually with a return statement?

    for example: String name = new String ("Frank Herbert", "Mary Kolly", "Mike Polisi");
    then call each name with a return statement.
    You can make an array of Strings.
    http://java.sun.com/docs/books/tutor...ts/arrays.html


    and the return would be:

    Code:
    return name[3]

  • #5
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by moist View Post

    Deloch, i ran the code in jCreator and it coughed up the following error message:

    "non-static method getName() cannot be referenced from a static context"

    The strange thing is that getName() method is under main so i don't understand why it's coughing up this error message.
    That's because, getName() method is a non static method, and it can not be called from a static method main().

    make the geName() method static, and error will go away.

  • #6
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    simple arraylist usage

    Code:
    import java.util.ArrayList;
    public class Name {
    public static ArrayList<String> names  ;
    
    public static void main (String args[]){
    	names = new ArrayList<String>(20);
    	add("alex");
    	add("loves");
    	add("your");
    	add("pepsi");
    	printall();
    }
    public static void add (String n){
    	names.add(n);
    }
    public static void printall(){
    	for(String c:names){
    		System.out.println(c);
    	}
    }
    }
    sorry for the random text

  • #7
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    'Mutley' I don't understand, what you are trying to explain. is it relevant to this thread?
    This thread is no longer active.

  • #8
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ch4sethe5un mentioned arrays. I thought it was worth the example.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •