继续迭代外部while循环

时间:2013-08-27 06:54:15

标签: java file nested-loops

我必须从两个文件中读取数据。为此,我必须使用while迭代这两个文件。这是我的代码......

// File1中的数据是A,B,C,D // File2中的数据是A,B,C

Scanner scanFile = new Scanner(new DataInputStream(fOne));
                while (scanFile.hasNextLine())
                {
                    countTwo = 0;
                    if(scanFile.nextLine()!=null)
                    {
                        count++;
                        Toast.makeText(getBaseContext(), "Count : " + count, 500).show();
                    }
                    else
                        scanFile.close();

                    Scanner scanFileT = new Scanner(new DataInputStream(fTwo));
                    while(scanFileT.hasNextLine())
                    {
                        if(scanFileT.nextLine()!=null)
                        {

                            countTwo++;
                            Toast.makeText(getBaseContext(), "CountTwo : " + countTwo, 500).show();                 
                        }

                        else
                            scanFileT.close();
                    }


                }

我正在使用while循环。我在这里得到的,第一次计数= 1,countTwo变量为1,2和3,然后再次将变量计为2,3,4(因为file1中的数据为4,文件2中的数据为3)。现在我必须迭代外部while循环,例如我得到计数​​值为count = 2和countTwo = 1,2,3。再次count = 3和countTwo = 1,2,3。再次count = 4和countTwo = 1,2 ,3。需要做什么?

4 个答案:

答案 0 :(得分:1)

在scanFile的第一次迭代之后,scanFileT将在您完全迭代完毕后耗尽。您需要重新初始化它。

Scanner scanFileT = new Scanner(new DataInputStream(fTwo));
while(scanFileT.hasNextLine())
{
.
.
}

更新后:

 Scanner scanFileT = new Scanner(new DataInputStream(fTwo));
 while(scanFileT.hasNextLine())
 {

 }
 scanFileT.close();

答案 1 :(得分:1)

try
            {
            FileInputStream fOne = openFileInput("File1");

            } 
            catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            }
            Scanner scanFile = new Scanner(new DataInputStream(fOne));
            while (scanFile.hasNextLine())
            {
                countTwo = 0;
                if(scanFile.nextLine()!=null)
                {
                    count++;
                    Toast.makeText(getBaseContext(), "Count : " + count, 500).show();
                }
               FileInputStream fTwo = openFileInput("File2");
               Scanner scanFileT = new Scanner(new DataInputStream(fTwo));
                while(scanFileT.hasNextLine())
                {
                    if(scanFileT.nextLine()!=null)
                    {

                        countTwo++;
                        Toast.makeText(getBaseContext(), "CountTwo : " + countTwo, 500).show();                 
                    }
                }
              fTwo.close();
            }

在外部while循环内而不是外部读取你的secon文件,即第一个文件的每一行都将读取你的第二个文件而countTwo将从1再次开始

答案 2 :(得分:0)

你可以在外部while循环的开头指定countTwo = 1,或者你可以在进入内部while循环之前指定这个值

答案 3 :(得分:0)

  try
                    {
                        fOne = openFileInput("File1");
                        //fTwo = openFileInput("File2");
                    } 
                    catch (FileNotFoundException e) 
                    {
                        e.printStackTrace();
                    }

                    Scanner scanFile = new Scanner(new DataInputStream(fOne));
                    while (scanFile.hasNextLine())
                    {
                        countTwo = 0;
                        if(scanFile.nextLine()!=null)
                        {
                            count++;

                        }

                        try
                        {
                            fTwo = openFileInput("File2");
                        }

                        catch (FileNotFoundException e) 
                        {
                            e.printStackTrace();
                        }


                        Scanner scanFileT = new Scanner(new DataInputStream(fTwo));
                        while(scanFileT.hasNextLine())
                        {
                            if(scanFileT.nextLine()!=null)
                            {                           countTwo++;

                            }
                         }
                        scanFileT.close();


                    }
                    count = 0;