-4

I have some text

"Summary : Daily Monthly Yearly"
"Amount : $1,401,508,225.38  $34,132,889,672.53  $334,088,690,177.34"

My question here is that how do i store this amounts in their respective strings i.e. Daily, Monthly, Yearly ?

Barranka
  • 20,547
  • 13
  • 65
  • 83
  • 1
    possible duplicate of [How to split a String in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – PakkuDon Aug 23 '14 at 04:29
  • 3
    Your question is not answerable as asked. What do you mean by *store*? Do you want to associate them somehow as in a Map? Or do you want to concatenate strings? – merlin2011 Aug 23 '14 at 04:29
  • Do you have arrray for summary and amount? – Nabin Aug 23 '14 at 04:30
  • @Nabin yes i have all the three amounts in String[] – Ankush Minda Aug 23 '14 at 04:32
  • And where do you have Daily, Monthly and Yearly? In string or string[]? – Nabin Aug 23 '14 at 04:34
  • @Nabin yes in String[] – Ankush Minda Aug 23 '14 at 04:35
  • @Nabin How do i store Daily, Monthly and Yearly in String[] ? Could you plz tell me? – Ankush Minda Aug 23 '14 at 04:37
  • Have you read the link provided by @PakkuDon? I've just read it, and the answer is right there. – Barranka Aug 23 '14 at 04:38
  • @Barranka actually at that link it is dividing a string in two parts..but here i need to store this details to a String[] – Ankush Minda Aug 23 '14 at 04:39
  • 1
    [Read the documentation for `split`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)) or test it in your code and debug it. Just because the example provided in the question deals with splitting the string in two parts doesn't mean it can't work with more parts (after all, it returns an array). Also, I recommend you [read this article](http://whathaveyoutried.com) – Barranka Aug 23 '14 at 04:43
  • @AnkushMinda See the answer below and for details, follow the link provided by PakkuDon – Nabin Aug 23 '14 at 04:49
  • Welcome to Stack Overflow, please take the [Tour](http://stackoverflow.com/tour). What have you tried so far? Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – DavidPostill Aug 23 '14 at 07:26

2 Answers2

0

As per your comment you said you have array of Strings that contains three amounts and as per your question, you want to store in respective Strings. You can use for loop and store in HashMap.

First get your time in array as following:

String textWithTime = "Daily Monthly Yearly";

String[] timearray = textWithTime.split(" ");//note the space inside double quotation

If you have amount of money in a string, then do the following as well

String[] amountarray = textWithAmount.split(" ");//note the space inside double quotation

Do as following:

//Make an object of HashMap here
HashMap<String, String> hashMap = new HashMap<String, String>();

for(int i=0;i<stringarray.length();i++){
  hashMap.put(timearray[i], amountarray[i]); 
}

And to retrive the key and values back you can do the follow:

for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    //Use these key and values
}
Nabin
  • 11,216
  • 8
  • 63
  • 98
-1

If your question is about how to split strings, see split() method of String class. If it is more about how to structure resulting data, you may want to do as below:

Create a enum and a class:

public enum Period {
   DAILY,
   MONTHLY,
   YEARLY;
}

public class Income {
 private Period p;
 private double money;

 public Income (Period p, double money) {
    this.p = p;
    this.money = money;
  }
  ....

  // getters, etc
}

then you could use it like this:

Income daily = new Income (Period.DAILY, 1401508225.38);
javabrew
  • 306
  • 4
  • 7