public class Exercise {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) {


     String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana","Rihanna"};
    String B[] = {"Madona","Diddy","Joe","Alice"};


             for(int j = 0; j < A.length; j++) {
            for(int s = j +1; s < B.length; s++)
      for(int i = 0 ; i < B.length; i++) { 
      if(A[j].compareToIgnoreCase(B[i]) < 0) { 

      } 
       } 

       List<String> list = new ArrayList(Arrays.asList(A));
        list.addAll(Arrays.asList(B));
       Object[] C = list.toArray();


       System.out.println(Arrays.toString(C));




    }
        }
            }

And the out put was like this:

run:
[Joseph, Ariana, Xena, Rene, Peter, Diana, Rihanna, Madona, Diddy, Joe, Alice]
[Joseph, Ariana, Xena, Rene, Peter, Diana, Rihanna, Madona, Diddy, Joe, Alice]
[Joseph, Ariana, Xena, Rene, Peter, Diana, Rihanna, Madona, Diddy, Joe, Alice]
[Joseph, Ariana, Xena, Rene, Peter, Diana, Rihanna, Madona, Diddy, Joe, Alice]
[Joseph, Ariana, Xena, Rene, Peter, Diana, Rihanna, Madona, Diddy, Joe, Alice]
[Joseph, Ariana, Xena, Rene, Peter, Diana, Rihanna, Madona, Diddy, Joe, Alice]
[Joseph, Ariana, Xena, Rene, Peter, Diana, Rihanna, Madona, Diddy, Joe, Alice]
BUILD SUCCESSFUL (total time: 6 seconds)

Edited 4 Years Ago by Ezzaral: code formatting

What are the results when you execute the code?

Post the full text of the error messages here.

Edited 4 Years Ago by NormR1

What does the print out show? The 7 lines you printed all look the same. Why were 7 lines printed?
Can you print the array before sorting and again after sorting it to show what the sort code has done?

First i merged the two arrays:

public static void main(String[] args) {


         String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana","Rihanna"};
        String B[] = {"Madona","Diddy","Joe","Alice"};


        List<String> list = new ArrayList(Arrays.asList(A));
      list.addAll(Arrays.asList(B));
      Object[] C = list.toArray();


      System.out.println(Arrays.toString(C));




    }
}

How to be connected with these two functions:

      for(int j = 0; j < C.length; j++) { 

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

      if(C[i].compareToIgnoreCase(C[j]) < 0)  { 

      String K = C[j]; 

        C[j] = A[i]; 

      C[i] = K; 

      } 

      } 

  System.out.println(C[j]);


  } 
 }

Edited 4 Years Ago by Ezzaral: Code formatting

Add this after you sort the C array to see its contents:
System.out.println("C after sort=" + Arrays.toString(C));

I can not read and understand your code because it is very poorly formatted. The }s shoud be vertically beneath the statement with its paired {
Your }s are not in the correct columns

Edited 4 Years Ago by NormR1

Do you mean like this:

  public class Opdracht1 {


   public static void main(String[] args) {


     String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana","Rihanna"};

    String B[] = {"Madona","Diddy","Joe","Alice"};



       ** List<String> list = new ArrayList(Arrays.asList(A));

      list.addAll(Arrays.asList(B));

      Object[] C = list.toArray();

**

                        System.out.println("C after sort=" + Arrays.toString(C));


                       **for(int j = 0; j < C.length; j++) { 


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


                 if(C[i].compareToIgnoreCase(C[j]) < 0) { 


                    String K = C[j];


                     C[j] = A[i]; 


              C[i] = K; **


           } 


             } 


                  System.out.println(C[j]);




     }
       }
                }

Edited 4 Years Ago by huphane: Modifying

I would fix your code tags but it has no value as your code formatting indention does not make any kind of sense. See Formatting help from link in right top corner of the editor box. Basically you should highlight your code and push TAB to indent it. But first you have to have some indention worth showing.

Is the call to println on line 14 before or after the code that does the sorting?

You should show the C array both before and after you sort it.

No. For example the last } in the code should be in column 1. The } on line 34 is to the right of the } on line 33. It should be to the left.
Your extra blank lines also make the code hard to read.

You did not have the code print out the array both before the sort and after the sort. The comment with where you do print the array looks wrong.

Let me start the whole story over again. I have sorted one array using compareTo method:

               public class Exercise {

               public static void main(String[] args) {


     String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana","Rihanna"};


                    for(int j = 0; j < A.length; j++) {
                        for(int i = j +1; i < A.length; i++) {
            if(A[i].compareToIgnoreCase(A[j]) < 0) { 

            String K = A[j]; 
            A[j] = A[i]; 
            A[i] = K; 
            } 
            } 
            System.out.println(A[j]);


            } 
            } 
            }

This was the original exercise and i have got it working, but i asked myself "How about

if you got two arrays how you gonna do it? So if you add an other array like this one:

  String B[] = {"Madona","Diddy","Joe","Alice"};

How you get the job done?

Edited 4 Years Ago by pyTony: You can not do bold inside code segment with **

A little better. However there should NOT be }s one below the other like lines 24, 25 and 26.

Lines 16,17,18 should be indented to show they are inside of the if statement.

After you merge the two arrays, you have one array. You have the code to sort that one array.

Edited 4 Years Ago by NormR1

If you remove one of those )s you get a compile error, i have tried it.

Not being Java programmer, but knowing the general principles, I would expect formatting similar to this from your code:

public class Exercise {
    public static void main(String[] args) {
           String A[] = {"Joseph","Ariana","Xena","Rene","Peter","Diana","Rihanna"};
           for(int j = 0; j < A.length; j++) {
                   for(int i = j +1; i < A.length; i++) {
                           if(A[i].compareToIgnoreCase(A[j]) < 0) {
                                String K = A[j];
                                A[j] = A[i];
                                A[i] = K;
                           }
                   }
                   System.out.println(A[j]);
           }
    }
}

Can you see any change in readability?

Edited 4 Years Ago by pyTony

I'm talking about what columns they should be in, their alignment and positions. The compiler will tell you if there are too many or too few. The comiler does not care what columns they are in. You could put all the code on one line and get it to compile.

Thanks i can see the chages: Can anybody connect those two methods?

    package ExeCise1;

   import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.List;




  public static void main(String[] args) {

    String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana","Rihanna"};

           String B[] = {"Madona","Diddy","Joe","Alice"};

              List<String> list = new ArrayList(Arrays.asList(A));

                 list.addAll(Arrays.asList(B));

                     Object [] C = list.toArray();

                        System.out.println(Arrays.toString(C));


                        Here starts the trick. How can you connect those two methods?



                   **  for(int j = 0; j < C.length; j++) {

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

                           if(C[i].compareToIgnoreCase(C[j]) < 0) {

                                  String K = C[j]; 

                                      C[j] = C[i]; 

                                           C[i] = K; 


                                        }

                                  }

                                 System.out.println(A[j]);


                          }

                      }

               }

    **

Your formatting is still wrong. DO NOT indent every line of code. Indent code within {}s.

Look at how the previous post by pyTony formatted code.

connect those two methods?

There is only one method named main in your code.
Can you explain what you mean?

Have you tried compiling the code you posted?

Please post here the compiler error messages you get.

public class Exercise {

        /**
       * @param args the command line arguments
       */

        public static void main(String[] args)       {


     String A[] = { "Joseph","Ariana","Xena","Rene","Peter","Diana","Rihanna"};

          String B[] = {"Madona","Diddy","Joe","Alice"};




           List<String> list = new ArrayList(Arrays.asList(A));

                  list.addAll(Arrays.asList(B));

                          Object[] C = list.toArray();


                                  System.out.println(Arrays.toString(C));




                }
        }







            Can anyone help me sorting Array C{} using compareTo method Like this:







                    for(int j = 0; j < C.length; j++)    { 

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

                                    if(C[i].compareToIgnoreCase(C[j]) < 0) { 

                                            String K = C[j]; 

                                                C[j] = C[i]; 

                                                    C[i] = K; 

                            } 

                      } 

                                System.out.println(C[j]);



            } 

 }
Comments
Not learning from advice

Even I tried this way:

         public static void main(String[] args) {


     String a [] = { "Joseph","Ariana","Xena","Rene","Peter","Diana","Rihanna"};

      String b  [] = {"Madona","Diddy","Joe","Alice"};

    ArrayList list = new ArrayList() ;

    list.add(a);

    list.add(b);

  Collections.sort( list, new Comparator()
                                                                       {

        @Override
                 public int compare( Object a, Object b )

                                                                               {

                    return( (String)a ).compareToIgnoreCase( (String) b );

                       }

                 } 




                 It doesnot work

Does it compile? If not post the error messages.

Where do you print out the before list and the after list?

Why so many blank lines? That makes the code very hard to read.

You are continually ignoring our requests to format the code that you post so that is is readable.
You also ignore our coding suggestions like printing the list before and after the sort.
Can you explain your actions?

Why should anyone try to help you if you continue to ignore what is recommended???

Edited 4 Years Ago by NormR1

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package exercise1;

                import java.util.*; 


                /**
                 *
                 * @author joseph
                 */
                    public class exercise {

                    /**
                     * @param args the command line arguments
                     */

                    public static void main(String[] args) {


                         String a [] = { "Joseph","Ariana","Xena","Rene","Peter","Diana",
                "Rihanna"};
                        String b  [] = {"Madona","Diddy","Joe","Alice"};

                        ArrayList list = new ArrayList() ;
                        list.add(a);
                        list.add(b);

                      Collections.sort( list, new Comparator()
                                                                     {
                            @Override
                                     public int compare( Object a, Object b )
                                                                                    {
                                     return( (String)a ).compareToIgnoreCase( (String) b );

                             }


                });


                        the error compiler is here:




                                                        run:
                       Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
                                at opdracht1.Opdracht1$1.compare(Opdracht1.java:36)
                                at java.util.TimSort.countRunAndMakeAscending(TimSort.java:324)
                                at java.util.TimSort.sort(TimSort.java:189)
                                at java.util.TimSort.sort(TimSort.java:173)
                                at java.util.Arrays.sort(Arrays.java:659)
                                at java.util.Collections.sort(Collections.java:217)
                                at opdracht1.Opdracht1.main(Opdracht1.java:31)
                            Java Result: 1
                            BUILD SUCCESSFUL (total time: 1 second)

Edited 4 Years Ago by huphane: Modified

[Ljava.lang.String; cannot be cast to java.lang.String

You have not posted the line with the error. What is on line 36?
The error says that you are tring to cast an object that is an array of String to a String.

If you would print out the contents of list before the sort you might see what was in list.

Edited 4 Years Ago by NormR1

I got the solution. So if anybody else needs it, it is here:

                 /*
             * To change this template, choose Tools | Templates
             * and open the template in the editor.
             */
            package ExerCise;




            /**
             *
             * @author Huphane
             */
            public class Exrcise           {

                /**
                 * @param args the command line arguments
                 */

                public static void main(String[] args)     {

                    String[] A = { "Joseph","Ariana","Xena","Rene","Peter","Diana" };
                     String[] B = { "Madona","Diddy","Joe","Alice","Brian","Sara" };
                    String[]  C = new String[A.length + B.length];


                   System.arraycopy(A, 0,  C, 0, A.length);
                   System.arraycopy(B, 0,  C,  A.length, B.length);

                for (int i = 0; i < C.length; i++)  {
                    for(int j = i + 1 ; j <  C.length; j++) {
                        if( C[j].compareToIgnoreCase( C[i]) < 0)          {

                            String K =  C[j];
                            C[j] =  C[i];
                            C[i] = K ;
                            }

                    }
                  System.out.println(  C[i]);



              }    
          }
       }
Hi Guys, I have solved my earlier question, but i am trying to do it in another way and i am stuck:
I need to compare two sorted string arrays to arranged in alphabetical order without sorting them but using compareTo method or overloading. just to get the whole list arranged. first comparing Ariana with Alice if Ariana > is than Alice print Alice, then comparing Ariana and Brian if Ariana < is than Briana print Brian and than comparing Brian and Diana so on.        

  package opdarcht1;

  public class Opdarcht1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String[] A = { "Ariana","Diana","Joseph","Peter","Rene","Xena" };
    String[] B = { "Alice","Brian","Diddy","Joe","Madonna","Sara" };


                  for (int i = 0; i < A.length; i++)      {
                     for(int j = 0 ; j <  B.length; j++)      {
                         if( A[i] > B[j])                          {
                         System.outprintln(B[j]);
                         }
                         alse
                         System.out.println(A[i]);


                      }


              }


     }
This question has already been answered. Start a new discussion instead.