| | |
Having trouble with comparing strings and arrays
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 5
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Oct 2004
Posts: 5
Reputation:
Solved Threads: 0
I figured I might as well post the code so i can get some other suggestions on it too...
Thanks again
Java Syntax (Toggle Plain Text)
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()); } }
Last edited by shdwdrgn517; Oct 27th, 2004 at 8:21 pm. Reason: Line of code not needed
![]() |
Similar Threads
- Help for comparing strings and copying strings please. (C++)
- Strings (Java)
- problem comparing strings (PHP)
- Error comparing strings from arrays (PHP)
Other Threads in the Java Forum
- Previous Thread: Newbie needs help with Streams pls
- Next Thread: Help with problem
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation awt binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer invokingapacheantprogrammatically j2me java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number object objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree webservices windows






