1 2 Previous Next 17 Replies Latest reply: Jan 28, 2009 9:45 AM by 843785 RSS

    StringTokenizer and Sorting Methods

    843785
      Hello,

      Goal:

      1. Write a Class that reads a file using StringTokenizer
      2. Take the words and store in an object (initialized for example as "fileobject")
      3. This should then be passed to a Bubble, Selection and Insertion method. The object should be created 3x. Once for each sort method


      Here what I have so far but don't know if correct

      ***************************
      public class StringArrayApp {

           
           public static void main(String[] args) {
                
           //File read = new File("myfile.txt");
                FileReader read = new FileReader(myfile.txt);
      StringTokenizer st = new StringTokenizer(read);

      ***************************************

      Not very familiar with StringTokenizer.

      I am new to Java and any help will be greatly appreciated.

      Thanks
        • 1. Re: StringTokenizer and Sorting Methods
          699554
          Have you first looked at the java api for File, FileReader and StringTokenizer, if not i recommend doing so. Secondly there are great examples in the java api for StringTokenizer for you to understand what the class is capable of and the possible output.
          • 2. Re: StringTokenizer and Sorting Methods
            699554
            Heres a small example for you, i use a Scanner to read contents from my file, which means i dont need to use a StringTokenizer
            public static void main(String[] args){
              if(args.length == 0){
                System.out.println("File path required as command line argument");
              }
              File file = new File(args[0]);
              if(!file.exists()){
                System.out.println("File does not exist");
              }
                      
              try{
                Scanner sin = new Scanner(file);
                String temp;
                while(sin.hasNext()){
                  temp = sin.next();
                  System.out.println(temp);
                }
                sin.close();
              }
              catch(Exception e){               
            }
            Edited by: Melanie_Green on Jan 25, 2009 12:29 AM
            • 3. Re: StringTokenizer and Sorting Methods
              DarrylBurke
              Melanie_Green wrote:
              Heres a small example ...
              In your program, when you detect that no argument was passed from the command line, you print a message. And go ahead to construct a file with args[0], which will throw an ArrayIndexOutOfBoundsException.

              Again, if the file doesn't exist, you print another message. And then go ahead to process a file that doesn't exist.

              Shouldn't you be handling these either by taking console input (in a loop) or by returning from the method without attempting to process a file that doesn't exist?

              db
              • 4. Re: StringTokenizer and Sorting Methods
                699554
                sry i intended to return at both points, well spotted
                • 5. Re: StringTokenizer and Sorting Methods
                  843785
                  hi,

                  I have read it but still not completely sure on how to use it

                  any help would be appreciative.

                  thanks
                  • 6. Re: StringTokenizer and Sorting Methods
                    796311
                    How do you recognize a word?
                    Use those characters as your tokens
                    exclude them
                    process the StringTokenizer 'collection' of words.
                    • 7. Re: StringTokenizer and Sorting Methods
                      843785
                      Melanie,

                      This is great help. I have the text outputing the words in a tokenized fashion (space delimited) by using the Scanner class but I am having problems passing the variable "tempfile" to my three StringArray object reference variables due to "type msmatch". I guess this is because the StringArray class is expecting an int data type. I am attaching my StringArray.java which is a listing of 12 methods (it includes the 3 sort methods).

                      Goal:

                      1. pass the "tempfile" variable as an Array to three different objects since I have 3 different methods
                      2. ultimately once the variable is passed to the methods they should be sorted 3 ways using Bubble, Selection, Insertion sorts. I have no idea how to get it to work

                      I am praying for some help

                      Thanks

                      *********************************StringArray.java*******************************

                      import java.util.Scanner;

                      public class StringArray implements InterfaceStringArray {
                                
                                
                           
                                public void initialize(int arrSize) //initializes an array of size=arrSize
                                
                                {
                                     //method body
                                     
                                }
                                public int getSize() //returns the current array size
                                
                                {
                                     //method body
                                }
                                public void insert(String newItem) //inserts newItem in the next available spot
                                
                                {
                                     //method body
                                }
                                public boolean delete(String deleteItem) //returns true if all occurrences of deleteItem are deleted, //false if no matching items found
                                
                                {
                                     //method body
                                }
                                public int search(String searchItem) //returns the index of the first matching item, -1 if no match
                                
                                {
                                     //method body
                                }
                                public boolean setItem(int index, String itemValue) //returns true if successful, false otherwise
                                
                                {
                                     //method body
                                }
                                public String getItem(int index) //returns the item at the requested index
                                
                                {
                                     //method body
                                }
                                public boolean resize(int newSize) //returns true if successful (widening), false if narrowing
                                
                                {
                                     //method body
                                }
                                public void bubbleSort() //sorts the array strings using Bubble Sort Algorithm (ascending)
                                
                                {
                                     //method body
                                }
                                public void selectionSort() //sorts the array strings using Selection Sort Algorithm (ascending)
                                
                                {
                                     //method body
                                }
                                public void insertionSort() //sorts the array strings using Insertion Sort Algorithm (ascending)
                                
                                {
                                     //method body
                                }
                                public String getStringArray() //returns a String of all the array strings separated by a white space
                                
                                {
                                     //method body
                                }

                                
                                
                                
                      } //end class StringArray.java


                      *************************************************THE END************************************************************************

                      Here is my driver class file named "StringArrayApp.java

                      *****************************************StringArrayApp.java********************************

                      import java.io.*;
                      import java.util.*;


                      public class StringArrayApp {

                           
                           public static void main(String[] args) throws IOException
                           {
                                
                                //Declarations for file input
                                
                                Scanner sc = new Scanner(new File("c:\\java\\ScienceMagazineArticle.txt"));
                                String tempfile;
                                while(sc.hasNext()){
                                tempfile = sc.next();
                                System.out.println(tempfile);
                                }
                                sc.close();
                                
                                     StringArray[] bs; // defines three references to an array
                                     StringArray[] ss;
                                     StringArray[] is;
                                     
                                     bs = new StringArray(tempfile]; // creates the array, and
                                     ss = new StringArray[tempfile];     // sets references to the array
                                     is = new StringArray[tempfile];

                           }
                           

                      }

                      ********************************************THE END******************************************************************
                      • 8. Re: StringTokenizer and Sorting Methods
                        843785
                        wow! you should ask Melanie how she formats her code so nicely because her code is easy to read on the post...
                        regards
                        walker8
                        • 9. Re: StringTokenizer and Sorting Methods
                          800282
                          walker8 wrote:
                          wow! you should ask Melanie how she formats her code so nicely because her code is easy to read on the post...
                          regards
                          walker8
                          You don't know that? Well, you can do that by using the code-button.
                          ; )

                          Good to see an old-timer like yourself back on the forums!
                          • 10. Re: StringTokenizer and Sorting Methods
                            843785
                            hi,

                            I understand her code using the Scanner class to read the file contents. I am just looking for her on accomplishing my additional goals outlined in my post

                            if you can help I would appreciate it.
                            • 11. Re: StringTokenizer and Sorting Methods
                              843785
                              :) "code button"? what will they think of next?
                              regards
                              Walker8 <-- (misses teh water cooler)
                              • 12. Re: StringTokenizer and Sorting Methods
                                843785
                                Do you know how to accomplish what I am trying to do?
                                • 13. Re: StringTokenizer and Sorting Methods
                                  843785
                                  verbalh wrote:
                                  hi,

                                  I understand her code using the Scanner class to read the file contents. I am just looking for her on accomplishing my additional goals outlined in my post

                                  if you can help I would appreciate it.
                                  okay, i'm sorry. I looked at your code and all the methods are empty. kinda looks like an outline. where did you find it?
                                  • 14. Re: StringTokenizer and Sorting Methods
                                    843785
                                    I have the sort methods from a java book. I can send them to you if you'd like to see? It is a sample that only needs minor tweaking
                                    1 2 Previous Next