我有一個文本文件,當我讀取文件的每一行並將其寫入數組。我想檢查角色是不是美元符號'$'。如果是,則跳轉到下一個字符並寫下一部分,直到下一個數組中的下一個美元符號。所以要把每一行分成三部分,每一部分在不同的陣列中。檢查文本文件中的每個字符,Java
感謝您的時間和幫助!
公共無效retrieveFromTxt()拋出IOException異常
{
textArea.setText(" ");
String fileName = "Name_Of_City.txt";
String line = " ";
String entry = null;
y = 0;
char letter = '$', value = ' ';
FileReader fileReader = new FileReader(fileName);
BufferedReader br1 = new BufferedReader(fileReader);
String TextLine = br.readLine();
x = 1; // Random Variable to jump from character to character in array.
// To Get Values Back from FIle Each value in correct array, I seperated each value with "$" sign
// Checking each letter and printing it to array[y] possition until the $ sign is met, then jump over it and continue in other array.
try {
while(y < 19) {
while(TextLine != null) {
while(TextLine.charAt(x)!=(letter)) {
line = line + TextLine.charAt(x);
Country[y] = (line );
x++;
}
}
while(TextLine != null) {
while(TextLine.charAt(x)!=(letter)) {
line = line + TextLine.charAt(x);
City[y] = (line );
x++;
}
}
while((line = br1.readLine()) != null) {
while(line.charAt(x)!=(letter)) {
Population[y] = (textArea.getText()+ entry);
x++;
}
}
y++;
textArea.setText(textArea.getText()+ "\n");
}
}
catch(FileNotFoundException error) {
//Exception can be met if program cannot find the file , in that case messageDialog will pop up with help message
JOptionPane.showMessageDialog(null,"Missing file"+", " + fileName + ", for a support contact [email protected]!") ;
}catch (StringIndexOutOfBoundsException err){}
br1.close();
}
}
到目前爲止,您嘗試了什麼?如果不是,你應該看看字符串標記器 – zython
顯然你期望每一行有三個部分,但這並不清楚,因爲你沒有給我們樣本輸入。你可能可以通過'$'分開。 –
@zython:創建和維護Java的每個人,這是不好的建議。根據[StringTokenizer API](http://docs.oracle.com/javase/8/docs/api/java/util/StringTokenizer.html):'「StringTokenizer是遺留類,儘管使用它,但由於兼容性原因而被保留在新的代碼中是不鼓勵的,建議任何尋求這種功能的人都使用String或java.util.regex包的拆分方法。「' –