0

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?

4
  • If i understand you correctly, you want the countTwo to start from 1 everytime the count increment. If that is the case, I think you should just assign countTwo = 1 before entering that inner while loop! Commented Aug 27, 2013 at 6:58
  • Actually, I have to compare contents of file here. Means, first content of first file will be matched with all contents of second file. Again, second content of first file will be matched with all contents of second file and so on till the last content of first file.. Commented Aug 27, 2013 at 7:02
  • Your example doesn't even compile. Please provide a runnable example of what you are doing. Commented Aug 27, 2013 at 7:35
  • Also consider first reading the input files and storing them in memory and then doing the comparisons. Reading the second file mutiple times may prove to be a bottleneck. Commented Aug 27, 2013 at 7:44

4 Answers 4

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Anil Edited the code, Initialize your countTwo Variable to zero inside the outer While loop
@Anil You have to close the fileTwo Inputstream and read it inside the outer while loop. Code updated
1

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();

6 Comments

Sorry my explanation is correct, but you may want to close your scanner before re-initialize it.
hey what are u saying? I have closed scanner out of outer while loop
before re-initializing it, close the previous scanner. Has to be in the loop doesn't it. BTW "hey what are u saying?" doesn't sound very polite.
sorry for that...But when i put scanner.close() in loop, nothing works
I think you need to re-show your code. Maybe edit your question.
|
0

you can assign countTwo = 1 in the beginning of outer while loop, alternatively you can assign this value just before entering the inner while loop

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.