简体   繁体   中英

Continue iteration of outer while loop

I have to read data from two files. For that, I have to iterate these two files using while. Here is my code...

// Data in File1 are A, B, C, D // Data in File2 are 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();
                    }


                }

I am using while loop. What I am getting here, first time count = 1 and countTwo variable as 1, 2 and 3 and then again count variable as 2, 3, 4 (As data in file1 are 4 and in file2 are 3). Now I have to iterate outer while loop such as I get count values as count=2 and countTwo= 1, 2, 3. Again count=3 and countTwo = 1, 2, 3. Again count=4 and countTwo = 1, 2, 3. What needs to be done?

After the first iteration of scanFile, scanFileT will be exhausted as you have fully iterated through it. You will need to re-initialize it.

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

After your Update:

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

 }
 scanFileT.close();
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();
            }

Read your secon file inside the outer while loop instead of outside, ie for every single line of your first file your second file will be read and the countTwo will start from 1 again

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

  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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM