[英]Java reading a .txt file and sorting using a substring
因此,我寫出了打開並讀取.txt文件然后打印內容的代碼。 現在,我已經完成了我要根據數據的每一位中的第一個字符將數據分為三個單獨的鏈接列表的操作,這些字符將是F,T或P。一些數據的示例F12,F43,T31,P64,P17 ,T23等。經過排序后,它們應該看起來像這樣...
F灣:F12,F43
T灣:T31,T23
托架P:P64,P17
感謝您提供任何幫助,我的意思是任何幫助。 下面是到目前為止的代碼。
編輯:-好的,我現在修改了更改,但是我仍然得到相同的打印。 所以在更改之前我得到了此打印T16,T17,F99,F14,P34,P88,T63,F58,P02,P76,F77,T99,P14,P24,T88,F63,F53,T02 T16,T17,F99,F14,P34,P88,T63,F58,P02,P76,F77,T99,P14,P24,T88,F63,F53,T02,所以我不知道怎么了?
package stackandqueue;
import java.util.*;
import java.util.Stack;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.Arrays;
public class StackAndQueue
{
public static void main(String[] args) throws IOException
{
// Create an three empty queues of station bays objects
LinkedList<String> bayoneQueue;
bayoneQueue = new LinkedList<String>();
LinkedList<String> baytwoQueue;
baytwoQueue = new LinkedList<String>();
LinkedList<String> baythreeQueue;
baythreeQueue = new LinkedList<String>();
// Open and read text file
String inputFileName = "PodData4.txt";
FileReader fileReader = null;
// Create the FileReader object
try {
fileReader = new FileReader("PodData4.txt");
BufferedReader br = new BufferedReader(fileReader);
String str;
while((str = br.readLine()) != null)
{
System.out.println(str + "\n");
switch (str.charAt(0))
{
case 'F':
bayoneQueue.add(str);
break;
case 'T':
baytwoQueue.add(str);
break;
case 'P':
baythreeQueue.add(str);
break;
default: // in case of invalid input!
}
}
br.close();
}catch(IOException ex)
{
//handle exception;
}
finally
{
fileReader.close();
// close resources
}
// Close the input
}
}
我想您所需要的只是while循環內的switch語句
while ((str = br.readLine()) != null) {
System.out.println(str + "\n");
switch (str.charAt(0)) {
case 'F':
bayoneQueue.add(str);
break;
case 'T':
baytwoQueue.add(str);
break;
case 'P':
baythreeQueue.add(str);
break;
default: // in case of invalid input!
}
}
還應考慮將fileReader.close()
語句移至finally子句內,因為它是注釋中提到的“關閉資源”操作。
我將您的try
更改為“ try-with-resources”,它將自動關閉BufferedReader
。 然后將從文件中讀取該行,並在每個逗號周圍將其拆分為strings
數組。 然后, switch
語句將每個字符串添加到其各自的LinkedList
。 我還將您的fileReader.close()
移到了finally
塊中。
// Open and read text file
String inputFileName = "PodData2.txt";
FileReader fileReader = new FileReader("PodData2.txt");
// Create the FileReader object
try (BufferedReader br = new BufferedReader(fileReader);)
{
String[] strings = br.readLine().split(",");
for (String str : strings)
{
switch (str.charAt(0))
{
case 'F':
bayoneQueue.add(str);
break;
case 'T':
baytwoQueue.add(str);
break;
case 'P':
baythreeQueue.add(str);
break;
default:
// In-case of invalid input
}
}
}
catch(IOException ex)
{
//handle exception;
}
finally
{
fileReader.close();
}
System.out.println("Bay F: " + bayoneQueue.toString());
System.out.println("Bay T: " + baytwoQueue.toString());
System.out.println("Bay P: " + baythreeQueue.toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.