Reputation: 27
I have two string arrays in ar1
and ar2
, and I am reading the input from file and storing in arrays , ar1
contains
Cat
Lam
Orange
Kam
Ramveer
None
Tue
Apple
ar2 contains
Dog
elephant
Kam
Monday
Parrot
Queen
Ramveer
Tuesday
Xmas
I am trying to sort the arrays in alphabetical order, and i am using Array.sort()
, but getting the exception
Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.binarySort(ComparableTimSort.java:232)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:176)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at CompareArrays.pr1(CompareArrays.java:51)
at CompareArrays.main(CompareArrays.java:86)
Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)
Code
File file1= new File("C:\\Users\\Ramveer\\Desktop\\updates\\f1.txt");
File file2=new File("C:\\Users\\Ramveer\\Desktop\\updates\\f2.txt");
Scanner sc1=new Scanner(file1);
Scanner sc2=new Scanner(file2);
while(sc1.hasNextLine()){
ar1[c1]=sc1.nextLine();
c1++;
}
while(sc2.hasNextLine()){
ar2[c2]=sc2.nextLine();
c2++;
}
Arrays.sort(ar1);
for(int k=0;k<c1;k++){
System.out.println(ar1[k]);}
}
Any help would be great. Thanks!
Upvotes: 0
Views: 6162
Reputation: 10969
Use an Arraylist as then you dont have to estimate the size of your array as the ArrayList grows dynamically as you add more strings, your code would go something like this
File file1= new File("C:\\Users\\Ramveer\\Desktop\\updates\\f1.txt");
File file2=new File("C:\\Users\\Ramveer\\Desktop\\updates\\f2.txt");
Scanner sc1=new Scanner(file1);
Scanner sc2=new Scanner(file2);
List<String> list1 = new ArrayList<String>()
List<String> list2 = new ArrayList<String>()
while(sc1.hasNextLine())
list1.add(sc1.nextLine().toLowerCase()); //edited -- bad approach but would work if case not important
while(sc2.hasNextLine()){
list2.add(sc2.nextLine().toLowerCase()); //edited -- bad approach but would work if case not important
Collections.sort(list1);
Collections.sort(list2);
for(String s: list1)
System.out.println(s);
Or you could do this to implement a case insensitive sort, which would be better then altering the string as you add it to array
Collections.sort(list1, new Comparator<Object>()
{
@Override
public int compare(Object o1, Object o2)
{
String s1 = (String) o1;
String s2 = (String) o2;
return s1.compareToIgnoreCase(s2);
}
}
Then repeat for list2. But an even better way would be to write a new comparator method as such
public class SortIgnoreCase implements Comparator<Object> {
public int compare(Object o1, Object o2) {
String s1 = (String) o1;
String s2 = (String) o2;
return s1.compareToIgnoreCase(s2);
}
}
then call Collections.sort(list1, new SortIgnoreCase());
which is a cleaner way to write the code to sort multiple lists
Upvotes: 1
Reputation: 35597
If your Array don't have null values Arrays.sort()
works properly.
So it is better to use List
to avoid that kind of a scenario.
Then you can convert your arrays to ArrayList
and use Collections.sort()
to sort them.
String[] str1 = {"Cat","Lam","Orange","Kam","Ramveer","None","Tue","Apple"};
String[] str2 = {"Dog","Elephant","Kam","Monday","Parrot","Queen","Ramveer","Tuesday","Xmas"};
List<String> lst1=Arrays.asList(str1);
List<String> lst2=Arrays.asList(str2);
Collections.sort(lst1);
Collections.sort(lst2);
System.out.println(lst1+"\n"+lst2);
Upvotes: 0
Reputation: 8842
If you really want to use arrays then use this for reading:
List<String> list1 = new ArrayList();
while (sc1.hasNextLine()) {
list1.add(sc1.nextLine());
}
String[] ar1 = list1.toArray(new String[list1.size()]);
If you can use List collection instead then:
List<String> list1 = new ArrayList();
while (sc1.hasNextLine()) {
list1.add(sc1.nextLine());
}
Collections.sort(list1);
Upvotes: 0
Reputation: 425428
If your arrays are too big for the data, you'll have the default value of null
in the unused elements; these null
values will cause your exception.
Before loading and sorting your arrays, put blanks in all elements:
Arrays.fill(a1, "");
Arrays.fill(a2, "");
Upvotes: 1
Reputation: 200296
Since you are using arrays, you must predict the number of entries in advance. It seems like your prediction is off, so some array elements stay null
.
Please consider using ArrayList
instead of a raw array. Sorting is done with Collections.sort
.
Upvotes: 8