View Full Version : return statement in java
moist
01-31-2009, 01:50 AM
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...
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
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/tutorial/java/nutsandbolts/arrays.html
and the return would be:
return name[3]
servlet
02-02-2009, 08:57 AM
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
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.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.