Home Answers Viewqa Java-Beginners How to return multiple values from a method

 
 


Ed
How to return multiple values from a method
2 Answer(s)      2 years and 10 months ago
Posted in : Java Beginners

I came the following method "How do I return multiple values from a method?" posted elsewhere. The problem is that I wanted to tested the method posted, but I am getting the incorrect result. Can someone tell me how my Driver.java program [see below] should be coded?


public class UserName {
protected String ivFirstName, ivLastName;

public UserName(String first, String last) {
ivFirstName = first;
ivLastName = last;
}

public String getFirstName() {
return ivFirstName;
}

public String getLastName() {
return ivLastName;
}
}


public class User {
protected String ivFirstName = "Java";
protected String ivLastName = "Developer";

/**
* This is returning a single Object, which contains
* multiple values.
*/
public UserName getName() {
return new UserName(ivFirstName, ivLastName);
}
}


// This is my Driver.java program to test the method posted.
// The result I got was: UserName@7d8a992f
//
public class Driver {
public static void main (String[] args) {
User method = new User();
System.out.println(method.getName());
}
}





View Answers

February 19, 2013 at 11:47 AM


hi friend,

In your code you are only initializing the value and you are not getting the value try the modified code may this will be helpful for you.

UserName.java

package roseindia.net.example;

public class UserName {
    protected String ivFirstName, ivLastName;

    public UserName(String first, String last) {
    ivFirstName = first;
    ivLastName = last;
    }

    public String getUserName()
    {
        return ivFirstName+" "+ivLastName;
    }

    public String getFirstName() {
        return ivFirstName;
        }

    public String getLastName() {
    return ivLastName;
    }
    }

User.java

package roseindia.net.example;
public class User {
    protected String ivFirstName = "Java";
    protected String ivLastName = "Developer";

    /**
    * This is returning a single Object, which contains
    * multiple values.
    */
    public String getName() {
     UserName un = new UserName(ivFirstName, ivLastName);
     return un.getUserName();

    }
    }

Driver.java

package roseindia.net.example;

public class Driver {
    public static void main (String[] args) {
    User method = new User();
    System.out.println(method.getName());
    }
    }

February 19, 2013 at 12:39 PM


hi

modify your Driver.java class as

public class Driver {
    public static void main (String[] args) {
    User method = new User();
    System.out.println(method.getName().getFirstName()+
    " "+ method.getName().getLastName());
    }
    }









Related Pages:
How to return multiple values from a method - Java Beginners
How to return multiple values from a method  I came the following method "How do I return multiple values from a method?" posted elsewhere..."; /** * This is returning a single Object, which contains * multiple values
How to return multiple values from a method - Java Beginners
How to return multiple values from a method  I came the following method "How do I return multiple values from a method?" posted elsewhere..."; /** * This is returning a single Object, which contains * multiple values
Java Method Return Multiple Values
Java Method Return Multiple Values In this section we will learn about how a method can return multiple values in Java. This example explains you how a multiple values can be return by a method. This example explains you all
How to pass multiple values from a servlet to JSP?
How to pass multiple values from a servlet to JSP?  hi, I want to pass multiple values form a servlet to JSP. Hw do i do that? I am able to send one value at a time without any prb, but I am unable to carry multiple (from two
How to carry multiple values from a Servlet to a JSP?
How to carry multiple values from a Servlet to a JSP?  By using...). ----response.sendRedirect("index.jsp?uname="+username);---- But I want multiple values needs to be carried from my servlet to a JSP. How do I do
how to get multiple hyperlink values from a table column to another jsp file?
how to get multiple hyperlink values from a table column to another jsp file... file named "dbtable" will get the parameter from "index" and search... show the specific details from that hyperlink which was clicked. there are 3
@SessionAttributes multiple values
@SessionAttributes multiple values In this section we will see how you can use @sessionattributes to set multiple session values to it. While developing... and someCombobox. The correct way to use @sessionattributes for multiple values
Java Method Return Value
Java Method Return Value       Java  method Return Value return to the code when... the value within the body of method. The method declared void does not return any
Regarding method returns multiple values
Regarding method returns multiple values  Hi, 1)I have a requirement like,1 method returns multiple values.how to do that one. 2)can i use method, for example like getFile()[] in this way or not. plzzzzzzzzzzzzzzzzzz provide
Retriving multiple values of checkboxes in struts
Retriving multiple values of checkboxes in struts  how to retrive multiple values of checkboxes of jsp in action class   JSP Retrieve checkbox values 1)checkbox.jsp: <html> <body> <form method="post
select tag multiple values
select tag multiple values   I want to insert multiple values in database which i have selected from select tag
PHP Return Statements
. In PHP we can return values, arrays, or object. If the function is called from within the function then execution will immediately stops, return is also used...PHP Returning Statement Values: PHP Return statement is used to return a value
how to store multiple values from drop down in database where i am using java struts 1.3
how to store multiple values from drop down in database where i am using java... is displaying.. i need code in java so that it takes multiple values... where i have to select multiple keyskills. but it is taking only one valuee.. i
PHP Functions and Return values
PHP Functions and Return values A function is the a block of code whom you can... need it. To protect the script from being auto executed on page load , we put... to the function. You may call these function from any where within page. Syntax
How to return a string from a JTextField
How to return a string from a JTextField  Hi, Am new to java, and am trying to get a string from a JTextField so that I can compare it with another.... The problem is i have no clue how to get the very string i need because i think
how to get a values from a multipart/form-data - JSP-Servlet
how to get a values from a multipart/form-data  Dear sir , I used your code for getting a values,i used the fllowing function... ,matter,file field values but not status and from field i am getting null
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row  Hai, I need... or .txt) file and get the values from that file and store them into database
Call an array from multiple functions
Call an array from multiple functions  I am having two methods in this method i am getting array values from another class. i create same array name for accessing the method values(array) but it will access only one method values
PHP Returning Values from Function
Returnig Values from function: From a function we can return values using optional return statement, in PHP we can return arrays, objects. With the help of the return statement the control moves back to the calling portion
How to get a values - JSP-Servlet
getting a null value aprt from a attachmented file,so how to get a other values...How to get a values  Dear sir, I have a one form with a multipart/form-data as follows To From Subject
calling webservices that return complexTypes in android using ksoap2
calling webservices that return complexTypes in android using ksoap2   Here's my code for android application,to call aweb service method and return...{} . How can I retrieve the real values
how to display multiple images on browser
how to display multiple images on browser  i want to display multiple images on brower including string values from sql database using jsp
how to display multiple images on browser
how to display multiple images on browser  i want to display multiple images on brower including string values from sql database using jsp
retrieve multiple columns values from multiple csv files in java
retrieve multiple columns values from multiple csv files in java  Suppose there is folder in C:\fileupload which contains these two csv files...)///// here VendorID is corresponding to file1.csv i want to retrive data from
Passing java variables from JSP to Servlet - return null values
Passing java variables from JSP to Servlet - return null values  I want to pass some variables from a JSP page to a servlet. These variables are from... from JSP to servlet gives null values. I got msg=null. Is there another way
return all rows mysql php
return all rows mysql php  How to find or return results that are not available in another table .. using join method in PHP and MYSql.   SELECT * FROM Declined LEFT JOIN Request ON Declined.RequestID
How to Display values from databse into table
How to Display values from databse into table  I want to display values from database into table based on condition in query, how to display... enter either bookname or authorname and click on search button the values from
method - Java Beginners
. Suppose, for instance, that you want to write a method to return what..., canWithdraw, canClose; } Here is a method that fills in field to return multiple values: Public class BankAccount { private long number; private long
insert multiple selection - Java
insert multiple selection - Java  how to insert multiple selection values from html into database using servlets
Multiple values for a single parameter
Multiple values for a single parameter   ... from where the values will be passed to the controller. In this program we have... the values we have entered in the html form by the method getParameterValues
Return Value from One Form
Return Value from One Form  I have two buttons in main form client and bank.when i click client button new form will open, in this dedupe button... value of dedupe form and use in bank form..how to do
java method return type : - Java Beginners
java method return type :  i have one question regarding methods,,, if we create a method with return type as class name (public employee addemp(int... int classasmethod() { System.out.println("classasmethod method"); return 10
Remove multiple elements in arraylist
Remove multiple elements in arraylist   how can we remove multiple values in arrayList? only remove method we used for single remove.but i want to delete multiple value in arrayList
calling a javascript function by passing multiple values from applet in web application
calling a javascript function by passing multiple values from applet in web application  Hi all, I have a requirement like this, I have to call a javascript function from JApplet by passing 3 values. I am new to Applets
Data from multiple ResultSets?
Data from multiple ResultSets?  hello, How can I get data from multiple ResultSets?   Hi, You can iterate the ResultSet separately and get the required data. Thanks
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file  Hi am new to java. i need to create... Solution field from database and display in the textbox of the jsp.   1
retrive values - JSP-Servlet
retrive values  how to get multiple values from html to jsp
Java Multiple Insert Query
the value after selecting the multiple values from the dropdown list...Java Multiple Insert Query In this example we will discuss about how to execute multiple insert query in Java. This example explains you about how
i am getting multiple values in listbox
i am getting multiple values in listbox   hi sir, i am using msaccess 2007.my query is look like this. select distinct a.name,b.fee from table1 a,table2 b where a.stuno=b.stuno and b.recdate in(select max(recdate) from
how to retrieve data from table with runtime values in sql?
how to retrieve data from table with runtime values in sql?  how to retrieve data from table with runtime values in sql? For example, I have table... value. But I dont know how to retrieve the values whenever i pass different dates
how to retrieve data from multiple tables in jsp using javabeans
how to retrieve data from multiple tables in jsp using javabeans   ...; private String name; public int getBook_id() { return Book_id; } public... getName() { return name; } public void setName(String name) { this.name
how to retrieve data from multiple tables in jsp using javabeans
how to retrieve data from multiple tables in jsp using javabeans   ...; private String name; public int getBook_id() { return Book_id; } public... getName() { return name; } public void setName(String name) { this.name
how to retrieve data from multiple tables in jsp using javabeans
how to retrieve data from multiple tables in jsp using javabeans   ...; private String name; public int getBook_id() { return Book_id; } public... getName() { return name; } public void setName(String name) { this.name
PHP Return
(or multiple values using array in PHP) or the control to the calling line from the function. After getting a return statement the PHP parser ignores... is returned from the return statement then it is mandatory to remove the parenthesis
Return Array in PHP
Return Array in PHP  my question is how to return Array from PHP script
How to call the run method from another class?
How to call the run method from another class?  Sorry, if this is a stupid question. I would like to find out how to call the run method... to FereastraPrincipala after introducing values in the textfields from
How to retrieve array values from html form to jsp?
How to retrieve array values from html form to jsp?  Hi! I am... sample code for how to retrive array values from html to jsp.   hi friend... it into jsp. Means i just want to retrieve values from html form containing array
JDialog return value to JDialog ????
JDialog return value to JDialog ????  hello all.... legend. JD1 = JDialog1 JD2 = JDialog2 JD1 open JD2......JD2 has any values, how I return this values to JD1 ??? // JD1 open JD2 JD2 jd2 = new JD2(null, true
JDialog return value to JDialog ????
JDialog return value to JDialog ????  hello all.... legend. JD1 = JDialog1 JD2 = JDialog2 JD1 open JD2......JD2 has any values, how I return... need return this result on JLabel to JD1. Please show me a how to or example
how to pass values from 2nd page tp 3rd page....
how to pass values from 2nd page tp 3rd page....  Hi, How to pass values from 2nd page to 3rd page..... here are my programs(not exactly correct... how to get the values(of userid) from Login1.jsp.... plz help me

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.