-1

I have been working on this for a while and I am about to pull my hair out!! If I use this...

public void readFile() {

        BufferedReader buffReader = null;
        StringBuilder result = new StringBuilder();
        try {
            FileInputStream fileIn = openFileInput("VariableStore.txt");
            buffReader = new BufferedReader(new InputStreamReader(fileIn));
            String line;
            while ((line = buffReader.readLine()) != null) {
                result.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                assert buffReader != null;
                buffReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String resultString = result.toString();
        String[] controlString = resultString.split("$");
//        String wb = controlString[4];
//        String sb = controlString[5];

            ((Button) this.findViewById(R.id.wakeButton)).setText(resultString);
//        ((Button) this.findViewById(R.id.sleepButton)).setText(sb);
//        ((Button)this.findViewById(R.id.wakeButton)).setText(result);
//        ((Button)this.findViewById(R.id.wakeButton)).setText(result);
//        ((Button)this.findViewById(R.id.wakeButton)).setText(result);

    }

The Button.setText works fine with "resultString" or with "result" which is a string I have input formatted as xxx$xxx$xxx$xxx$xxx so when I read it back in with the readFile() I want to use .Split and put it into an array "controlString" and then assign the array elements to my widgets i.e. setText(controlString[0]); but if I so much as even uncomment the lines String wb = controlString[4]; or String sb = controlString[5]; my program crashes. Why wont the array elemts work here? Here is my writeFile().... (Which works perfectly.

public void writeFile() {

BufferedWriter buffWriter = null;
String wb = ((Button)this.findViewById(R.id.wakeButton)).getText().toString();
String sb = ((Button)this.findViewById(R.id.sleepButton)).getText().toString();
String tb = ((EditText)this.findViewById(R.id.textHoursBetween)).getText().toString();
String ti = ((EditText)this.findViewById(R.id.textIncrementTime)).getText().toString();
String td = ((EditText)this.findViewById(R.id.textIncrementDays)).getText().toString();

String writeString = wb + "$" + sb + "$" + tb + "$" + ti + "$" + td;

try {
    FileOutputStream fileOut = openFileOutput("VariableStore.txt", Context.MODE_PRIVATE);
    buffWriter = new BufferedWriter(new OutputStreamWriter(fileOut));
    try {
        buffWriter.write(writeString);
    } catch (IOException e) {
        e.printStackTrace();
    }

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

    try {
        assert buffWriter != null;
        buffWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

CJG
  • 19
  • 5
  • 2
    "my program crashes" -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Apr 06 '15 at 23:52
  • I cant use an AVD because I am on windows 10 and HAX doesn't work with Windows 10 so I am developing fro my cell phone. – CJG Apr 07 '15 at 00:32
  • 1
    First, avoid creating a thousand strings. `((Button) this.findViewById(R.id.wakeButton)).setText(controlString [1]);` works without creating a new string object. Second, if you are developing from phone, go to market and download a logcat. Then check it for your errors – jb15613 Apr 07 '15 at 00:49
  • ok so I Toasted the Exception and I got Length = 1; index=4 I get what its saying but I still don't understand why. when I toast results I get x$x$x$x$ just like I would expect, it seems like the split may not be working? – CJG Apr 07 '15 at 01:25
  • "I cant use an AVD because I am on windows 10 and HAX doesn't work with Windows 10 so I am developing fro my cell phone. " -- so? Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you are able to run your app from your IDE on your phone, you can examine LogCat on your phone. – CommonsWare Apr 07 '15 at 10:50

1 Answers1

-1

I found the problem... Instead of this:

String[] controlString = resultString.split("$");

I had to use this:

String[] controlString = resultString.split(Pattern.quote("$"));
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
CJG
  • 19
  • 5
  • What the heck is pattern.quote() anyway? and why is it necessary to make Split work? – CJG Apr 07 '15 at 02:27
  • `split()` takes a regular expression as its parameter, as is described in [the documentation](http://developer.android.com/reference/java/lang/String.html#split%28java.lang.String%29). `$` is a special character in regular expressions, indicating a match with the end of a line, as is described in [the documentation](http://developer.android.com/reference/java/util/regex/Pattern.html). If you want to literally split on a `$` character, you can use `quote()` on `Pattern` to escape if for you. – CommonsWare Apr 07 '15 at 10:52