user3483494
user3483494

Reputation:

Remove all null array values before sorting

I have been getting some errors. My array looks like this:

 String words[] = new String[50000];

The user is asked to input values into the array

words[i]=c.readLine();

The program will exit the infinite for loop once duplicated values are inputed. Now the program must sort the array in alphabetical order, yet it is trying to sort some of the null values and returns errors.

Arrays.sort(words, String.CASE_INSENSITIVE_ORDER);         
for (int a = 0; a < words.length; a++) {
    c.println(words[a]);
}

This program will work only if the user inputs exactly 50000 values. I am unable to guess how many values the user will input, how do I solve this?

I am thinking I have to somehow remove all null values before alphabetically sorting. Any ideas?

Upvotes: 1

Views: 1428

Answers (5)

Marcinek
Marcinek

Reputation: 2117

You could count the inputed Values and Sort only the non null values using

Arrays.sort(words, 0,inputCount, String.CASE_INSENSITIVE_ORDER);

where inputCount is the count of the encapsulating endless while loop.


You could also modify your for-loop

for (int a = 0; a < inputCount; a++) {

Upvotes: 0

Baby
Baby

Reputation: 5092

use ArrayList if you are not sure of the size. don't hardcode it.

But if you insist to use array, what you can try:

int notNull=words.length;    
for(int i= 0;i<words.length;i++){
    if(words[i]==null){
        notNull=i;
    break;
}
}
String[] newWords=Arrays.copyOfRange(words, 0, notNull);

newWords array will be your new array without null values

Upvotes: 1

Makoto
Makoto

Reputation: 106410

Instead of worrying about the null values in your set, think of it like this:

  • If you're comparing against one non-null value and one null value, the non-null value wins.
  • If you're comparing against two non-null values, neither wins.

You can create a custom Comparator<String>, and use it in Arrays#sort. As an aside, String.CASE_INSENSITIVE_ORDER is also a Comparator<String>, but it doesn't quite fill the need that you seem to have.

Here's an example implementation:

public class NullAwareStringComparator implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        if (o1 == null && o2 == null) {
            return 2;
        } else if (o1 == null) {
            return 1;
        } else if (o2 == null) {
            return -1;
        } else {
            return o1.compareToIgnoreCase(o2);
        }
    }
}

You would then call your sort method with it as such:

Arrays.sort(words, new NullAwareStringComparator());

This puts all of the null values at the end of the array. If you want the reverse ordering, flip the sign of the numbers returned in the compare method.

Alternatively, if you don't know/care how many elements you can hold, consider using a TreeSet<String> instead - this has the added benefit of ignoring duplicate entries.

Set<String> sortedStrings = new TreeSet<>(new NullAwareStringComparator());

You could also use String.CASE_INSENSITIVE_ORDER as the comparator instead, since at this point, you're not nearly as worried about null entries as you previously were:

Set<String> sortedStrings = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);

...and to sort the collection, you would call Collections.sort().

Upvotes: 0

isak gilbert
isak gilbert

Reputation: 2611

You could use a java.util.Set to hold your words. That way you don't have to deal with initializing or extending the array.

Upvotes: 0

Tawnos
Tawnos

Reputation: 1887

You could use an ArrayList or a Set (or any other dynamically-sizing collection) to track how many strings are actually input.

If you want to stick with a pre-created array, you could count how many lines are actually read and only loop up to that amount.

You could use a binary insertion sort when the strings are being input. This will be a faster search for duplicates and will give you an ordered list ready to be output.

There are a lot of options, but this doesn't seem to be a good one. Since you have 50000 spots, what happens if the user tries to enter one more than that?

Upvotes: 0

Related Questions