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
. . .