1

My storage string gives me all the digits in want besides the last, I know its because the last digit has nothing to compare to the right. Can I add the last digit to the end of my string somehow,

    for(int i = 0;i < othercontent.length -1 ;i++ )
    {
        if(othercontent[i] != othercontent[i + 1])
        {
            storage = storage + othercontent[i]; 

        }
    }
6
  • What you want to accomplish at the end? Commented Sep 18, 2013 at 11:26
  • 1
    Can you please give a sample input/output? Commented Sep 18, 2013 at 11:26
  • I have sorted and now I am getting rid of all the duplicates in the array Commented Sep 18, 2013 at 11:27
  • the whole program is huge so its kinda hard to give an input output example but say AABBBCCC will go in AB will come out I want ABC Commented Sep 18, 2013 at 11:28
  • I am only trying to get rid of duplicates not the last element and I cant use the API methods Commented Sep 18, 2013 at 11:30

6 Answers 6

1
for(int i = 0; i < othercontent.length ;i++ )
{
    if(i == 0 || othercontent[i] != othercontent[i - 1])
    {
        storage = storage + othercontent[i]; 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am little wary to add additional checks inside the loop. Instead, you can push the 0th iteration outside.
1

if othercontent is String array :

TreeSet<String> set = new TreeSet<>(Arrays.asList(othercontent));
othercontent = set.toArray(new String[0]);
for (String string : othercontent) {
    System.out.println(string);
}

if othercontent is String :

String othercontent = "ZZZZQQWEDDODRAABBNNNNO";
LinkedList<Character> list = new LinkedList<>();
for (Character character : othercontent.toCharArray()) {
    list.add(character);
}
TreeSet<Character> set = new TreeSet<>(list);
StringBuilder builder = new StringBuilder();
for (Character character : set) {
    builder.append(character);
}

System.out.println(builder.toString());

not only sorting , but also removing dublicates are solved with this code

OUTPUT :

ABDENOQRWZ

Comments

0

You can check if you reached the last element:

for(int i = 0;i < othercontent.length -1; i++ ) {
    if(othercontent[i] != othercontent[i + 1]) {
        storage = storage + othercontent[i]; 
    }
    //only gets executed if the last iteration is reached
    if(i==othercontent.length-2) {
        storage = storage + othercontent[i+1];
    }
}

Or, instead of using a condition, just write this after your loop:

storage = storage + othercontent[othercontent.length-1];

1 Comment

You don't need to add additional condition inside the loop, instead you can just add the last char outside the loop (with some additional checks if needed). something like storage = storage + othercontent[othercontent.length-1];
0

You can add the last digit to your string outside of for loop as it doesn't require to check any condition

 for(int i = 0;i < othercontent.length -1; i++ ) {
    if(othercontent[i] != othercontent[i + 1]) {
        storage = storage + othercontent[i]; 
    }

}

 storage = storage + othercontent[othercontent.length - 1];

Comments

0
for(int i = 0; i < othercontent.length -1 ; ++i )    {
    if(othercontent[i] != othercontent[i + 1]) {
        storage = storage + othercontent[i];
    }
}
if(othercontent.length>0){
    storage = storage + othercontent[othercontent.length-1];
}

Comments

0

If you are checking for the duplicates, you should do something like this outside the loop.

if(othercontent.length>0 && storage[storage.length-1] ! = othercontent[othercontent.length-1])
{
    storage = storage+othercontent[othercontent.length-1];
}

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.