1 2 3 Previous Next 40 Replies Latest reply: Nov 10, 2008 8:11 PM by 807589 RSS

    set String [] array from file.dat

    807589
      Alright, what I'm trying to do here is load in a .dat file with words, convert those words to strings that will be saved within an array of strings, then use a bubble sort to arrange the array alphebatically using compareTo(), and then finally a function that can prompt the user for a word and see if its within th array.
      Heres my Code so far:




      import javax.swing.JOptionPane; 
      import java.io.*;
         
          public class FileIO{
          
             public static void main(String[]args)throws IOException{
                 
               int ct = 0;
                 
               String [] arr = new String [20];
            
                      
               BufferedReader inFile =
                  new BufferedReader(new FileReader("words1.dat"));
                 
                           
               setArray(arr,inFile);
            
               for(int i = 0; i < arr.length; i++)
                  System.out.println(arr);

      testArray(arr);

      arrangeArray(arr);

      String input = inFile.readLine();
           
      for(int i = 0; i < arr.length; i++)
      System.out.println(arr[i]);
           
           
      inFile.close();
      }//main

      public static int setArray(String [] arr,BufferedReader inFile)throws IOException{

      int ct = 0;
      String inputLine = inputLine(inFile);

      while(inputLine != null){
      arr[ct] = inputLine;

      ct++;
      inputLine = inputLine(inFile);
      }//while
      return ct;
      }//setArray

      public static String inputLine(BufferedReader inFile)throws IOException{
      String inputLine = inFile.readLine();
      return inputLine;
      }//inputLine

      public static void arrangeArray(String [] arr){
      for(int j = 0; j < arr.length; j++) {
      for(int i = j + 1; i < arr.length; i++) {
      if(arr[i].compareTo(arr[j]) < 0) {
      String t = arr[j];
      arr[j] = arr[i];
      arr[i] = t;
      }//if
      }//for1
      System.out.println(arr[j]);
      }//for2
      }//arrangeArray


      public static void testArray(String [] arr){
      boolean answer = false;

      String inputStr = JOptionPane.showInputDialog(null,
                          "Please quire file for word,\nInput Word to query.");
      for(int i = 0; i < arr.length; i++){
      if(arr[i].equals(inputStr))
      answer = true;
      System.out.println(inputStr + " is present");                    
      }//for     
      if(answer == true)
      System.out.println(inputStr + " is in the file");
      else
      System.out.println(inputStr + " is not in the file");

      inputStr = JOptionPane.showInputDialog(null,
                          "Please quire file for word,\nInput Word to query.");                                                  

      }//testArray
      }//class
      Edited by: halfpipehippie on Nov 10, 2008 5:48 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: set String [] array from file.dat
          807589
          You haven't asked any question.
          • 2. Re: set String [] array from file.dat
            807589
            "fix it for me" is implied.
            • 3. Re: set String [] array from file.dat
              807589
              Sorry, question is that I cant seem to get this to work, I am running into an exception on my alphabeticalize function at the compareTo(), I feel that it should work though, the array is set and the function call should work. sorry! thanks for the help
              • 4. Re: set String [] array from file.dat
                807589
                Nope! Sorry that is a statement and not a question. See this funny curly thing at the end? That is called a question mark and it follows a question.
                • 5. Re: set String [] array from file.dat
                  807589
                  also I note that I'm missing the

                       
                  PrintWriter outFile =
                                 new PrintWriter(new FileWriter(outputFile));
                  and some other, but I've been spending much time on breaking it down into section to see where it goes wrong. I can get the arrangeArray function to work from this:

                  // A bubble sort for Strings. 
                      class SortString { 
                        static String arr[] = { 
                           "Now", "is", "the", "time", "for", "all", "good", "men", 
                           "to", "come", "to", "the", "aid", "of", "their", "country" 
                           };
                     
                         public static void main(String args[]) { 
                           for(int j = 0; j < arr.length; j++) { 
                              for(int i = j + 1; i < arr.length; i++) { 
                                 if(arr.compareTo(arr[j]) < 0) { 
                  String t = arr[j];
                  arr[j] = arr[i];
                  arr[i] = t;
                  }//if
                  }//for2
                  System.out.println(arr[j]);
                  }//for1
                  }//main
                  }//class
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                  • 6. Re: set String [] array from file.dat
                    807589
                    Oh goody! More statements and more code but still no question.
                    • 7. Re: set String [] array from file.dat
                      807589
                      my question is how come i keep getting an acception in my program when i have myarray set and i have a function that should run this test?

                      happy now?

                      theres two ? in that post
                      • 8. Re: set String [] array from file.dat
                        807589
                        flounder wrote:
                        Oh goody! More statements and more code but still no question.
                        Maybe we should look for a question within the code? Though a +?+ in code doesn't really ask a question, it's part of a ternary operator... What should we do?
                        • 9. Re: set String [] array from file.dat
                          807589
                          halfpipehippie wrote:
                          theres two ? in that post
                          Actually there were 3.
                          • 10. Re: set String [] array from file.dat
                            807589
                            Question: why would i get an exception within my arrangeArray(arr) function call on the line containing the compareTo() statement?
                            • 11. Re: set String [] array from file.dat
                              807589
                              halfpipehippie wrote:
                              my question is how come i keep getting an acception in my program when i have myarray set and i have a function that should run this test?
                              It's Exception and what type did you get? Please copy and paste the full stack trace.
                              • 12. Re: set String [] array from file.dat
                                807589
                                I would really like some help here, if you cant help me I can look somewhere else for help.

                                Thanks
                                • 13. Re: set String [] array from file.dat
                                  807589
                                  I think this might be what your asking for, also I know my testArray function doesnt work right.













                                  ----jGRASP exec: java FileIO

                                  array
                                  runtime
                                  program
                                  pointer
                                  variable
                                  size
                                  indirection
                                  dereference
                                  argument
                                  allocation
                                  function
                                  style
                                  dynamic
                                  address
                                  element
                                  null
                                  null
                                  null
                                  null
                                  null
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  element is present
                                  Exception in thread "main" java.lang.NullPointerException
                                       at FileIO.testArray(FileIO.java:74)
                                       at FileIO.main(FileIO.java:22)

                                  ----jGRASP wedge2: exit code for process is 1.
                                  ----jGRASP: operation complete.
                                  • 14. Re: set String [] array from file.dat
                                    807589
                                    And we are trying to help you but if you can't be bothered in providing details that will help us track down your problem, why should we be bothered trying to help?
                                    1 2 3 Previous Next