Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 String Sorting HELP
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xxbow-pkerxx




PostPosted: Tue Jan 02, 2007 7:36 pm   Post subject: String Sorting HELP

Im a newer java programmer and i need major help the problem is that i keep getting an error (using drjava)

the error

NullPointerException:
at java.lang.String.compareTo(Unknown Source)
at insert1_1000.insertSort(insert1_1000.java:53)
at insert1_1000.main(insert1_1000.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

the code:
code:


import java.io.*;

class insert1_1000 {
  public static void main (String [] args){
   
    FileReader fileIn;
    BufferedReader buffer;
   
   
    int size=6000; // array size
    String [] array = new String [size]; // array
    int time;
   
    try{ // read file in to array
      fileIn = new FileReader ("words1.txt");
      buffer = new BufferedReader (fileIn);
     
      for (int i=0; i < size; i++){
     
        array [i]= buffer.readLine ();
         fileIn.close ();
      }
     
    }catch (IOException err){};
   
 
    time=insertSort (array); // send array in to methord
   
    System.out.println ("it took "+time);

   
  }// end main
 
  public static int insertSort (String [] array) {
    String temp;//creates a variable to store the number to be sorted
    int current=0;//creates a variable to store the current element being sorted in the array
    int size = 1000;
   
   
     long startTime = System.currentTimeMillis ();// start timer
     
    for (int i=0; i<size; i++) {
     
      current=i;//makes current the element of the number to be sorted
      temp=array[i];//stores the nubmer to be sorted
      //Loop will continue until it reaches 1 or the spot is found within the array
     
      while (current>0 && array[current-1].compareTo (temp) > 0 ) {
        array[current]=array[current-1];//shifts the next number to current spot
        current--;//moves to next current
      }
      array[current]=temp;//puts the number to be sorted into its element
    }
   
    int elapsedTime= (int)(System.currentTimeMillis()-startTime);
   
    return elapsedTime;
   
  }
 
}// end class
[/code]
Sponsor
Sponsor
Sponsor
sponsor
xxbow-pkerxx




PostPosted: Tue Jan 02, 2007 7:38 pm   Post subject: please reply

please reply to me here or via e-mail

Taha1234567890@hotmail.com
McKenzie




PostPosted: Wed Jan 03, 2007 9:15 am   Post subject: (No subject)

Attach the data file. I tried putting empty strings in the array and it stops it from crashing. I'd guess you don't have 6000 lines in your file, or the file is not in the same folder.

I find the placement of
code:
         fileIn.close ();

odd, but It won't cause your problem.
xxbow-pkerxx




PostPosted: Wed Jan 03, 2007 2:39 pm   Post subject: (No subject)

heres the data files

if testing 3 of them



words1.txt
 Description:

Download
 Filename:  words1.txt
 Filesize:  62.46 KB
 Downloaded:  612 Time(s)

HellblazerX




PostPosted: Wed Jan 03, 2007 8:12 pm   Post subject: (No subject)

My guess would be in this line:
code:
while (current>0 && array[current-1].compareTo (temp) > 0 ) {

Since you started with current being 0, current - 1 will give you -1, so you'll be comparing with something that doesn't exist. I think Java will check all conditions inside the parentheses, regardless of whether the first condition is true or not.
McKenzie




PostPosted: Thu Jan 04, 2007 9:14 pm   Post subject: (No subject)

Oops,

The FileIn.close() is causing the problem. Your program will read in the first 767 lines then die with it there. Just place it after your for loop. I haven't even looked at your sort, so it may or may not work.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: