I'm trying to compare a string to an ArrayList Object. But no matter how I try to compare it, such as just ==, or .equals(), or .compareTo(), or String.valueOf(ArrayList.get()) with all of the above options, it won't compare correctly, even if they are equal when outputted they aren't equal when compared. I can post the whole code if you want me to, it is to have a word <= 5 characters input and rearranged every way possible but not outputting duplicates. I can get the every possible output part but I can't seem to compare the string to an ArrayList object. Thanks in advance for your help.

I figured I might as well post the code so i can get some other suggestions on it too...

import cs1.Keyboard;
import java.util.ArrayList;

public class Combinations {

    public static void main(String[] args) 
    {
        System.out.println("Enter 5 or less characters to be rearranged:");
        String str = Keyboard.readString();
        int check = 0;
        int not=0;
        char[] ch2 = {'a','b'};
        char[] ch3 = {'a','b','c'};
        char[] ch4 = {'a','b','c','d'};
        char[] ch5 = {'a','b','c','d','e'};
        ArrayList combos = new ArrayList();
        if (str.length()==1)
            combos.add(str);
        if (str.length()==2)
        {
            for(int x = 0; x<2;x++)
                for (int y = 0; y<2; y++)
                {
                    if(x!=y)
                    {
                        ch2[0] = str.charAt(x);
                        ch2[1] = str.charAt(y);
                        if( combos.size()!=0)
                            while (check<combos.size())
                            {
                                if (String.valueOf(ch2) == String.valueOf(combos.get(check)))
                                    not = 1;
                                check++;
                            }
                        if (not != 1)
                            combos.add(String.valueOf(ch2));
                        check = 0;
                        not = 0;
                    }
                }
        }
        if (str.length()==3)
        {
            for(int x = 0; x<3;x++)
                for(int y = 0; y<3;y++)
                    for (int z = 0;z<3;z++)
                    {
                        if (x!=y && x!=z && y!=z)
                        {
                            ch3[0] = str.charAt(x);
                            ch3[1] = str.charAt(y);
                            ch3[2] = str.charAt(z);
                            if (combos.size()!=0)
                                while(check<combos.size())
                                {
                                    if (String.valueOf(ch3) == String.valueOf(combos.get(check)))
                                        not = 1;
                                    check++;
                                }
                            if(not!=1)
                                combos.add(String.valueOf(ch3));
                            check =0;
                            not = 0;
                        }
                    }
        }
        if (str.length()==4)
        {
            for(int x = 0; x<4;x++)
                for(int y = 0; y<4;y++)
                    for (int z = 0;z<4;z++)
                        for (int a = 0; a<4;a++)
                            if (x!=y && x!=z && x!=a && y!=z && y!=a && z!=a)
                            {
                                ch4[0] = str.charAt(x);
                                ch4[1] = str.charAt(y);
                                ch4[2] = str.charAt(z);
                                ch4[3] = str.charAt(a);
                                if (combos.size()!=0)
                                    while(check<combos.size())
                                    {
                                        if (String.valueOf(ch4) == String.valueOf(combos.get(check)))
                                            not = 1;
                                        check++;
                                    }
                            if(not!=1)
                                combos.add(String.valueOf(ch4));
                            check =0;
                            not = 0;
                            }
        }
        if (str.length()==5)
        {
            for(int x = 0; x<5;x++)
                for(int y = 0; y<5;y++)
                    for (int z = 0;z<5;z++)
                        for (int a = 0; a<5;a++)
                            for (int b = 0; b<5; b++)
                                if (x!=y && x!=z && x!=a && x!=b && y!=z && y!=a && y!=b && z!=a && z!=b && a!=b)
                                {
                                    ch5[0] = str.charAt(x);
                                    ch5[1] = str.charAt(y);
                                    ch5[2] = str.charAt(z);
                                    ch5[3] = str.charAt(a);
                                    ch5[4] = str.charAt(b);
                                    if (combos.size()!=0)
                                    while(check<combos.size())
                                    {
                                        if (String.valueOf(ch5) == String.valueOf(combos.get(check)))
                                            not = 1;
                                        check++;
                                    }
                                    if(not!=1)
                                        combos.add(String.valueOf(ch5));
                                    check =0;
                                    not = 0;
                                }
        }
        for (int x = 0; x<combos.size(); x++)
            System.out.println(combos.get(x));
        System.out.println("Number of combinations:" + combos.size());
    }
    
}

Thanks again

>I'm trying to compare a string to an ArrayList Object.
This is your problem. Unless you can figure out a way to convert the ArrayList to a String so that they are comparable, you'll need to manually compare each item individually in a loop.

ArrayLists have a .toString method but it calls the String.valueOf() method to translate and I've tried the .valueOf() so I figured the toString wouldn't work any better.

Nvm, I apparently didn't try .equals(), thought I did. Thanks anyway to those who tried to help

This article has been dead for over six months. Start a new discussion instead.