File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String Sorting error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Sorting error" Watch "String Sorting error" New topic
Author

String Sorting error

Siddhesh Desai
Greenhorn

Joined: Jun 28, 2004
Posts: 9
import java.util.*;
import java.io.*;
class TestString
{
public static void main(String args[])
throws IOException
{
String str[]= new String[10];
//int array[]=new int[10];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<5;i++)
str[i]=br.readLine();
for (int i=0; i<str.length-1; i++) {
for (int j=i+1; j<str.length; j++) {
if (str[i].compareTo(str[j]) > 0) {
String temp=str[j]; str[j]=str[i]; str[i]=temp;
}
}
}
}
}
Program complies but following Run Time errors occors
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.compareTo(String.java:1004)
at TestString.main(TestString.java:17)
--------------------------------------------------
please help me
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
change this
String str[]= new String[10];

to this
String str[]= new String[5];

or change this
for(int i=0;i<5;i++) str[i]=br.readLine();

to this
for(int i=0;i<10;i++) str[i]=br.readLine();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Sorting error
 
Similar Threads
output is not coming please help
io Problem ..moving the files one directory to another
Reading Data from ASCII text file and inserting in database
sorting string with out using predefined java methods
splitting String into substrings