Hi....

I've wrote a code about arrange the names in descending order. But, I'm not sure it's right or wrong. Can u help me, please?

The code is:

import javax.swing.JOptionPane;    
    public class Names
 {
     public static void main(String[] argus){

         int i = 0;
         int n =10 ;
         int j;
         String temp;
         String[] names = new String[10];
         for ( i =0 ; i < names.length ; i++)
         {
             names[i] = JOptionPane.showInputDialog( null, " Enter the Name number " + (i+1));
            }
               System.out.println("Array content before sorting ");
               for ( i = 0; i < names.length ; i++)
               System.out.println( names[i] +" ");

               for ( i = names.length-1 ; i =>0 ; i--)
                   for (j = 0; j < i ; j++)
                       if( names[j] < names[j+1] )
                       {
                           temp = names[j];
                           names[j+1] = names[j];
                           names[j+1] = temp;                                                  
}            
}            
}

I'll wait for u...

Have you run it? Did it work correctly?

It seems like you could tell on your own if it's working or not. If it's not, then post specific questions about what is not working correctly.

Hi..

The Problem was in this statement
for ( i = names.length-1 ; i =>0 ; i--)

the program siad it's an illigal statement.

The another problem was in this statement

if( names[j] < names[j+1] )

the program said we can't use this operation for string .. So, what should I do to compare between them?

Thank u.

use the built in function

eg

String str1 = "hello";
String str2 = "hello";

str1.compareTo(str2) will give you the answer... its no good doin == since that will attempt to check if they are the same object in memory which they should never be.

Hope this helps

Also use i>=0 rather than i=>0 in your for loop.

THIS CODE WILL DO IT 4 U.

import javax.swing.*;
public class Names


{


public static void main(String[] argus){



int i = 0;


int n =10 ;


int j;


String temp;


String[] names = new String[10];



for ( i =0 ; i < names.length ; i++)


{
names = JOptionPane.showInputDialog( null, " Enter the Name number " + (i+1));


}
System.out.println("Array content before sorting ");



for ( i = 0; i < names.length ; i++)


System.out.println( names +" ");



for ( i = 0 ; i <=(names.length)/2 ; i++)
{


temp = names;


names[names.length - i -1] = names;


names = temp;
}
for ( i = 0; i < names.length ; i++)


System.out.println( names +" ");
}
}

hey mate, the problem with the two statements you mentioned is

1. for ( i = names.length-1 ; i =>0 ; i--)
-->names.length()-1

2. if( names[j] < names[j+1] )
-->you'll have to use String.compareTo(String) i think. or compareToIgnoreCase

but i didn't look through the rest of your code.
:icon_eek:

1. for ( i = names.length-1 ; i =>0 ; i--)
-->names.length()-1

You are thinking of the String.length() function. In his case, "names" is an array and names.length is correct.

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.