0

This is some data. and i want to split this data in two parts. first is year (ex. 913) and second is information about that specific year. and then i want to store this data in treemap as key-value pair. i have tried to do this thing using split function but it is not giving me satisfactory result. so please guide me through this. Thanks in advance.

This is my code.

String open = "event";                                                                       
String close = "birth;
String text = "";
int start = data.indexOf(open);
if (start != -1)
{
     int end = data.indexOf(close, start + open.length());
      if (end != -1)
     {
              text = data.substring(start + open.length(), end);
               text = text.replace("==","");
            String[] words = text.split("–");
               for(String w : words)
                {
                        Log.d("trace w", w.trim());
                }
      }                                                                                                       
}


AD 70 – Emperor Alexander III dies of exhaustion while playing the game tzykanion (Byzantine name for polo). He is succeeded by his 8-year-old nephew Constantine VII.
1513 – Italian Wars: Battle of Novara. Swiss troops defeat the French under Louis II de la Trémoille, forcing the French to abandon Milan. Duke Massimiliano Sforza is restored.
1523 – Gustav Vasa, the Swedish regent, is elected King of Sweden, marking a symbolic end to the Kalmar Union. This is the Swedish national day.
1586 – Francis Drake's forces raid St. Augustine in Spanish Florida.
1644 – The Qing dynasty Manchu forces led by the Shunzhi Emperor capture Beijing during the collapse of the Ming dynasty.
1654 – Queen Christina abdicates the Swedish throne and is succeeded by her cousin Charles X Gustav.
1674 – Shivaji, founder of the Maratha Empire, is crowned.
1749 – The Conspiracy of the Slaves in Malta is discovered.
1762 – Seven Years' War: British forces begin a siege of Havana, Cuba, and temporarily capture the city in the Battle of Havana.
1808 – Napoleon's brother, Joseph Bonaparte, is crowned King of Spain.
1809 – Sweden promulgates a new Constitution, which restores political power to the Riksdag of the Estates after 20 years of enlightened absolutism. At the same time, Charles XIII is elected to succeed Gustav IV Adolf as King of Sweden.
1813 – War of 1812: Battle of Stoney Creek: A British force of 700 under John Vincent defeats an American force twice its size under William Winder and John Chandler.
1822 – Alexis St. Martin is accidentally shot in the stomach, leading to William Beaumont's studies on digestion.
1832 – The June Rebellion in Paris is put down by the National Guard.
1844 – The Young Men's Christian Association (YMCA) is founded in London.
1844 – The Glaciarium, the world's first mechanically frozen ice rink, opens.
3
  • 1
    i have tried to do this ... can you show us your code? Commented Jun 6, 2018 at 4:08
  • looks like you can split on " - " Commented Jun 6, 2018 at 4:10
  • yes i can split on " - " but it gives me below result. 913 Emperor Alexander III dies of exhaustion while playing the game tzykanion (Byzantine name for polo). He is succeeded by his 8-year-old nephew Constantine VII. 1513 1513 is a part of second string. i want to put 1513 as new element in array list Commented Jun 6, 2018 at 4:30

5 Answers 5

1

A java 8 alternative:
List<String> list= Stream.of(yourString.split("–")).collect(Collectors.toList());

Sign up to request clarification or add additional context in comments.

Comments

0
ArrayList<String> list= Arrays.asList(Your_String.split("delimiter"));

Comments

0

If your data is somewhat constant in its format, you could find the first instance of "–". From there you can easily substring each line.

Like this:

String s = "AD 70 – Emperor Al... Constantine VII.";
String y = s.substring(0, s.indexOf('–')).trim();
String info = s.substring(s.indexOf('–') + 1).trim();

If you do it recursively, you can then add the key and the values to your treemap.

Not sure if this is the most elegant solution, but it works.

A.

3 Comments

The advantage of not splitting on " – " is that if your data contains the character multiple times, you will not end up with more than 2 strings.
data is not constant. it can be vary.
But is it always in the form "stuff – more stuff"?
0

Below is the steps you can follow to achieve above requirement.

  1. Split the string by "-".
  2. Store into treemap collection.

Below is the example.

 public static void convertSplitStringIntoTreeMap() {
    List<String> listOfString = new ArrayList<String>();
    String str = "2012 - This is for testing";
    String str2 = "2013 - This is for testing2";
    listOfString.add(str);
    listOfString.add(str2);

    TreeMap<Integer, String> tmap = new TreeMap<Integer, String>();

    for (String st : listOfString) {
        String[] splitString = st.split("-");
        tmap.put(Integer.parseInt(splitString[0].trim()), splitString[1]);
    }

    System.out.println(tmap);
}

Comments

0

I would use a Scanner from your data.

    Scanner dataInput = new Scanner(data);
    Map<String, String> dataMap = new HashMap<>();

    String input = dataInput.nextLine();
    while (!input.isEmpty()) {
        String[] spliting = input.split("(?=–)", 2);
        dataMap.put(spliting[0], spliting[1]);
        input = dataInput.nextLine();
    } 

    dataMap.forEach((k,v) -> System.out.println(k + v));

the value will remain with the –.

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.