0
item1:100

item2:50

item3:200

I want to store separate item and price and store them in two different arrays: like in one array ,

item1

item2

item3

and in another array

100

50

200
4
  • You question is very similar to stackoverflow.com/questions/7925726/… , but you are using : as a delimiter. Also, another quesyion about string split on android: stackoverflow.com/questions/3732790/android-split-string Commented Aug 14, 2012 at 7:33
  • @Coral, I wouldn't call that first one a dupe since it's not really about how to split a string, it's more why ^ doesn't work when splitting. The second one is a much closer match. In fact, too close. I'll have to close-vote on this one as a dupe. Commented Aug 14, 2012 at 7:44
  • where is your input string stored ? Is this also a String object or is it coming from a file ? Commented Aug 14, 2012 at 7:46
  • @sunil string coming from text file stored on remote machine!! Commented Aug 17, 2012 at 2:37

5 Answers 5

2

You can try the following code:

String DELIMITER = ":";

ArrayList<String> arrItems = new ArrayList<String>();
ArrayList<Integer> arrPrices = new ArrayList<Integer>();

Then you can just split the string and store the relevant values in the arraylist as:

String[] strTemp = "item1:100".split(DELIMITER);
arrItems.put(strTemp[0]);
arrPirces.put(strTemp[1]);
Sign up to request clarification or add additional context in comments.

1 Comment

hey thanx buddy woking well!!
1

You can use split to separate a string based on another string (actually a regular expression):

String str = "item1:100";
String[] words = str.split (":");

From there, it's a simple matter of adding words[0] to one array and words[1] to the other.

The following complete program shows how it's done:

import java.util.ArrayList;
class Test {
    private static ArrayList<String> items = new ArrayList<String>();
    private static ArrayList<Integer> values = new ArrayList<Integer>();
    private static void addToArray (String str) {
        String[] words = str.split(":");
        items.add(words[0]);
        values.add(new Integer (words[1]));
    }
    public static void main(String[] args) {
        addToArray ("item1:100");
        addToArray ("item2:50");
        addToArray ("item3:200");

        for (int i = 0; i < items.size(); i++)
            System.out.println ("Item = " + items.get(i) +
                ", value = " + values.get(i));
    }
}

The output:

Item = item1, value = 100
Item = item2, value = 50
Item = item3, value = 200

Keep in mind I'm just doing simplistic string splitting here with no error handling code - you may want to add that if you value robust code, particularly if there's a possibility your input data may not be valid (eg, "item4:hello" will generate a java.lang.NumberFormatException).

Comments

1

You can use StringTokenizer class for your requirement.

StringTokenizer st = new StringTokenizer(yourString, ":");
arrayOne[0] = st.nextToken();
arrayTwo[0] = st.nextToken(); 

Comments

0

You can use split() method for string.

     String[] temp = yourString.split(":");

Try this.

Comments

0

Java provides the split function in strings. str.split(":") for "item:100" returns a String[] with the elements {"item1","100}. Now just iterate through the split arrays and build a combined array of the two parts.

Source: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.