Arrays.sort() not working.

import java.io.*;
import java.util.Arrays;

class PHONELST{

public static void main(String ar[])throws Exception{

DataInputStream s= new DataInputStream(System.in);

    int i=0,t=0,n=0,flag=0;
//int arr[]=new int[10000];
    String str[]=new String[10000];

    t=Integer.parseInt(s.readLine());

    while(t!=0){
        n=Integer.parseInt(s.readLine());
        for(i=0;i<n;i++)
            str[i]=s.readLine();
          

     Arrays.sort(str);

        for(i=0;i<n-1;i++){
            if(str[i+1].startsWith(str[i])){
                flag=1;
                System.out.println("NO");
                break;
            }
        }
        if(flag==0)
            System.out.println("YES");
        flag=0;
    t--;

    }
   
}

}

Use Arrays.sort(str,0,n) : Since all the elements might not be present in “str” array , the Arrays.sort(str) will try to compare “null” elements and get an exception .

2 Likes

java is … : http://www.crosstalkonline.org/storage/issue-archives/2008/200801/200801-Dewar.pdf

Java, evil ? Lol

1st of all, the title about the part that talks most about why java is “evil” is: “The Pitfalls of Java as a First Programming Language”, it mostly says that for beginners is easier to switch from lower level languages like C or C++ to Java instead of doing it the other way around.
2nd they have another part titled “Why Java Matters”, you should read that as well.
3rd every language has its advantages and disadvantages and I do believe they did not do any justice to Java in that article. Finally as a programmer you should keep your mind open and be VERY careful with that kind of statement.

2 Likes

@junior ok.