Home arrow static arrow Java Programming [Archive] - Get method
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Get method
This topic has 6 replies on 1 page.

Posts:1,367
Registered: 1/5/04
Get method  
Jun 18, 2004 2:48 AM



 
class Test
{
String x;
String y;

Test(String s1,String s2)
{

x=s1;
y=s2;

}

Get( )
{

// how do i return two Strings i.e x and y ? here i have one method. but i want to return 2 string .

}

}

}

 

Posts:5,119
Registered: 16.08.01
Re: Get method  
Jun 18, 2004 3:00 AM (reply 1 of 6)



 
either:
public String[] get(){  return new String[]{x,y};}

or:
public String get(){  return x + " " + y;}
 

Posts:1,367
Registered: 1/5/04
Re: Get method  
Jun 18, 2004 3:47 AM (reply 2 of 6)



 
class T
{
String x;
String y;

public void set(String k,String m)
{
x = k;
y = m;
}

public String[]get()
{

String v = new String[2];
v[0]=x;
v[1]=y;
return v;

}

}

class T1

{

public static void main(String args[])
{
String array = new String[2];
T obj = new T();
obj.set("hello","world");

array = obj.get();

System.out.println("1st String="array[0]"2nd String="+array[1]);

}

}


getting error in the code

 

Posts:1,888
Registered: 22/08/01
Re: Get method  
Jun 18, 2004 3:55 AM (reply 3 of 6)



 
read this : http://forum.java.sun.com/features.jsp#Formatting first

then post the error you get, we cant see the message you get from were we are.
 

Posts:1,367
Registered: 1/5/04
Re: Get method  
Jun 18, 2004 4:01 AM (reply 4 of 6)



 
C:\j2sdk1.4.2_03\bin>javac C:\mycode\T1.java
C:\mycode\T1.java:16: incompatible types
found : java.lang.String[]
required: java.lang.String
String v = new String[2];
^
C:\mycode\T1.java:17: array required, but java.lang.String found
v[0]=x;
^
C:\mycode\T1.java:18: array required, but java.lang.String found
v[1]=y;
^
C:\mycode\T1.java:19: incompatible types
found : java.lang.String
required: java.lang.String[]
return v;
^
C:\mycode\T1.java:33: incompatible types
found : java.lang.String[]
required: java.lang.String
String array = new String[2];
^
C:\mycode\T1.java:37: incompatible types
found : java.lang.String[]
required: java.lang.String
array = obj.get();
^
C:\mycode\T1.java:39: array required, but java.lang.String found
System.out.println("1st String="array[0]"2nd String="+array[1]);
^
C:\mycode\T1.java:39: array required, but java.lang.String found
System.out.println("1st String="array[0]"2nd String="+array[1]);
^
8 errors

C:\j2sdk1.4.2_03\bin>
 

Posts:1,888
Registered: 22/08/01
Re: Get method  
Jun 18, 2004 4:06 AM (reply 5 of 6)



 
the error says what you did wrong

array required, but java.lang.String found

You are trying to store an array in a String and thats a big nono, make sure the variabel you want to store the array in , is an array too.
 

Posts:1,367
Registered: 1/5/04
Re: Get method  
Jun 18, 2004 9:23 AM (reply 6 of 6)



 
thanks ...i have found error....i fixed it.. it was a array syntax problem.

thanks
 
This topic has 6 replies on 1 page.