Welcome to Dream.In.Code
Become a Java Expert!

Join 150,191 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,038 people online right now. Registration is fast and FREE... Join Now!




help with String Tokenizing user input

 
Reply to this topicStart new topic

help with String Tokenizing user input

xetulul
24 Sep, 2008 - 01:22 PM
Post #1

New D.I.C Head
*

Joined: 7 Sep, 2008
Posts: 9

I'm having a hard time trying to tokenize the string user input. The program has to open a file, read the strings in a file and place each string into a node in a linked list. Then you apply different edit properties to the nodes, such as add,find. Then you call show() method to show a portion of the file or the whole file.
I never learned how to split an entire line of user input.
The program should work as shown below. I have completed the program but it doesn't tokenize the string, it asks the user for what line, and what text,which is not what prof wants.
My code for tokenizing the string works for some methods and not for others.Also when i ask for new text it doesn't read the whole line user input. Can someone help.

OPEN
---------------
> open README.txt


SHOW
---------------------------
> show 56 62
0056: The term "vendors" used here refers to licensees, developers, and
0057: independent software vendors (ISVs) who license and distribute the
0058: Java 2 Runtime Environment with their programs.
0059:
0060: Vendors must follow the terms of the Java 2 RTE Binary
0061: Code License agreement.
0062:
>
IF USER TYPES: 'show' ,without any values, it means all should be shown.

ADD
----------------------------
> add 3
Text to add? asdf ghij klmn
Example
> show 66 70
0066: The files that make up the Java 2 Runtime Environment are divided into
0067: from redistributions of the Java 2 Runtime Environment at the
0068: licensee's discretion.
0069:
0070: The following section contains a list of the files and directories that
> add 68
Text to add? xxx yyy zzz
> show 66 70
0066: The files that make up the Java 2 Runtime Environment are divided into
0067: from redistributions of the Java 2 Runtime Environment at the
0068: xxx yyy zzz
0069: licensee's discretion.
0070:
>



FIND
-----------------------
Prints out a list of line numbers that contain a given string
> find
Text to find? xyz abc
Found on line(s) 3 17 22 54 106. Or
Text not found.




CODE

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


class EditorNewTokenize
{
   ...

   String cmdIn,cmd,value1,value2;
   int v1,v2;
   String lineAdd,cmdLine;
   String fname,fnamesave;


   public void menu() throws Exception
   {

      Scanner myScanner=new Scanner(System.in);
      System.out.println("\n Options");
      System.out.println( "-----------------");
      System.out.println( " ~ open "+
                        "\n ~ add"+
                        "\n ~ show "+
                        "\n ~ delete"+
                        "\n ~ quit "+
                        "\n-----------------");
      do
      {

         System.out.print("--> " );

         cmdIn=myScanner.nextLine();

         StringTokenizer st = new StringTokenizer(cmdIn);
         cmd=st.nextToken();

         int j=1;
         while (st.hasMoreTokens())
         {
            if(j==1)
               value1=st.nextToken();
            else
               value2=st.nextToken();
            j++;
         }  
  
         if(cmd.equals("open") )
         {
            linecount=0;
            open(value1);
         }
         else if(cmd.equals("show"))
         {
            v1=Integer.parseInt(value1);
            v2=Integer.parseInt(value2);

            if(v1>0)
               show(v1,v2);
            else
               show();
         }
         else if(cmd.equals("add"))
         {
            v1=Integer.parseInt(value1);
            System.out.println("Line to add: ");
            lineAdd=myScanner.nextLine();
            add(v1,lineAdd);
         }
         else if(cmd.equals("delete"))
         {
            v1=Integer.parseInt(value1);
            delete(myScanner.nextInt());
         }      
         else if(cmd.equals("quit")|| cmd.equals("q"))
         { break; }
         else
         { System.out.println("Invalid Entry!Type help for assistance"); }

      }
      while(cmd!="quit");
   }

         methods
         .   .   .



User is offlineProfile CardPM
+Quote Post

Ellie
RE: Help With String Tokenizing User Input
26 Sep, 2008 - 01:44 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 428



Thanked: 4 times
Dream Kudos: 150
My Contributions

This is a very helpful site for String Tokenizing

Tokenizer

In particular, this sample does what you are after I think:

CODE
// source code developed by DevDaily Interactive, Inc.

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

  class TokenTest {

     public static void main (String[] args) {
        TokenTest tt = new TokenTest();
        tt.dbTest();
     }


     void dbTest() {

        DataInputStream dis = null;
        String dbRecord = null;

        try {

           File f = new File("customer.db");
           FileInputStream fis = new FileInputStream(f);
           BufferedInputStream bis = new BufferedInputStream(fis);
           dis = new DataInputStream(bis);

           // read the first record of the database
           while ( (dbRecord = dis.readLine()) != null) {

              StringTokenizer st = new StringTokenizer(dbRecord, ":");
              String fname = st.nextToken();
              String lname = st.nextToken();
              String city  = st.nextToken();
              String state = st.nextToken();

              System.out.println("First Name:  " + fname);
              System.out.println("Last Name:   " + lname);
              System.out.println("City:        " + city);
              System.out.println("State:       " + state + "\n");
           }

        } catch (IOException e) {
           // catch io errors from FileInputStream or readLine()
           System.out.println("Uh oh, got an IOException error: " + e.getMessage());

        } finally {
           // if the file opened okay, make sure we close it
           if (dis != null) {
              try {
                 dis.close();
              } catch (IOException ioe) {
                 System.out.println("IOException error trying to close the file: " +
                                    e.getMessage());
              }

           } // end if

        } // end finally

     } // end dbTest

  } // end class

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:18AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month