How to return two values from one method ??
for example i've made the following program.

import java.io.*;
class vowel{
public static void main (String args[])throws IOException{
String sentence;
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
sentence = br.readLine();
System.out.print (vowels(sentence));
	}
	static char vowels (String default1){
	char ch;
	int vowels_position [] = {0,0,0,0,0};
	char vowels_value [] = {'a','e','i','o','u'};
	for (int i=0; i<default1.length(); i++){
	ch=default1.charAt(i);
	switch (ch) {
	case 'a': case 'A':{vowels_position[0]++;break;}
	case 'e': case 'E':{vowels_position[1]++;break;}
	case 'i': case 'I':{vowels_position[2]++;break;}
	case 'o': case 'O':{vowels_position[3]++;break;}
	case 'u': case 'U':{vowels_position[4]++;break;}
	  }
	}
	int count=0;
	for (int k=0; k<vowels_position.length; k++)
	if (vowels_position[k]>vowels_position[count])
	count=k;
	return (vowels_value[count]);
  }
}

this program counts every vowel in a string and then return the vowel which appeared mostly, i want my program not only return the vowel that appeared most time but also return count of that vowel.

i want to return vowels_value[count] & vowels_position[count]

Recommended Answers

All 13 Replies

The return statement only returns the value of one variable.
The prototype for a method only specifies one type to be returned by the method.
Hence there is no way to return the values of more than one variable in one statement.

You either need to redesign the logic of the method, say have it return the index: count
or put all the values you want to return into a new class and return a reference to that class.

The formatting of your code makes it very difficult to see where the method definition is. Can you make it like this:

System.out.print (vowels(sentence));
    }
   // Find and return the value ...
   static char vowels (String default1){
	char ch;

or put all the values you want to return into a new class and return a reference to that class.

what does it mean ??

You can only return one item from a method - period.
However, that item can be a complex object. For example, if you made a method which calculated a location on a graph, you could not return two ints (x,y) but you could (and would) return a Point object, from which you could getX() and getY().

I think you could simplify things here by returning a Map of vowels to frequencies - then you'd have the whole shebongle and your calling method could select any vowels that it was interested in knowing about the frequencies of.
However, if you want to keep your current structure, you could make a little VowelFrequency object with two fields: the vowel being counted and the frequency of occurrence. You could return that, it would be a unitary object.

Define a class that will hold the values you want to return.
In the new class's constructor take those values and save them in class variables.
Return a reference to that new class:

MyValsClass myVals = new MyValsClass( vowels_value[count], vowels_position[count]);
return myVals; // return the object containing the values to be returned


Why can't your method return: count and let the caller use that to index into the two arrays.

sorry but i am still a beginner , i don't know that much about java so i can't use it


Define a class that will hold the values you want to return.
In the new class's constructor take those values and save them in class variables.
Return a reference to that new class:

Can you give a little code about using constructor and different classes. ??


I've done this , but is there any way to minimize the code ?

import java.io.*;
class vowel{
public static void main (String args[])throws IOException{
String sentence;
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
sentence = br.readLine();
System.out.print (vowels(sentence) + "" + vowels1(sentence));
	}
	// Method Returning the Vowel
	static char vowels (String default1){
	char ch;
	int vowels_position [] = {0,0,0,0,0};
	char vowels_value [] = {'a','e','i','o','u'};
	for (int i=0; i<default1.length(); i++){
	ch=default1.charAt(i);
	switch (ch) {
	case 'a': case 'A':{vowels_position[0]++;break;}
	case 'e': case 'E':{vowels_position[1]++;break;}
	case 'i': case 'I':{vowels_position[2]++;break;}
	case 'o': case 'O':{vowels_position[3]++;break;}
	case 'u': case 'U':{vowels_position[4]++;break;}
	  }
	}
	int count=0;
	for (int k=0; k<vowels_position.length; k++)
	if (vowels_position[k]>vowels_position[count])
	count=k;
	return (vowels_value[count]);
  }
    // Method Returning the Vowel Count
  	static int vowels1 (String default1){
	char ch;
	int vowels_position [] = {0,0,0,0,0};
	char vowels_value [] = {'a','e','i','o','u'};
	for (int i=0; i<default1.length(); i++){
	ch=default1.charAt(i);
	switch (ch) {
	case 'a': case 'A':{vowels_position[0]++;break;}
	case 'e': case 'E':{vowels_position[1]++;break;}
	case 'i': case 'I':{vowels_position[2]++;break;}
	case 'o': case 'O':{vowels_position[3]++;break;}
	case 'u': case 'U':{vowels_position[4]++;break;}
	  }
	}
	int count=0;
	for (int k=0; k<vowels_position.length; k++)
	if (vowels_position[k]>vowels_position[count])
	count=k;
	return (vowels_position[count]);
  }
}

Norm sez:
Why can't your method return: count and let the caller use that to index into the two arrays.

That's true. If your arrays were fields of this class instead of method variables, then you could simply return the index, since you're using the same index for both arrays.


This is a matter of scope - that's a way of talking about where a variable can be used within a program. There's a lot to be known about this, and I won't go into it very deeply right now, but what you need to know at the moment is this:

a variable can generally be called on after it is declared, and up to the end of the statement block that it appears in, and in any blocks that are enclosed in that block. A statement block is the space in between a pair of curly braces: {}.

Here's a toy method that will give you an idea:

public void method1()
{
  int q;
  for (int i = 0; i <4; i++)
  {
    int r = i +7;
    int q = i*i;
  }
  
  System.out.println(q);  // fine, since q is in scope
  System.out.println(r);  //  no! r is out of scope, and it no longer exists
  
}

A variable declared outside of any method in a class is in some ways just another variable, and in some ways not - you can think of it as a variable for now. (It's stored a little differently, but that's not important). This is often called a "field" of the class, and it has to be declared as "public" or "private".
In terms of scope, it appears as follows:

public class ThisClass
{
  private  int myField =0;

  public void AMethod()
  {
        //can see myField here!
   
     while (true)
     {
        //can see myField here!
     }
  }

  public static void AnotherMethod()
  { 
       //can see myField here too!
 }
}

Since the scope of myField encloses all of the class's methods, it is available to all of the methods.

So if you were to make the arrays in your program into class fields, you could call them throughout the class, in any method, and that should solve your problem.

Hey thanks jon.kiparsky!!!

But how could i use constructor and multiple classes in this program ?? i mean what should i have to store in constructor and in class ??

Well, you wouldn't store anything in constructor - a constructor is a method, and a method can't store anything. Whatever exists in the method exists only as long as the method is on the stack - when the method is called, it goes on the stack and its variables exist, things happen to them, maybe it calls other methods, but eventually it disappears and any variables it had go with it.

So by making the arrays into private fields of your class vowel, you'd make them persistent. They live on the heap, and as long as that class has a current reference, you can get at those. They're also in scope as long as you're calling on them from within vowel.

Or, by creating a data class to hold the two pieces of data, you just create a class with two fields, a char and an int, and getter methods to return those. Construct an instance of the new class while you're in your vowels method, and use that as the return. That way, again, you have persistence because your data is on the heap, and as long as you have a reference to the data object (ie, as long as you capture the return value when it's returned) you can do what you like with the data.

I didn't understand you at all :( because we are still on beginning of Java ....i read you post about 20 times but didn't understand !!
can you please provide some example code ???

Okay, stepping back:
A method can return exactly one value, or none. If the method's declaration says it will return a value, it must return a value of the correct type (or null). If it is declared as returning void, it may not return anything. (It can have a return statement, which means "stop executing this method and return to where the method was called", but that return cannot take an argument).

So to answer your explicit question, no you cannot solve the problem in the way you wanted, by putting two values in your return statement.

However, the value that you return can be a reference to an object. An object is a collection of data that can do tricks. So, if you can define a class that contains the sort of data you want to return, you can return an object of that class, and everything's hunky-dory.
That's one way to solve your problem.

Are you okay with this so far? If not, what do you not understand (of this post, we'll move on when you have this)

commented: great :) +0
import java.io.*;
class vowel{
public static void main (String args[])throws IOException{
String sentence;
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
sentence = br.readLine();
System.out.println (vowels(sentence)[0]);
System.out.println (vowels(sentence)[1]);
	}
   static Object[] vowels (String default1){ // return an Object array including various data types
	char ch;
	int vowels_position [] = {0,0,0,0,0};
	char vowels_value [] = {'a','e','i','o','u'};
	for (int i=0; i<default1.length(); i++){
	ch=default1.charAt(i);
	switch (ch) {
	case 'a': case 'A':{vowels_position[0]++;break;}
	case 'e': case 'E':{vowels_position[1]++;break;}
	case 'i': case 'I':{vowels_position[2]++;break;}
	case 'o': case 'O':{vowels_position[3]++;break;}
	case 'u': case 'U':{vowels_position[4]++;break;}
	  }
	}
	int count=0;
	for (int k=0; k<vowels_position.length; k++)
	if (vowels_position[k]>vowels_position[count])
	count=k;
	Object o[]=new Object[2]; // only two different types of elements are needed
	o[0]=vowels_value[count];  // char type element
	o[1]=vowels_position[count]; // int type element
	return o; // return the multi-type Object array
  }
}
commented: great :) +0
commented: Give help, not code. +0

Thanks alot every one, specially jon and tong :)
Thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.