CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   return statement in java (http://www.codingforums.com/showthread.php?t=157540)

moist 01-31-2009 01:50 AM

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!

DELOCH 01-31-2009 03:31 AM

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'?

moist 01-31-2009 03:14 PM

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.

ch4sethe5un 02-01-2009 11:14 PM

Quote:

Originally Posted by moist (Post 776455)
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]

servlet 02-02-2009 08:57 AM

Quote:

Originally Posted by moist (Post 776455)

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.

Mutley 02-08-2009 10:57 PM

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

servlet 02-09-2009 05:51 AM

'Mutley' I don't understand, what you are trying to explain. is it relevant to this thread?
This thread is no longer active.

Mutley 02-09-2009 08:11 AM

ch4sethe5un mentioned arrays. I thought it was worth the example.


All times are GMT +1. The time now is 01:29 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.