我正在尝试编写一个程序,该程序将使用插入、删除和排序等命令获取文本文件,并让它们从链接列表中插入或删除节点。到目前为止,我已经能够标记字符串并将它们写出来;但我也想使用 if 语句,无论文本行是说插入还是删除。这是我到目前为止所拥有的。感谢您的帮助。
(车道.txt)
insert 1
insert 7
insert 5
delete 7
insert 2
insert 4
delete 5
代码:
导入java.io。; 导入java.util.;
类 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
}//结束类(class)
输出:
操作:插入 键值:1
操作:插入 键值:7
操作:插入 键值:5
操作:删除 键值:7
操作:插入 键值:2
操作:插入 键值:4
操作:删除 键值:5
最佳答案
字符串应该使用等于
进行比较:
if (action.equals("insert")) { // etc.
关于java - 添加 if 语句进行标记化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10864368/