How to read out of all lines in the .txt file specific fields required for the output?

I am new to android app development. need your help in solving this. I am not getting expected result. There are about 108 lines in my .txt file. It is only reading the same line for any given input. Wherhas it has to display the line according to the selected values by the user out of these 108 lines.

My textfile looks something like this

10  
60  
40  
Z185-LS41F,GE-A24,5.3K/3.0K&4.8K/5.4K,87bar/565°C,33mbar,TKSI: 126.9 MUSD  
10  
60  
50  
Z286-LS41F,GE-A24,5.3K/3.0K&4.8K/5.4K,87bar/565°C,33mbar,TKSI: 126.9 MUSD  
.  
.  
.  

so on 108 lines

if user gives input values like this for petrol:10, diesel:60, air:40 then

Expected output:

Z185-LS41F,GE-A24,5.3K/3.0K&4.8K/5.4K,87bar/565°C,33mbar,TKSI: 126.9 MUSD

Any help on this will be highly appreciated

here is the code

import android.app.Activity;

import android.content.res.AssetManager;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.EditText;

import android.widget.Toast;



import java.io.BufferedReader;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.LineNumberReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import static java.lang.System.in;


class Entry {

    /* fields */

    String petrol;
    String diesel;
    String air;
    String btn;

    Entry(String str1, String str2, String str3, String str4)
    {
        this.petrol = str1;
        this.diesel = str2;
        this.air = str3;
        this.btn = str4;
    }
}


public class MainActivity extends Activity {

    TextView petrol;
    TextView diesel;
    TextView air;
    EditText edt1;
    EditText edt2;
    EditText edt3;
    Button btn;
    BufferedReader reader;
    StringBuilder text = new StringBuilder();


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        petrol = (TextView) findViewById( R.id.petrol );
        diesel = (TextView) findViewById( R.id.diesel);
        air = (TextView) findViewById( R.id.air );
        edt1 = (EditText) findViewById( R.id.edt1 );
        edt2 = (EditText) findViewById( R.id.edt2 );
        edt3 = (EditText) findViewById( R.id.edt3 );
        btn = (Button) findViewById( R.id.btn );

        btn.setOnClickListener( new ClickButton() );
    }

    private class ClickButton implements Button.OnClickListener {


        //String Text1 = "";
        //String Text2 = "";
        //String Text3 = "";
        //String Text4 = "";
        String mLine = "";
        Entry e = new Entry( "", "", "", "" );

        @Override
        public void onClick(View v) {
            AssetManager assetManager = getAssets();

            // To load text file
            InputStream input;
            BufferedReader reader = null;
            try {

                input = assetManager.open( "file.txt" );

                 reader = new BufferedReader( new InputStreamReader( input, "UTF8" ) );






                        e.petrol = reader.readLine() + " ";
                        e.diesel = reader.readLine() + " ";
                        e.air = reader.readLine() + " ";
                        e.btn = reader.readLine() + " ";




                String Button = btn.getText().toString();
                    //if edt1.getText() == e.petrol && edt2.getText() == e.diesel && edt3.getText() == e.air
                    edt1.setText( e.petrol );
                    edt2.setText( e.diesel );
                    edt3.setText( e.air );

                    btn.setText( e.btn );
                while ((mLine = reader.readLine()) != null) {
                    //process line
                    text.append( mLine );
                    text.append( '\n' );
                }
            } catch (IOException e) {
                Toast.makeText( getApplicationContext(), "Error reading file!", Toast.LENGTH_LONG ).show();
                e.printStackTrace();
                //log the exception
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        //log the exception
                    }


                }


            }
        }
    }

1 answer

  • answered 2018-01-14 13:04 Saketh Katari

    The below code (tested) creates a file named sample.txt in internal_storage and tries to access sample.txt based on user inputs - petrol, diesel & air and finally displays the result on the screen.

    MainActivity.java -

    package com.example.sakethkatari.myapplication;
    
    import android.app.Activity;
    import android.content.Context;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    
    public class MainActivity extends AppCompatActivity
    {
        private EditText editTextPetrol = null;
        private EditText editTextDiesel = null;
        private EditText editTextAir = null;
        private TextView textViewResult = null;
        private Button buttonSubmit = null;
    
        private Context context = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            context = getApplicationContext();
            createFile("sample.txt", "10\n20\n30\nSaketh\n40\n50\n60\nKatari\n");
    
            editTextPetrol = (EditText) findViewById(R.id.editTextPetrol);
            editTextDiesel = (EditText) findViewById(R.id.editTextDiesel);
            editTextAir = (EditText) findViewById(R.id.editTextAir);
            textViewResult = (TextView) findViewById(R.id.textViewResult);
            buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    
            buttonSubmit.setOnClickListener( new MyListener(editTextPetrol, editTextDiesel, editTextAir, textViewResult, context) );
        }
    
        private void createFile(String filename, String data)
        {
            FileOutputStream fos = null;
    
            try
            {
                fos =context.openFileOutput(filename, Context.MODE_PRIVATE);
                fos.write( data.getBytes() );
                fos.close();
            }
    
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
    class MyListener implements View.OnClickListener
    {
        private EditText editTextPetrol = null;
        private EditText editTextDiesel = null;
        private EditText editTextAir = null;
        private TextView textViewResult = null;
        private  Context context = null;
    
        private String petrol = null;
        private String diesel = null;
        private String air = null;
        private String filename = null;
        private String result = null;
    
        MyListener(EditText editTextPetrol, EditText editTextDiesel, EditText editTextAir, TextView textViewResult, Context context)
        {
            this.editTextPetrol = editTextPetrol;
            this.editTextDiesel = editTextDiesel;
            this.editTextAir = editTextAir;
            this.textViewResult = textViewResult;
            this.context = context;
        }
    
        @Override
        public void onClick(View view)
        {
            petrol = editTextPetrol.getText().toString();
            diesel = editTextDiesel.getText().toString();
            air = editTextAir.getText().toString();
            filename = "sample.txt";
    
            result = foo(filename, petrol, diesel, air );
    
            if( result == null )
            {
                result = "No entry found!";
            }
    
            textViewResult.setText(result);
        }
    
        private String foo(String filename, String petrol, String diesel, String air)
        {
            FileReader fileReader = null;
            BufferedReader br = null;
            String currentPetrol = null, currentDiesel = null, currentAir = null, currentResult = null;
            File file = null;
    
    
            try
            {
                file = new File(context.getFilesDir(), filename);
                fileReader = new FileReader(file);
                br = new BufferedReader(fileReader);
    
                while(true)
                {
                    currentPetrol = br.readLine();
    
                    if( currentPetrol == null )
                    {
                        currentResult = null;
                        break;
                    }
    
                    currentDiesel = br.readLine();
    
                    if( currentDiesel == null )
                    {
                        currentResult = null;
                        break;
                    }
    
                    currentAir = br.readLine();
    
                    if( currentAir == null )
                    {
                        currentResult = null;
                        break;
                    }
    
                    currentResult = br.readLine();
    
                    if( currentResult == null )
                    {
                        break;
                    }
    
                    if( currentPetrol.equals(petrol) && currentDiesel.equals(diesel) && currentAir.equals(air) )
                    {
                        break;
                    }
                }
    
                fileReader.close();
                br.close();
            }
    
            catch(Exception e)
            {
                e.printStackTrace();
            }
    
            return currentResult;
        }
    }
    

    activity_main.xml -

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="10dp"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textViewPetrol"
            android:text="Petrol"
            android:layout_marginTop="60dp"
            />
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editTextPetrol"
            />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textViewDiesel"
            android:text="Diesel"
            android:layout_marginTop="30dp"
            />
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editTextDiesel"
            />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textViewAir"
            android:text="Air"
            android:layout_marginTop="30dp"
            />
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editTextAir"
            />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textViewResult"
            android:layout_marginTop="40dp"
            android:layout_gravity="center"
            />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/buttonSubmit"
            android:text="Submit"
            android:layout_gravity="center"
            android:layout_marginTop="60dp"
            />
    
    </LinearLayout>