【问题标题】:Checking each character from text file , Java检查文本文件中的每个字符,Java
【发布时间】:2016-08-28 20:15:36
【问题描述】:

我有一个文本文件,当我读取文件的每一行并将其写入数组时。我想检查字符是否不是美元符号 '$' 。如果是,则跳转到下一个字符并写入以下部分,直到下一个数组中的下一个美元符号。因此将每一行分成 3 部分,每部分在不同的数组中。

感谢您的时间和帮助!

public void 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 oleninshelvijs@gmail.com!" ) ;
    }catch (StringIndexOutOfBoundsException err){}
    br1.close();
}

}

【问题讨论】:

  • 到目前为止你尝试过什么?如果不是,您应该查看字符串标记器
  • 显然您希望每行包含三个部分,但这并不清楚,因为您没有给我们提供示例输入。您也许可以在 $ 上进行拆分。
  • @zython:对于创建和维护 Java 的人来说,这是个糟糕的建议。根据StringTokenizer API"StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."
  • @HovercraftFullOfEels 感谢指正,你知道的越多

标签: java bufferedreader file-handling


【解决方案1】:

尝试使用 string.split 函数。你可以这样做:

String s = "my$sign"
String parts[] = s.split("\\$")

现在parts[0] 将持有“my”,parts[1] 将持有“sign”。

编辑:正如下面用户 Ukfub 所指出的,您需要转义该符号。更新我的代码以反映。

【讨论】:

    【解决方案2】:

    字符串部分[] = s.split("$")

    这不起作用,因为“split”方法的参数是正则表达式。您必须转义特殊字符'$':

    String parts[] = s.split("\\$")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 2013-03-02
      • 1970-01-01
      • 2011-04-20
      相关资源
      最近更新 更多