Java 读取 .txt 文件并使用子字符串排序

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 子句内部,因为它是您评论中提到的 "closing resource" 操作。

我已将您的 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());