3

I'm still new to Java and I would like to understand Strings and Arrays so I got this idea of manipulating elements and place them according to my objective. The objective is that there will be Array of Strings "ABBCCCBBAA" and the "AA","BB" must be replaced into "A" , "BA","AB" into CC. "CC","BC" into B. I basically have no idea how to make it happen but I know it must have Arrays of String. Please help

5
  • 4
    "ABBCCCBBAA" is a String , not an array . Commented Jun 3, 2013 at 16:09
  • May be you are looking for RegEx? Commented Jun 3, 2013 at 16:11
  • first step would be to get the logic of what you're trying to do. I must admin I'm not getting it... Commented Jun 3, 2013 at 16:12
  • For example, are you trying to make an algorithm where you want to replace every two chars in the input string like "AB"->"A", "BC"->"B", "CC"->"B" in one pass or do you want to do multiple passes and convert everything that matches "AA", "BB" etc into "A" in the first pass, then restart and replace things like "BA", "AB" with "B"... Commented Jun 3, 2013 at 16:22
  • exactly! sorry I was not able to explain carefully this is my first time to post a question and I'm not even sure if I have to use Arrays or is there anyway to do manipulate Strings like using .hasNext() Commented Jun 3, 2013 at 16:32

2 Answers 2

1

Regular expression can be very handy for you. Code bellow can do, your job with the use of regular expression:

   String mainStr = "ABBCCCBBAA";

    Pattern p = Pattern.compile("(AA)|(BB)|(BA)|(AB)|(CC)|(BC)");
    Matcher m = p.matcher(mainStr);

    while (m.find()) {
        String matchedStr = m.group(0);

        if("AA".equals(matchedStr) || "BB".equals(matchedStr)){
            mainStr = mainStr.replaceFirst(matchedStr,"X");
        }

        else if("BA".equals(matchedStr) || "AB".equals(matchedStr)){
            mainStr = mainStr.replaceFirst(matchedStr,"Y");
        }
        else if("CC".equals(matchedStr) || "BC".equals(matchedStr)){
            mainStr = mainStr.replaceFirst(matchedStr,"Z");
        }
    }
    mainStr = mainStr.replaceAll("X","A").replaceAll("Y","CC").replaceAll("Z","B");
    System.out.println(mainStr);

Above code will handle your case of multiple occurrence of same pattern in a given string like:

ABBCCCBBAABBBBAA

will generate output:

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

2 Comments

Thank you for giving me another idea. We really have great and kind people here, awesome community!! :))
And this was my 100th answer... :)
1

I am assuming that by "array of strings" you mean:

String[] myvariable = new String[number];
myvariable[0] = "ABBCCBBAA";
myvariable[1] = "some_other_string";

If you are new to Java I suggest you read a beginner's book like Head First Java and also look into java documentation; you don't even have to go that far if you are programming with a decent IDE, like Netbeans (thanks to its intelli-sense feature) is a source of documentation for what you seek (meaning that you can look at all the methods available for a string, read what they do, and see if they can help accomplish what you need).

I am assuming (from what you have said) that you want to replace "AA" for "A", and from that result replace "BB" for "BA", and from that result replace "AB" into "CC", and from that result "BC" into "B".

The code I am posting is REAL simple, and it will only work for this particular case (as I have understood it), if you want to create a method that does this for any string, you need to change some things, but I'll leave that to you.

String[] yourArrayOfStrings = new String[1];
yourArrayOfStrings[0] = "ABBCCBBAA";

String resultOfReplacement= yourArrayOfStrings[0].replaceFirst("AA", "A");
System.out.println(resultOfReplacement); //debugging purposes

resultOfReplacement = resultOfReplacement.replaceFirst("BB", "BA");
System.out.println(resultOfReplacement); //debugging purposes

resultOfReplacement = resultOfReplacement.replaceFirst("AB", "CC");
System.out.println(resultOfReplacement); //debugging purposes

resultOfReplacement = resultOfReplacement.replaceFirst("BC", "BB");
System.out.println(resultOfReplacement); //debugging purposes

The only reason why I created a String[] was because that's what you stated in your question, otherwise I would have simple created a String variable like I did with resultOfReplacement. To access the first element in an array you do arrayVariable[index]. Here I use the replaceFirst function that comes with Java for variables of type String. If you look the method up, it'll tell you that it will look for the first match of the first parameter and replace it with the second parameter.

The System.out.println I have added are for debugging purposes, so you can see on the console what is clearly happening with each replacement. So, the first time I call replaceFirst(...) on the original string which is a[0].

This will happen:

The method will look in "ABBCCBBAA" for the FIRST AND ONLY THE FIRST time "AA" appears and replace it with "A". The result is "return" and you must assign it to a variable if you want access to it to do more actions upon it. In this case, I assign it to a new String variable. You could have just assigned back to a[0], which is likely what you want. (You'd do so like this: a[0]=ourArrayOfStrings[0].replaceFirst("AA", "A");)

For the second replacement, the method will look in "ABBCCBBA" for the first time "BB" appears and replace it for "BA".

See the pattern? This is just a start, and depending on what you want you might need other methods like replaceAll().

Most IDEs will tell you what methods are available for a variable when you access it via ".", so that when you are typing " variablename. " right at that moment a list of methods available for it should appear, if they don´t you can go ahead and do a shortcut like ctrl+space for it to appear and navigate through the methods via the arrow keys so you can read what they do (at least for Eclpise and Netbeans, while programming in Java, it works). Documentation is power!

2 Comments

woah I did no expect a very very decent and comprehensive answer from a person here. Thank you so much I'll will follow your advice.
The answer I gave is very very very simplistic, but I figured that if you are new that's what you need. I do recommend reading the book (or an Action book) and getting to know your IDE. Java is powerful, and you can create very good, clean and reusable code with it. Cheers!

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.