0

Ok so I have looked at this (question asking how to split a string) however the answer isn't really relevant to my question.

The user will input a weight which is stored in the sqlite DB but I also want the number to show in a TextView below where it was just entered (as the app keeps track of weights over a period of 7 days).

When I try and get the String from my DB its stored as a long String and what I want to do is split that String (I hope I'm making sense!).

I have the following method;

public String[] getWeight() {

    String selectQuery = "SELECT " + DowncroftContract.WEIGHT_VALUE + " FROM " + DowncroftDatabase.WEIGHT_TABLE; //+ " WHERE " + DowncroftContract.WEIGHT_DOGS_ID + " = " + str_dogsId + " ORDER BY " + DowncroftContract.WEIGHT_DATE + " ASC;";
    SQLiteDatabase db = downcroftDatabase.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    String[] data = null;

    if (cursor.moveToFirst()) {
        do {
            results = cursor.getString(cursor.getColumnIndex(DowncroftContract.WEIGHT_VALUE + ""));
            String[] splitString  = results.split("");
            String split1 = splitString[1];
            String split2 = splitString[2];
            DayOne.append(split1);
            DayTwo.append(split2);
        } while (cursor.moveToNext());

    }
    cursor.close();
    return data;
}

Now the above method will split the string up to single figures but I cant seem to figure out how to split the string so that its splitting it for every two figures.

E.G User enters 20 presses enter - it then drops down into a TextView called DayOne.

The following day the users enters 24 and presses enter- that then drops down into a TextView called DayTwo.

I think I need an array with possibly a for loop however I am wondering if it is possible to achieve what I want by tweaking what I already have?

Community
  • 1
  • 1
Display name
  • 730
  • 2
  • 8
  • 23

2 Answers2

1

You just mean you want to split a String every 2 characters?

Like you have string 123456 and you want 12, 34, 56?

Try this:

String[] split = result.split("(?<=\\G..)");

This is gonna split your String every 2 characters. \G asserts the position after previous match (or the start of the String if there's not previous match) followed by 2 characters.

Shadov
  • 5,421
  • 2
  • 19
  • 38
  • Yes, you are correct, however when I make the changes as you've suggested, I get the following error; java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. However I have initialized the cursor as you can see in my code above? – Display name Oct 06 '16 at 18:39
  • There's no way change from this post affected this, you either had something wrong before or you accidentally changed something. I'm not sure if you are working with cursor correctly, don't remember it really well, sorry. – Shadov Oct 06 '16 at 19:13
  • The `(?<=\\G..)` pattern does not work in Android (ICU flavor), only in Java (`java.util.regex`). – Wiktor Stribiżew Oct 06 '16 at 20:28
0

[Guy above did basically the same thing, only saw on refresh, snippet will probably still be useful though..]

The part that is splitting your string is String[] splitString = results.split("");

This splits after every "nothing", so in essence, after every character split the string.

Here's a little code snippet I worked together for you...

import java.util.Arrays; //only essential for the Arrays toString bit...

public class Main {

    //Static so it's usable anywhere..
    public static String[] splitStringBy(int everyXLetter, String stringCode)
    {
        // Split the given string by regex, every Xth letter...
        String[] splitIntoSingleElements = stringCode.split("(?<=\\G.{"+everyXLetter+"})");
        // Return it
        return splitIntoSingleElements;
    }

    public static void main(String[] args)
    {
        //Set some string text...
        String text = "askfjaskfjasf";
        //Split and store the string using the above function
        String[] splitText = splitStringBy(2, text);

        //Return it any way you like
        System.out.println(Arrays.toString(splitText)); // [as, kf, ja, sk, fj, as, f]
        System.out.println(splitText[0]); // as
    }
}