Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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/9/loops/2.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 使用增量作为列在拆分后向arraylist中添加值_Java_Loops_Arraylist_Split - Fatal编程技术网

Java 使用增量作为列在拆分后向arraylist中添加值

Java 使用增量作为列在拆分后向arraylist中添加值,java,loops,arraylist,split,Java,Loops,Arraylist,Split,我正在使用运行在EclipseIDE上的Java。 我正在努力取得成功 Name: John Money: 900 Password: john321 Name: Mark Money: 300 Password: mark321 从这样的文本文件 //name|money|password john|900|john321 mark|300|mark321 以下是我尝试过的: public static void ReadFile() throws IOException {

我正在使用运行在EclipseIDE上的Java。 我正在努力取得成功

Name: John
Money: 900
Password: john321

Name: Mark
Money: 300
Password: mark321
从这样的文本文件

//name|money|password

 john|900|john321
 mark|300|mark321
以下是我尝试过的:

public static void ReadFile() throws IOException {      
    FileReader rf = new FileReader("Data.dat");
    BufferedReader br = new BufferedReader(rf);
    String reader;
    ArrayList<String> dataInfo = new ArrayList<String>();
    while((reader = br.readLine())!=null)
    {
        dataInfo.add(reader);
    } // end of while loop
    br.close();
    ArrayList<String> name = new ArrayList<String>();
    ArrayList<String> money = new ArrayList<String>();
    ArrayList<String> password = new ArrayList<String>();

    for(int i =0; i<dataInfo.size();i++) {      
          String[] test = dataInfo.get(i).split("\\|"); 
          int j =0;
            for(String data : test) {
                //System.out.println(data); // this print like what i wanted from the above outcome i posted
                if(j==0){
                    name.add(data); 
                    System.out.println(name); // This works well.
                   j++;
                }
                if(j==1){
                    password.add(data); 
                     System.out.println(password); //ERROR this print the name instead          
                    j++;
                }
                if(j==2){
                    money.add(data);
                    System.out.println(money); //ERROR this print the name instead
                    j++;
                }
            }
    }//end of for-loop

    for(int k =0;k<name.size();k++)
    {
        System.out.println("Name: " +name.get(k));
        System.out.println("Money: " +money.get(k));
        System.out.println("Password: " +password.get(k));
        System.out.println("\n"); 
    }
}
我试图使用变量j作为列,因为在程序的后面,我需要使用名称、金钱和密码进行筛选搜索。我需要对for循环进行迭代,因为当用户输入他们希望删除的用户名时,我想删除整个文本,例如ifname.geti=john{//delete password.geti,money.geti},但现在的问题是,我添加到arrayList中的值始终是唯一的名称,其中as System.out.printlndata打印所有内容。

如果当前语句将首先执行,则需要其他语句

if j == 0 then increment j
那就行了

if j == 1 - yes it is
无论如何,IMO的一个更干净的方法是

String[] test = dataInfo.get(i).split("\\|"); 
if (test.length == 3) {
   just use test[0] test[1] and test[2]
}
else {
   // otherwise this is bad data - need to do something special
}
如果您真的坚持使用像j这样的构造,那么为什么要为每个块使用a,为什么不使用传统的for循环呢


您的问题是,if块对程序流没有任何作用。每个块在每次迭代中都会被输入,因为这些情况并不相互排斥

您必须使用else来确保每次迭代只输入一个块

for(String data : test) {
            //System.out.println(data); // this print like what i wanted from the above outcome i posted
            if(j==0){
                name.add(data); 
                System.out.println(name); // This works well.
               j++;
            }
            else if(j==1){
                password.add(data); 
                 System.out.println(password); //ERROR this print the name instead          
                j++;
            }
            else if(j==2){
                money.add(data);
                System.out.println(money); //ERROR this print the name instead
                j++;
            }
        }

您必须按照以下方式重新安排if块:

    if(j==2){
        password.add(data); 
        System.out.println(password);         
        j++;
    }
    if(j==1){
        money.add(data);
        System.out.println(money); 
        j++;
    }
    if(j==0){
        name.add(data); 
        System.out.println(name); 
       j++;
    }
其他明智的使用,如果没有

您的代码显示错误,因为当您转到第一个if块j==0时 它将j的值更改为1。如果块j==1,则进入第二个。 但变量数据仍然具有name的值。数据只有在forString数据的下一次迭代中才有金钱和密码:test

注意:根据您的文本文件,我认为j==1将有钱,j==2将有密码。您在if块中给出了错误的值。

您可以使用java.util.Scanner逐行读取文件。用|拆分每行,并将拆分后的值添加到相应的列表中:

ArrayList<String> name = new ArrayList<String>();
        ArrayList<String> money = new ArrayList<String>();
        ArrayList<String> password = new ArrayList<String>();

        Scanner scanner = new Scanner(new File("filePath"));
        while (scanner.hasNextLine()) {
            String[] split = scanner.nextLine().split("\\|");
            if(split.length==3) {
                name.add(split[0]);
                money.add(split[1]);
                password.add(split[2]);
            }
        }
        scanner.close();
for(String data : test) {
            //System.out.println(data); // this print like what i wanted from the above outcome i posted
            if(j==0){
                name.add(data); 
                System.out.println(name); // This works well.
               j++;
            }
            else if(j==1){
                password.add(data); 
                 System.out.println(password); //ERROR this print the name instead          
                j++;
            }
            else if(j==2){
                money.add(data);
                System.out.println(money); //ERROR this print the name instead
                j++;
            }
        }
    if(j==2){
        password.add(data); 
        System.out.println(password);         
        j++;
    }
    if(j==1){
        money.add(data);
        System.out.println(money); 
        j++;
    }
    if(j==0){
        name.add(data); 
        System.out.println(name); 
       j++;
    }
ArrayList<String> name = new ArrayList<String>();
        ArrayList<String> money = new ArrayList<String>();
        ArrayList<String> password = new ArrayList<String>();

        Scanner scanner = new Scanner(new File("filePath"));
        while (scanner.hasNextLine()) {
            String[] split = scanner.nextLine().split("\\|");
            if(split.length==3) {
                name.add(split[0]);
                money.add(split[1]);
                password.add(split[2]);
            }
        }
        scanner.close();