Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: STRING sorting plz correct it

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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))
    i have error in this line
    a[j+1]=a[j--];
    a[j+1]=c;
    //System.out.print(+" ");


    }


    }





    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: STRING sorting plz correct it

    i have error in line 29 and i cant solve it ....

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: STRING sorting plz correct it

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 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))
    			a[j+1]=a[j--];
    			a[j+1]=c;
    			//System.out.print(+" ");
     
     
    		}
     
     
    	}
     
     
     
     
     
    }
    >


    --- Update ---

    thank u very muchh

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: STRING sorting plz correct it

    Does it work now?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: STRING sorting plz correct it

    no i cant solve it.. what is the method ?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  2. is my code correct?
    By leonne in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 12th, 2012, 04:36 PM
  3. there is an exception in my program plz correct it.
    By nikita in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2012, 04:42 AM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. Correct connection to DB?
    By kney in forum JDBC & Databases
    Replies: 12
    Last Post: November 23rd, 2011, 02:17 PM