Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查文本文件中的每个字符,Java_Java_Bufferedreader_File Handling - Fatal编程技术网

检查文本文件中的每个字符,Java

检查文本文件中的每个字符,Java,java,bufferedreader,file-handling,Java,Bufferedreader,File Handling,我有一个文本文件,当我读取文件的每一行并将其写入数组时。我想检查字符是否不是美元符号“$”。如果是,则跳到下一个字符并写入以下部分,直到下一个数组中的下一个美元符号。因此,要将每条线分成3部分,每个部分在不同的数组中 感谢您的时间和帮助 public void retrieveFromTxt()引发IOException { textArea.setText(" "); String fileName = "Name_Of_City.txt"; String line =

我有一个文本文件,当我读取文件的每一行并将其写入数组时。我想检查字符是否不是美元符号“$”。如果是,则跳到下一个字符并写入以下部分,直到下一个数组中的下一个美元符号。因此,要将每条线分成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();
}
{
textArea.setText(“”);
String fileName=“Name\u Of_City.txt”;
字符串行=”;
字符串项=null;
y=0;
字符字母=“$”,值=”;
FileReader FileReader=新的FileReader(文件名);
BufferedReader br1=新的BufferedReader(文件读取器);
字符串TextLine=br.readLine();
x=1;//在数组中从一个字符跳到另一个字符的随机变量。
//为了从文件中获取正确数组中的每个值,我用“$”符号分隔每个值
//检查每个字母并将其打印到数组[y]位置,直到满足$符号,然后跳过它并继续在其他数组中。
试试{
而(y<19){
while(TextLine!=null){
while(TextLine.charAt(x)!=(字母)){
line=line+TextLine.charAt(x);
国家(y)=(行);
x++;
}
}
while(TextLine!=null){
while(TextLine.charAt(x)!=(字母)){
line=line+TextLine.charAt(x);
城市[y]=(行);
x++;
}
}
而((line=br1.readLine())!=null){
while(line.charAt(x)!=(字母)){
总体[y]=(textArea.getText()+条目);
x++;
}
}
y++;
textArea.setText(textArea.getText()+“\n”);
}
}
捕获(FileNotFoundException错误){
//如果程序找不到该文件,则会遇到异常,在这种情况下,messageDialog将弹出帮助消息
showMessageDialog(空,“缺少文件”+,“+文件名+”,用于支持联系人oleninshelvijs@gmail.com!" ) ;
}catch(StringIndexOutOfBoundsException错误){}
br1.close();
}

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

String s = "my$sign"
String parts[] = s.split("\\$")
现在,部件[0]将保留“我的”,部件[1]将保留“符号”

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

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

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

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

到目前为止你试过什么?如果没有,您应该仔细查看字符串tokenizera,显然您希望每行有三个部分,尽管这并不清楚,因为您没有给我们提供示例输入。您可能可以在
$
@zython上进行拆分:对于创建和维护Java的人来说,这是一个糟糕的建议。根据:
“StringTokenizer是一个出于兼容性原因保留的遗留类,尽管在新代码中不鼓励使用它。建议寻求此功能的任何人使用String拆分方法或java.util.regex包。”
@HovercraftFullOfEels感谢您的更正,您知道的越多