Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 4.67 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-31-2009, 01:50 AM   PM User | #1
moist
New to the CF scene

 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
moist is an unknown quantity at this point
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!
moist is offline   Reply With Quote
Old 01-31-2009, 03:31 AM   PM User | #2
DELOCH
Regular Coder

 
DELOCH's Avatar
 
Join Date: Apr 2006
Location: Canada
Posts: 537
Thanks: 4
Thanked 2 Times in 2 Posts
DELOCH is an unknown quantity at this point
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'?
DELOCH is offline   Reply With Quote
Old 01-31-2009, 03:14 PM   PM User | #3
moist
New to the CF scene

 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
moist is an unknown quantity at this point
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..
moist is offline   Reply With Quote
Old 02-01-2009, 11:14 PM   PM User | #4
ch4sethe5un
New Coder

 
Join Date: Jul 2008
Posts: 71
Thanks: 7
Thanked 3 Times in 3 Posts
ch4sethe5un is an unknown quantity at this point
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]
ch4sethe5un is offline   Reply With Quote
Old 02-02-2009, 08:57 AM   PM User | #5
servlet
Regular Coder

 
Join Date: Jan 2009
Location: india
Posts: 145
Thanks: 0
Thanked 5 Times in 5 Posts
servlet is an unknown quantity at this point
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.
servlet is offline   Reply With Quote
Old 02-08-2009, 10:57 PM   PM User | #6
Mutley
New to the CF scene

 
Join Date: Feb 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Mutley is an unknown quantity at this point
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
Mutley is offline   Reply With Quote
Old 02-09-2009, 05:51 AM   PM User | #7
servlet
Regular Coder

 
Join Date: Jan 2009
Location: india
Posts: 145
Thanks: 0
Thanked 5 Times in 5 Posts
servlet is an unknown quantity at this point
'Mutley' I don't understand, what you are trying to explain. is it relevant to this thread?
This thread is no longer active.
servlet is offline   Reply With Quote
Old 02-09-2009, 08:11 AM   PM User | #8
Mutley
New to the CF scene

 
Join Date: Feb 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Mutley is an unknown quantity at this point
ch4sethe5un mentioned arrays. I thought it was worth the example.
Mutley is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:52 AM.


Advertisement
Log in to turn off these ads.