0

I have to compare two .txt files. Below is my code..

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/file1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="File 1" />

    <Button
        android:id="@+id/file2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/file1"
        android:layout_below="@+id/file1"
        android:text="File 2" />

    <Button
        android:id="@+id/compare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/file2"
        android:layout_below="@+id/file2"
        android:layout_marginTop="16dp"
        android:text="Compare" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {


    FileOutputStream fos;
    FileInputStream fOne, fTwo;
    ArrayList<String> arr1 = new ArrayList<String>();
    ArrayList<String> arr2 = new ArrayList<String>();

    int count = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button  fileOne = (Button)findViewById(R.id.file1);
        Button  fileTwo = (Button)findViewById(R.id.file2);
        Button  compare = (Button)findViewById(R.id.compare);
        arr1.add("1");
        arr1.add("2");
        arr1.add("3");

        arr2.add("2");
        arr2.add("3");


        fileOne.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try
                {
                    fos = openFileOutput("File1", Context.MODE_PRIVATE);

                    for(int temp = 0; temp< arr1.size(); temp++)
                    {
                        fos.write((arr1.get(temp).getBytes()) );
                        fos.write(System.getProperty("line.separator").getBytes());

                    }
                    fos.close();
                    fos.flush();

                }

                catch(Exception e)
                {

                }
            }
        });


        fileTwo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try
                {
                    fos = openFileOutput("File2", Context.MODE_PRIVATE);

                    for(int temp = 0; temp< arr2.size(); temp++)
                    {
                        fos.write((arr2.get(temp).getBytes()) );
                        fos.write(System.getProperty("line.separator").getBytes());

                    }
                    fos.close();
                    fos.flush();

                }

                catch(Exception e)
                {

                }
            }
        });



        compare.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try
                {
                    fOne = openFileInput("File1");
                    fTwo = openFileInput("File2");

                    Scanner scanFileOne = new Scanner(new DataInputStream(fOne));
                    Scanner scanFileTwo = new Scanner(new DataInputStream(fTwo));


                    while (scanFileOne.hasNextLine())
                    {
                        String first = scanFileOne.next();
                        String second = scanFileTwo.next();
                        if(first.equals(second))
                        {
                            count++;
                        }
                    }

                    Toast.makeText(getBaseContext(), "" + count, 1000).show();

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

}

What I have to do is on Compare button click, I have to compare content of both files and message should be prompted. But here my app force closes..What needs to be done?

Looking Forward
  • 3,579
  • 8
  • 45
  • 65
  • I know you know your answer... I gave an answer for your question but you siad that you already know the given answer, and was asking another question that was not into your given question. So how can we determine that you know or don't know your answer? – Pankaj Kumar Aug 23 '13 at 10:07
  • 1
    you never check `scanFileTwo.hasNextLine()` – Patrick Evans Aug 23 '13 at 10:08
  • I am asking about http://stackoverflow.com/questions/18354732/remove-elements-from-arraylist/18354800 question – Pankaj Kumar Aug 23 '13 at 10:08

1 Answers1

1

your while loop needs to check that both files have a next line that can be retrieved

while (scanFileOne.hasNextLine() && scanFileTwo.hasNextLine())
Patrick Evans
  • 41,991
  • 6
  • 74
  • 87