I am trying to write a program that will take a text file with commands such as insert, delete and sort and have them insert or delete a node from a linked list. So far I have been able to tokenize the strings and write them out; but I also want to use if statements whether the text line says to insert or if it says delete. This is what I have so far. Thank you for any help.
(lane.txt)
insert 1
insert 7
insert 5
delete 7
insert 2
insert 4
delete 5
And the code:
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("lane.txt");
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 action = st.nextToken();
String key = st.nextToken();
System.out.println("Action: " + action);
if(action == "insert")
{
System.out.println("holla");
}
System.out.println("Key Value: " + key);
if(action == "delete")
{
System.out.println("holla");
}
System.out.println(" ");
}
} 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: ");
}
} // end if
} // end finally
} // end dbTest
} // end class
The output:
Action: insert Key Value: 1
Action: insert Key Value: 7
Action: insert Key Value: 5
Action: delete Key Value: 7
Action: insert Key Value: 2
Action: insert Key Value: 4
Action: delete Key Value: 5