STRING sorting plz correct it
class sortt
{
public static void main(String args[])
{
String []a={"rawezh","ehsan","hewa","kawan"};
A ob=new A();
ob.B(a);
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}
}
class A
{
public static void B(String[]a)
{
int n=a.length;
for(int i=1;i<n;i++)
{
String c=a[i];
int j=i-1;
while ((j>=0)&&(a[j]>c))
Quote:
i have error in this line
a[j+1]=a[j--];
a[j+1]=c;
//System.out.print(+" ");
}
}
}
Re: STRING sorting plz correct it
Please explain the problems you are having with the posted code. Show the program's output and explain what is wrong and show what it should be.
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: STRING sorting plz correct it
i have error in line 29 and i cant solve it ....
Re: STRING sorting plz correct it
Please post the full text of the error message.
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: STRING sorting plz correct it
i wanna sort String by slection sort.. so in the line 29 have error.
this is a error :: sortt.java:29: operator > cannot be applied to java.lang.String,java.lang.String
while ((j>=0)&&(a[j]>c))
and i cant slove it..
Re: STRING sorting plz correct it
Quote:
operator > cannot be applied to java.lang.String,java.lang.String
You can not use the > operator with objects like Strings. You need to use methods when working with objects. Read the API doc for the String class to find some methods that are useful for comparing the contents of String objects. The method will return an int value that signifies the relationship between the two Strings being compared.
Re: STRING sorting plz correct it
Code java:
<class sortt
{
public static void main(String args[])
{
String []a={"rawezh","ehsan","hewa","kawan"};
A ob=new A();
ob.B(a);
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}
}
class A
{
public static void B(String[]a)
{
int n=a.length;
for(int i=1;i<n;i++)
{
String c=a[i];
int j=i-1;
while ((j>=0)&&(a[j]>c))
a[j+1]=a[j--];
a[j+1]=c;
//System.out.print(+" ");
}
}
}
>
--- Update ---
thank u very muchh
Re: STRING sorting plz correct it
Re: STRING sorting plz correct it
no i cant solve it.. what is the method ?
Re: STRING sorting plz correct it
The one that is used to compare Strings.
Look at the API doc for all the details:
http://docs.oracle.com/javase/7/docs/api/