我有以下格式,其中左邊一列是lineNumber
右列一個簡單的.txt文件(「theFile.txt」)是word
:如何僅從.txt文件中選擇性地讀取數字和/或單詞?
5 today
2 It's
1 "
4 sunny
3 a
6 "
對於這個txt文件,我「M使得兩種不同的方法來每個得到僅的數量和僅字符串,加上掃描文件,並把每個lineNumber
和word
在雙鏈表另一種方法:
String fileName = "theFile.txt";
public int getNumberOnly() {
int lineNumber;
//code to only get the lineNumber but NOT the words
//This is as far as I got and I need help on this part
return lineNumber;
}
public String getWordsOnly() {
String words;
//code to only get the words but NOT the lineNumber
//This is as far as I got and I need help on this part
return words;
}
public void readAndPrintWholeFile(String fileName){
String fileContents = new String();
File file = new File("theFile.txt");
Scanner scanner = new Scanner(new FileInputStream(fileName));
DLL<T> list = new DLL<T>();
//Print each lineNumber and corresponding words for example
// 5 Today
// 2 It's
while (scanner.hasNextLine())
{
fileContents = scanner.nextLine();
System.out.println(list.getNumbersOnly() + " " + list.getWordOnly());
//prints the lineNumber then space then the word
}
}
//I already have all DLL accessors and mutators such as get & set next/previous nodes here, etc.
我卡在如何爲這兩種getNumbersOnly()
和getWordOnly()
我已經盡我所能去這個點的編碼方法體。謝謝你的幫助。
把這個分爲兩種方法有什麼意義?這是作業嗎? –
這是一個練習 - 我在這裏問,因爲我已經用盡了所有我知道的。 – 5120bee
是通過空間均勻的所有行分隔的數字和文字? –