0

I have the following string array:

February_Report_files\customerst.rptdesign
February_Report_files\Top10Percent.rptdesign
February_Report_files\TopNPercent.rptdesign
March_Report_files\by_sup_ML.rptdesign
March_Report_files\chart__cwong.rptdesign
March_Report_files\HTML5 Chart.rptdesign

I want save report files(*.rptdesign) file in different string arrays according to different folder names .for example :

result[0]="customerst.rptdesign,Top10Percent.rptdesign,TopNPercent.rptdesign"

 result[1]="by_sup_ML.rptdesign,chart__cwong.rptdesign,HTML5 Chart.rptdesign"

how can I get this result ? could you give me some suggestions ?

0

3 Answers 3

1

Here is a quick code snippet:

public static void main (String[] args)
{
    /* Sample Space */
    String[] strArr = new String[] {
        "February_Report_files\\customerst.rptdesign",
        "February_Report_files\\Top10Percent.rptdesign",
        "February_Report_files\\TopNPercent.rptdesign",
        "March_Report_files\\by_sup_ML.rptdesign",
        "March_Report_files\\chart__cwong.rptdesign",
        "March_Report_files\\HTML5 Chart.rptdesign",
    };
    /* Sort Sample Space */
    Arrays.sort(strArr);

    /* Initialize Result ArrayList */
    List<String> resultList = new ArrayList<>();

    /* Initialize */
    String[] strInit = strArr[0].split("\\\\");
    String prefix = strInit[0];
    StringBuilder result = new StringBuilder(strInit[1]);
    for(int i = 1; i < strArr.length; i++) {
        /* Split Using Backslash */
        String[] strSplit = strArr[i].split("\\\\");
        if(strSplit[0].equals(prefix)) {
            /* Append */
            result.append("," + strSplit[1]);
        } else {
            /* Add Result To List */
            resultList.add(result.toString());
            /* Reset Prefix, Result Strings */
            prefix = strSplit[0];
            result = new StringBuilder(strSplit[1]);
        }
    }
    /* Add Last Entry To List */
    resultList.add(result.toString());

    /* Print The Results */
    for(int i = 0; i < resultList.size(); i++) {
        System.out.println(resultList.get(i));
    }
}

Output of this program:

customerst.rptdesign,Top10Percent.rptdesign,TopNPercent.rptdesign
by_sup_ML.rptdesign,chart__cwong.rptdesign,HTML5 Chart.rptdesign
Sign up to request clarification or add additional context in comments.

Comments

1

Below is the implementation with hashmap

for(int i=0;i<strarr.length;i++){
        boolean blnExists=false;
        String key = strarr[i].substring(0,strarr[i].indexOf("\\"));
        String value=strarr[i].substring(strarr[i].indexOf("\\")+1);

        blnExists = result.containsKey(key);
        if(blnExists){
            value=result.get(key) + ","+value;
        }
        else{
            result.put(key, value);
        }
        result.put(key, value);

    }

`

Comments

0

Use startsWith():

if(fileName[i].startsWith("March_Report_files"))
    saveItHere(fileName[i]);
else
    saveItThere(fileName[i]);

You might want to shorten your String afterwards with

fileName[i]=fileName[i].replaceFirst.("March_Report_files","");

For example:

public class StringTest {
    public static void main(String[] args){
        String text = "Hello\\World";
        if(text.startsWith("Hello\\"))
            text=text.replaceFirst("Hello\\\\","");
        System.out.println(text);
    }
}

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.