1

Basically the code I have below does currently read from a text file, but what I want it to do is store a value so that I can use it later for a another function. So from the text file I would like to store the height (175) and weight (80) value. How would that be done?

Text File:

Name: ..........
Height: 175
Weight 80

MainActivity:

package com.example.readfromfiletest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    Button b_read;
    TextView tv_text;

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

        b_read = (Button) findViewById(R.id.b_read);
        tv_text = (TextView) findViewById(R.id.tv_text);

        b_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = "";
                try {
                    InputStream is = getAssets().open("test.txt");
                    int size = is.available();
                    byte[] buffer = new byte[size];
                    is.read(buffer);
                    is.close();
                    text = new String(buffer);

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                tv_text.setText(text);
            }
        });
    }
}
Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
Jman123
  • 21
  • 2

1 Answers1

0

Judging from your comments, it sounds like you're asking how to properly read in the values into different variables rather than reading them into one String. I think the first thing you should do to achieve this is read the file in line by line with a BufferedReader. Then for each line you read in you can determine which variable to assign the value to. For instance, you could do this:

Button b_read;
TextView tv_text;
String name = "";
int height = 0;
int weight = 0;

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

    b_read = (Button) findViewById(R.id.b_read);
    tv_text = (TextView) findViewById(R.id.tv_text);

    b_read.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String text = "";
            try {
                BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(getAssets().open("test.txt")));
                String line;
                while((line = bufferedReader.readLine()) != null){
                    text = text.concat(line + "\n");
                    String[] lineVals = line.split(":");
                    if(lineVals[0].equalsIgnoreCase("name")){
                        name = lineVals[1].trim();
                    } else if(lineVals[0].equalsIgnoreCase("height")){
                        height = Integer.parseInt(lineVals[1].trim());
                    } else if(lineVals[0].equalsIgnoreCase("weight")){
                        weight = Integer.parseInt(lineVals[1].trim());
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            tv_text.setText(text);
        }
    });
}

The BufferedReader reads in one line at a time. For example just, "Height: 175"

The line is then split on the ":", returning a String[] with two values. Continuing with our Height example, the array looks something like this: ["Height", " 175"]

The if statements (could also be case statements) then determine whether we're dealing with the name, height or weight variable.

The value is then assigned to its appropriate variable. The trim() method is called during this assignment to remove the space after the colon. You could also circumvent this by performing the split() method on ": ".

You could also stick with your current method and do some String manipulation involving splitting, Regex, or some other method, but I am of the opinion that my proposed solution will be a bit easier to read/work with in the future.

Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
  • Thanks a lot dude! Thats exactly what I wanted....regarding the text file I added some extra fields such as address and date of birth but I do not need to use them fields, do I need to add them below the name trim or are they not needed? – Jman123 Mar 22 '18 at 15:03
  • You can handle that a couple of ways. You could make more variables and add more if statements to apply the fields in the file to the proper variables so you can use them in the future if you so desire. You could also just leave things how they are in my example, and that will completely ignore the extra values in the file, and it should still work fine. Be warned that if there are multiple objects (multiple "Name" rows, multiple "Height" rows, etc) the variables will populate with the last ones. Don't forget to [accept my answer](https://meta.stackexchange.com/a/5235) if it worked for you. – Francis Bartkowiak Mar 22 '18 at 17:29
  • Ok thanks dude! I dont think I'll need mutliple objects, all I need is one for each one. – Jman123 Mar 23 '18 at 00:35
  • I do have a question.....if I want to save new values to the height and weight and replace the existing ones, how would I do that? – Jman123 Mar 23 '18 at 00:45
  • 1
    If you're asking how to save values to a text file, that is a totally separate question. For starters, I'd look at [this thread](https://stackoverflow.com/a/2885224/8972283), and [this thread](https://stackoverflow.com/a/10390351/8972283). Then try to develop a solution. If you run into issues and can't figure it out after a valiant attempt, then ask a new question and be sure to include the code from your attempt. – Francis Bartkowiak Mar 23 '18 at 01:19