Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


Ed
How to return multiple values from a method
2 Answer(s)      4 years and a month 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());
}
}






Advertisement
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 Tutorials/Questions & Answers:
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
Advertisements
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
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
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
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 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
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
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
select tag multiple values
select tag multiple values   I want to insert multiple values in database which i have selected from select tag
@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
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
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
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
multiple select values
multiple select values   can you provide an example for multiple select values for html:select tag
how to insert values from jsp into ms access
how to insert values from jsp into ms access   how to insert values using jsp into ms access database
How to get Keys and Values from HashMap in Java?
How to get Keys and Values from HashMap in Java? Example program of iterating... through keys and get the values from a HashMap object. The entrySet() method... to iterate the keys and get the values and print on the console. How to get keys
how to fetch values from .properties to a html file
how to fetch values from .properties to a html file  I have a .properties file with some key value pairs in it. I need to fetch the values from this .properties file into a html file.Please let me know how to do
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 values from xml using java?
How to values from xml using java?  Hi All, I want to read value from following < Line> xml using java.. In < Line>,data is in format of key and value pair.. i want to read only values..could u plz help me
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
how to get a values from processRecord - JSP-Servlet
how to get a values from processRecord  dear sir, i have a problem in getting a values from java to jsp.Here jsp is used to read a excel sheet that intern call a java program so i want a column values i.e a column contains
How to create file from input values in Jframe ?
How to create file from input values in Jframe ?  hi i m doing my project using java desktop application in netbeans.i designed a form to get the user's academic details and on clicking the submit button,it displays all
retrieve multiple attribute values
XML retrieve multiple attribute values  Hello All Am a beginner. Would like some help with this. The following is part of my XML file: <...; What I would like is to retrieve both the attribute values of process
retrieve multiple attribute values
retrieve multiple attribute values  Hello All Am a beginner. Would like some help with this. The following is part of my XML file: <process... like is to retrieve both the attribute values of process:Output during execution
retrieve multiple attribute values
retrieve multiple attribute values  Hello All Am a beginner. Would like some help with this. The following is part of my XML file: <process... like is to retrieve both the attribute values of process:Output during execution
how to get the values to dropdownlist from oracle database
how to get the values to dropdownlist from oracle database   </script> </head> <body> <select name... * from countryname"); while(rs.next()){ %> <option value="<
What is the return type of the main method?
What is the return type of the main method?  hi, What is the return type of the main method? thanks   Hi, In the java programming the Main() method doesn't return anything hence declared void. In Java, you need
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 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
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
How to invoke method of a library from command line
How to invoke method of a library from command line  Suppose there are two methods in a libray and you have to invoke one of the method from that library directly from the command prompt. How will you do
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
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 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
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
how to pass the mutiple values from <Ui:datagrid hyperlink - JSP-Servlet
how to pass the mutiple values from    I am getting the error when passing the values from this way emp2=${employee.lastName1} & name=${employee.firstName.How to pass the values from hyperlink in column
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
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a 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
how to set values from parent page to iframe in javascript
how to set values from parent page to iframe in javascript  how to set values from parent page to iframe in javascript pls reply me as possible as I am in great trouble
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
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
How to insert data from a combobox and textbox values into DB using JSP?
How to insert data from a combobox and textbox values into DB using JSP?  hi, How to insert a comb-box and a text box values in to DB using JSP? @DB:student; @table:stu_info; Combobox values:(class1,class2,class3); textbox1
 

 

 

DMCA.com