Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Sort String on CMD line w/o regards to case

Posted on 2004-11-06
22
Medium Priority
?
270 Views
Last Modified: 2010-03-31
The program should sort Strings on the command line without regard to case and print the following:

java Part4 Add a little SUGAR to your tea

will print

      a
      Add
      little
      SUGAR
      tea
      to
      your

I got it to sort but did not user the "ignoreUpperCase" method, can someone please assist on implementing the ignoreUpperCase method.

<code>

import java.util.*;
public class Part4
{
  public static void main(String args[])
  {
  List myList = new ArrayList(args.length);
  for (int i = 0; i < args.length; i++)
  {
   //System.out.println(i + ": " + args[i]);
   Arrays.sort(args);

  }
  for (int i = 0; i < args.length; i++)
   System.out.println(args[i]);

  }
  //public int compareTo(Object o)
  //{
 // return 0;
 //}
}
</code>

Thanks
0
Comment
Question by:Coconut77840
  • 9
  • 5
  • 4
  • +2
22 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 12513821
You can use the method  String.compareToIgnoreCase(String str)
Refer to the JAVADOC for String.
<*>
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12513837
Vector ignoredCasesValues = new Vector();
  for (int i = 0; i < args.length; i++)
  {
     String ignoredValue = args[i].toString(0.toLowerCase();
   //System.out.println(i + ": " + args[i]);
  }
    Arrays.sort(ignoredCasesValues);
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12513842
it will be:

      Arrars.sort(args, new Comparator(Object a, Object b)
                                      {
                                             String sa = (String a);
                                             String sb = (String b);
                                             return(sa.compareToIgnoreCase(sb));
                                      }
                      );

<*>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 24

Expert Comment

by:sciuriware
ID: 12513843
Just replace the old sort with mine.
<*>
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 100 total points
ID: 12513847
Sorry I forgot something:

      Arrars.sort(args, new Comparator()
                                      {
                                          public int compare(Object a, Object b)
                                           {
                                             String sa = (String a);
                                             String sb = (String b);
                                             return(sa.compareToIgnoreCase(sb));
                                           }
                                      }
                      );

<*>
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12513854
That should work.
Don't get confused by 'compare()'  <---->   'compareIoIgnoreCase()'
the latter can be anything.
<*>
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12513861
sorry little correction:

Vector ignoredCasesValues = new Vector();
  for (int i = 0; i < args.length; i++)
  {
     String ignoredValue = args[i].toString(0.toLowerCase();
   //System.out.println(i + ": " + args[i]);
    ignoredCasesValues.add(ignoredValue);
  }
    Arrays.sort(ignoredCasesValues);

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12513871
petmagdy, your code destroys the strings.
The question was to sort, not to alter the case!
<*>
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12513888
no sciuriware i keep the original as is in args[i]
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12513892
oopps u r right sciuriware
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12513906
So you end up with 'args[]' that's not sorted and 'ignoredCasesValues' that is
sorted but lost uppercases.
What's the deal?
<*>
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 12513986
Arrays.sort(args, String.CASE_INSENSITIVE_ORDER);
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12514005
Interesting, but this is not in the JAVADOC of 1.4 nor 1.5
<*>
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12514033
Indeed, I was looking at the class Arrays.

Well, Coconut77840, 2 good answers to your 125 points question.

You decide .........................................


<*>
0
 
LVL 92

Expert Comment

by:objects
ID: 12515752
>  for (int i = 0; i < args.length; i++)
>  {
>   //System.out.println(i + ": " + args[i]);
>   Arrays.sort(args);
>
>  }

you only need to call sort once, not once for every arg
the above can be replaced with

Arrays.sort(args, String.CASE_INSENSITIVE_ORDER);

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12517091
>>
the above can be replaced with

Arrays.sort(args, String.CASE_INSENSITIVE_ORDER);
>>

Please do not repeat comments that have already been made



0
 

Author Comment

by:Coconut77840
ID: 12517944
Would I have to add that Arrays.sort statement at the end of my current code?

Thanks guys
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12518507
After the first loop
0
 

Author Comment

by:Coconut77840
ID: 12518745
That made the trick.

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12518810
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12519059
> Please do not repeat comments that have already been made

Your comment was missing bits
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question