我正在從事Java中的任務,它要求我從.txt文件中讀取值,以便告訴程序執行什麼。如何解析txt文件中的特定值?
Example:
/*
script.txt contains the following
decreaseKey(2, 35)
delete(p)
*/
// What I have tried doing so far:
int p = Integer.parseInt(inputLine.substring(7)); // doesnt work if p is larger then a single char
int p = Integer.parseInt(inputLine.substring(12, 14)); // too specific, if value was 238792 it wouldn't work.
而且棘手的問題,我發現是爲decreaseKey我需要解析2個不同的值,並通過這樣做,我爲INT P中的同樣的事情兩次,並將它傳遞到我的功能...這顯然不會在這種情況下工作。
任何有關如何正確解析script.txt中的每條指令,以便我的程序中的值始終與txt文件中傳遞的值匹配的建議將非常棒!
也許你可以通過解析取決於支架的位置的命令,而不是依賴於硬啓動編碼後的位置,然後決定你需要爲命令解析多少參數。 – Smutje
你需要從script.txt輸出什麼內容? – SMA