2

My Program needs to be able to scan a text file, and store the values of the text file into a database. Suppose I've read in a line of code that looks like this

3,95003,"ALLENDALE",,41.030902,-74.130957,2893

I want to be able to call a part of the string and store it as a database value. How exactly would I do that? I already know how to read in text files, but i need to know how to filter and only grab a part of them.

In a database I would usually add a value by doing something like this.

values.put("A", "B");

How can i read a value from only part of the textfile into B?

Say I want it to display values.put("A", "ALLENDALE").

Answer:

AgencyString = readText();
        tv = (TextView) findViewById(R.id.letter);

        tv.setText(readText());

        StringTokenizer st = new StringTokenizer(AgencyString, ",");
        while (st.hasMoreElements()) {
            tv.setText(st.nextToken());
        }
    }

    private String readText() {
        InputStream inputStream = getResources().openRawResource(R.raw.agency);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int i;
        try {
            i = inputStream.read();
            while (i != -1) {
                byteArrayOutputStream.write(i);
                i = inputStream.read();
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return byteArrayOutputStream.toString();

    }

The next step for me is to store these values seperated into an array, and read them into my database. I was able to filter through my agency.txt file by using a StringTokenizer method and setting a 'comma' as my delimiter.

The Tokenizer
  • 1,546
  • 3
  • 28
  • 46

1 Answers1

2

Look at. Use delimiters ",". http://developer.android.com/reference/java/util/StringTokenizer.html http://developer.android.com/reference/java/lang/String.html#split(java.lang.String)

http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String, int)

Afshin Moazami
  • 2,003
  • 5
  • 32
  • 54
Marcelo H
  • 86
  • 1
  • 4