2 Replies Latest reply: Apr 19, 2007 7:28 PM by 807606 RSS

    Help with String Tokenizer Class.........

    807606
      public void readFile() throws IOException
           {     
                inputstream = new BufferedReader(new FileReader("C:\\CarInputData.txt"));
                
                int idx = 0; int tokenCount;
                String words[] = new String [500];
                StringTokenizer st;
                String line = inputstream.readLine();
                
                while (line!= null)
                     {               
                     st = new StringTokenizer(line,":");
                     tokenCount = st.countTokens();
                     System.out.println(st.nextToken());               
                     while (st.hasMoreTokens()) // make sure there is stuff to get
                {
                     words[idx] = st.nextToken();
                     idx++;
                }
                for (idx=0;idx<tokenCount; idx++)
                {
                     System.out.println(words[idx]);               
                     }                    
                line = inputstream.readLine();
                idx=0;
                     }     
           }

      Input file CarInputData.txt:

      Focus Wagon ZTW model:
      BasePrice-18,445:
      Color:c1.Fort Knox Gold Clearcoat Metallic:c2.Liquid Grey Clearcoat Metallic:c3.Infra-Red Clearcoat:c4.Grabber Green Clearcoat Metallic:c5.Sangria Red Clearcoat Metallic:c6.French Blue Clearcoat Metallic:c7.Twilight Blue Clearcoat Metallic:c8.CD Silver Clearcoat Metallic:c9.Pitch Black Clearcoat:c10.Cloud 9 White Clearcoat:
      Transmission:t1.Automatic-$0:t2.Manual-$-815:
      Brakes/TractionControl:b1.Standard-$0:b2.ABS-$425:b3.ABS with Advance Trac-$1625:
      Side Impact Air Bags:a1.present-$0:a2.Not Present-$350:
      Power Moonroof:p1.Present-$0:p2.Not present:$595

      Output:

      Focus Wagon ZTW model
      null
      BasePrice-18,445
      null
      Color
      c1.Fort Knox Gold Clearcoat Metallic
      c2.Liquid Grey Clearcoat Metallic
      c3.Infra-Red Clearcoat
      c4.Grabber Green Clearcoat Metallic
      c5.Sangria Red Clearcoat Metallic
      c6.French Blue Clearcoat Metallic
      c7.Twilight Blue Clearcoat Metallic
      c8.CD Silver Clearcoat Metallic
      c9.Pitch Black Clearcoat
      c10.Cloud 9 White Clearcoat
      null

      Transmission
      t1.Automatic-$0
      t2.Manual-$-815
      c3.Infra-Red Clearcoat


      Brakes/TractionControl
      b1.Standard-$0
      b2.ABS-$425Exception in thread "main" java.util.NoSuchElementException
           at java.util.StringTokenizer.nextToken(Unknown Source)
           at Automotive.Automotive.readFile(Automotive.java:36)
           at Automotive.Main.main(Main.java:16)

      b3.ABS with Advance Trac-$1625
      c4.Grabber Green Clearcoat Metallic

      Side Impact Air Bags
      a1.present-$0
      a2.Not Present-$350
      b3.ABS with Advance Trac-$1625

      Power Moonroof
      p1.Present-$0
      p2.Not present
      $595
      c4.Grabber Green Clearcoat Metallic

      My question is in Transmission after t1 and t2 it is again displaying another option called c3.....

      Similar is the case with other options too.......

      Can anyone tell where iam going wrong???????????
        • 1. Re: Help with String Tokenizer Class.........
          807606
          I don't see how you are getting an exception in the middle of your output and yet your program continues. are you pasting the correct output?

          Also, try this instead
          for (int i = 0 ; i < idx.length ; i++)    //for(idx=0;idx<tokenCount; idx++)
          { 
          System.out.println(words); 
          }

          Message was edited by:
          desimunda
          • 2. Re: Help with String Tokenizer Class.........
            807606
            You're writing too much code my friend.

            Just try this:
                BufferedReader inputstream = null;
                
                public void readFile() throws IOException {
                    inputstream = new BufferedReader(new  FileReader("C:\\CarInputData.txt"));
                    
                    StringTokenizer st;
                    String line = null;
                    
                    while ((line = inputstream.readLine()) != null) {
                        st = new StringTokenizer(line,":");
                        while (st.hasMoreTokens())
                            System.out.println(st.nextToken());
                    }
                }
            You can check this little example:

            http://www.javadb.com/using-the-stringtokenizer-class

            Cheers,

            Nick

            Message was edited by:
            eurogolfer

            (forgot to remove a couple of variable declarations)