1 2 3 Previous Next 32 Replies Latest reply: Jan 12, 2009 12:19 PM by 807589 RSS

    String Tokenizer Problem?

    807589
      Hello. I am doing a school assignment and I need a bit of help. I have to create a code that will take a text file, import it, tokenize it, and modify the strings that it recieves. I understand that bit, but I'm having trouble tokenizing the strings. I know the token method is obsolete, but this is the method the teacher requested. Following is the bit of code I'm having trouble with:
           //Seperating the tokens
           wordOne=String.parseStr(strTkn.nextToken());     
           suffixOne=String.parseStr(strTkn.nextToken());     
           wordTwo=String.parseStr(strTkn.nextToken());     
           suffixTwo=String.parseStr(strTkn.nextToken());     
           wordThree=String.parseStr(strTkn.nextToken());
           suffixThree=String.parseStr(strTkn.nextToken());
           }
      The error I am getting is:
      cannot find symbol method parseStr(java.lang.String)
        • 1. Re: String Tokenizer Problem?
          807589
          reidysj wrote:
          cannot find symbol method parseStr(java.lang.String)
          There is no such method "parseStr" available in the [String class|http://java.sun.com/javase/6/docs/api/java/lang/String.html]. Why are you trying to use it (note that nextToken() returns a String reference)?

          ~
          • 2. Re: String Tokenizer Problem?
            800313
            post an example of the string you are trying to split up and also what are the delimeters you are using for examples splitting it up by spaces/commas etc

            Calypso
            • 3. Re: String Tokenizer Problem?
              807589
              Well, the teacher had used the supposed parseStr method in a previous program. My program actually follows very closely to hers. Below I have pasted my entire code for the project (not finished yet, but sufficient enough to give a certain output.)
              import java.io.*;
              import java.util.StringTokenizer;
              public class BigVowels
              {
                   //File Input Variables: DO NOT CHANGE
                   private static FileInputStream inFile;
                   private static InputStreamReader inReader;
                   private static BufferedReader reader;
                   
                   //String Tokenizer Variable Used to Seperate 
                   //Line into different data elements:
                   private static StringTokenizer strTkn;
                   
                   //Any Program Variables Go Below:
                   private static String wordOne , wordTwo , wordThree , suffixOne , suffixTwo , suffixThree , line , word;
                   
                   //Throws IOException 
                   public static void main (String args[]) throws IOException
                   {
                        //These are the methods in which to throw IOException
                        initFile();
                        getData();
                        //TemporaryBlock calcTotal(); //This is subject to change.
                        printResults();
                        
                        //Close data file
                        inFile.close();
                   }
                   
                   //Prepare File for input
                   public static void initFile() throws IOException
                   {
                        inFile=new FileInputStream ("H:\\!!Java Download\\VowelsTest");
                        inReader= new InputStreamReader(inFile);
                        reader=new BufferedReader (inReader);
                   }
                   public static void getData() throws IOException
                   {
                        line=reader.readLine();
                        strTkn=new StringTokenizer(line);
                        word=strTkn.nextToken( );
                   //Seperating the tokens
                   wordOne=String.parseStr(strTkn.nextToken());     
                   suffixOne=String.parseStr(strTkn.nextToken());     
                   wordTwo=String.parseStr(strTkn.nextToken());     
                   suffixTwo=String.parseStr(strTkn.nextToken());     
                   wordThree=String.parseStr(strTkn.nextToken());
                   suffixThree=String.parseStr(strTkn.nextToken());
                   }
                   public static void printResults()
                   {
                        System.out.println(wordOne);
                        System.out.println(suffixOne);
                        System.out.println(wordThree);
                        System.out.println(suffixThree);
                   }
              }
                   
              I don't understand why it worked in the teacher's sample code but not in my code.

              Edited by: reidysj on Jan 8, 2009 7:24 AM
              • 4. Re: String Tokenizer Problem?
                807589
                reidysj wrote:
                Well, the teacher had used the supposed parseStr method in a previous program.
                Show us the teacher's code. I believe that your teacher called this method on something, but I sincerely doubt that it was called statically on the String class. You have to be precise in programming as it is not forgiving if you're not.
                • 5. Re: String Tokenizer Problem?
                  807589
                  The portion of the teachers code that uses the parse, does not, in fact, use strings. Is that where I am going wrong? The code uses integers and doubles:
                   // extracting the parts of the line into separate int and double variables
                      grade = Integer.parseInt(strTkn.nextToken());
                      avg = Double.parseDouble(strTkn.nextToken());
                      num1 = Integer.parseInt(strTkn.nextToken());
                      num2 = Integer.parseInt(strTkn.nextToken());
                      num3 = Integer.parseInt(strTkn.nextToken());
                    }  
                  • 6. Re: String Tokenizer Problem?
                    807589
                    I don't understand why it worked in the teacher's sample code but not in my code.
                    You can only use methods that are defined and accessible for the String class. Your teacher's code may be using a String class other than java.lang.String, but that's a dangerous practice. You may also have mistranslated the code. Either way, you cannot use the parseStr() method as a static method of java.lang.String because no such method exists.

                    The important question here is that you understand what it is you want to do. So, what exactly are you hoping to accomplish with parsing the tokens? If you can answer that question, we can assist you. But if you don't yet know what your requirements are, that's a critical first step.
                    • 7. Re: String Tokenizer Problem?
                      807589
                      reidysj wrote:
                      The portion of the teachers code that uses the parse, does not, in fact, use strings. Is that where I am going wrong? The code uses integers and doubles:
                      Yikes! Yes, this is where you are going wrong: You can't just make up a method and hope and pray that it will work.
                      • 8. Re: String Tokenizer Problem?
                        807589
                        reidysj wrote:
                        The portion of the teachers code that uses the parse, does not, in fact, use strings. Is that where I am going wrong?
                        Yes. The nextToken() method returns a String reference. There is no need to parse it if all you need is the raw string.

                        ~
                        • 9. Re: String Tokenizer Problem?
                          807589
                          The outside document is in the following format:
                          word suffix
                          newWord newSuffix
                          anotherNewWord anotherNewSuffix

                          I need to assign each word a variable and each suffix a variable so that I can further modify them.
                          • 10. Re: String Tokenizer Problem?
                            807589
                            Encephalopathic wrote:
                            You can't just make up a method and hope and pray that it will work.
                            But that's the patented P-PAM™ programming methodology.

                            Projectile-Partition Adhesion Methodology (P-PAM)™; you know, throwing code up against the wall to see if it sticks. :o)

                            ~
                            • 11. Re: String Tokenizer Problem?
                              807589
                              reidysj wrote:
                              I need to assign each word a variable and each suffix a variable so that I can further modify them.
                              Okay. As mentioned, there is no need at this point to parse the results of nextToken(). Just assign that value to the variable; in other words, remove the erroneous call to "String.parseStr()".
                              • 12. Re: String Tokenizer Problem?
                                807589
                                Ok, so that fixed the initial problem. Thank you very much. Now I just have to figure out why it can't locate the file I am directing it to...
                                • 13. Re: String Tokenizer Problem?
                                  807589
                                  reidysj wrote:
                                  Now I just have to figure out why it can't locate the file I am directing it to...
                                  Ensure that the file actually exists and that your application has permission to access that file.

                                  ~
                                  • 14. Re: String Tokenizer Problem?
                                    807589
                                    Ok, I relocated the file to make sure I was typing in the destination correctly. I must not have been, because now when I run, the error is different:

                                    Exception in thread "main" java.util.NoSuchElementException
                                    at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
                                    at BigVowels.getData(BigVowels.java:44)
                                    at BigVowels.main(BigVowels.java:22)

                                    Process completed.
                                    1 2 3 Previous Next