import java.io.*;
import java.util.Arrays;
public class r
{
 public static void main(String args[])
 {
  int n;
  String[] names =new String[30];
  InputStreamReader isr=new InputStreamReader(System.in);
  BufferedReader br=new BufferedReader(isr);
 try
  {
System.out.print("enter limit:");
n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
names[i]=br.readLine();
Arrays.sort(names);
System.out.println(Arrays.toString(names));
  }catch(IOException e){}
    
 }
}

Edited 5 Years Ago by Jessurider: n/a

If you can't even be bothered to post the exception, then why should anyone waste their time trying to figure out what's wrong with this for you?

this is the exception

C:\Program Files\IBM\Java50\bin>java r
enter limit:2
nid
ani
Exception in thread "main" java.lang.NullPointerException
        at java.lang.String.compareTo(String.java:438)
        at java.lang.String.compareTo(String.java:28)
        at java.util.Arrays.mergeSort(Arrays.java:1171)
        at java.util.Arrays.mergeSort(Arrays.java:1182)
        at java.util.Arrays.mergeSort(Arrays.java:1182)
        at java.util.Arrays.mergeSort(Arrays.java:1182)
        at java.util.Arrays.sort(Arrays.java:1095)
        at r.main(r.java:40)

Edited 5 Years Ago by Jessurider: n/a

Check my post, i already solved your matter. At least i deserve a positive point. ;-)

you mean like this

for(i=0;i<n;i++)
   {
   names[i] = new String();
   names[i]=br.readLine();
   }

i 've given u a +ve point

import java.io.*;
import java.util.Arrays;
public class r
{
public static void main(String args[])
{
String st[]=new String[30];
int n,k;
String[] names =new String[30];
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
try
{
System.out.print("enter limit:");
n=Integer.parseInt(br.readLine());
for(int i=0;i<30;i++)
	names[i] = new String();
for(int i=0;i<n;i++)
{
names[i]=br.readLine();
}
Arrays.sort(names);
System.out.println(Arrays.toString(names));
}catch(IOException e){}
 
}

Edited 5 Years Ago by Majestics: n/a

now the ans came like this

C:\Program Files\IBM\Java50\bin>javac r1.java

C:\Program Files\IBM\Java50\bin>java r1
enter limit:2
manu
ani
[, , , , , , , , , , , , , , , , , , , , , , , , , , , , ani, manu]

Can u please post what output u are expecting.... If you talking about the error , it is already solved...

Can u please post what output u are expecting.... If you talking about the error , it is already solved...

ya error is solved

but the output what i expect is this

C:\Program Files\IBM\Java50\bin>javac r1.java

C:\Program Files\IBM\Java50\bin>java r1
enter limit:2
manu
ani

ani
manu
This article has been dead for over six months. Start a new discussion instead.