Jump to content

sorting characters...

- - - - -

  • Please log in to reply
4 replies to this topic

#1
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
hi everyone again.... i had to make a program that should allow the user to input 5 characters and then you sort them in descending order and display the output...
i've made something like this.. it compiles but does not run...


 public static void main (String args[]) {

    	String [] num=new String [5];

    	int n=num.length;

    	int i;

    	int j;

    	for (i=0; i<num.length; i++){

    		String enter=JOptionPane.showInputDialog ("Enter a character:");

    	}


    	int out, in;


    	for (out=1; out<n; out++){

    		String temp=num[out];

    		in=out;

    		while (in>0&&num[in-1].compareTo(temp)>=0){

    			num[in]=num[in-1];

    			--in;

    		}

    		num[in]=temp;

    	}

Please, can anyone help me???
Thanks a lot!!!

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You want this:
public static void main (String args[]) {
        String [] num=new String [5];

        for (int i=0; i<num.length; i++){
            num[i] =JOptionPane.showInputDialog ("Enter a character:");
        }

        Arrays.sort(num, Collections.reverseOrder());
        for(int i=0 ; i<num.length ; i++){
            System.out.println(num[i]);
        }
    }


#3
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
i could have.. but we must use insertion sorting.... thats why i'm stuck,,, as a beginner.... i'm having a bit of difficulty here....

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
ppppffffffff :c-lol:


public static void main (String args[]) {

        String [] num=new String [5];


        for (int i=0; i<num.length; i++){

            num[i] =JOptionPane.showInputDialog ("Enter a character:");

        }


        for(int i=1 ; i<num.length ; i++){

            for(int j=0 ; j<i ; j++){

                if(num[i].compareTo(num[j])>0){

                    String temp = num[i];

                    num[i] = num[j];

                    num[j] = temp;

                }

            }

        }


        for(int i=0 ; i<num.length ; i++){

            System.out.println(num[i]);

        }

    }


I hope that is insertion sorting...

#5
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
aaaaaaa.. you're laffing at me?????:c-blink:
Thanks a lot!!!!!
looks like i made a mistake in the insertion sort....
in the insertion sort....




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users