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){}
    
 }
}

Recommended Answers

All 12 Replies

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?

for(int i=0;i<30;i++)
names[i] = new String();

You cant use things before initialzing....

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)

i've put the exception ..............

No, you haven't.

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

No, you haven't.

sorry sir......

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){}
 
}

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

You forgot to post what the program currently outputs.

Can you post your current code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.