Having trouble with comparing strings and arrays

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 5
Reputation: shdwdrgn517 is an unknown quantity at this point 
Solved Threads: 0
shdwdrgn517 shdwdrgn517 is offline Offline
Newbie Poster

Having trouble with comparing strings and arrays

 
1
  #1
Oct 27th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: shdwdrgn517 is an unknown quantity at this point 
Solved Threads: 0
shdwdrgn517 shdwdrgn517 is offline Offline
Newbie Poster

Re: Having trouble with comparing strings and arrays

 
0
  #2
Oct 27th, 2004
I figured I might as well post the code so i can get some other suggestions on it too...
  1. import cs1.Keyboard;
  2. import java.util.ArrayList;
  3.  
  4. public class Combinations {
  5.  
  6. public static void main(String[] args)
  7. {
  8. System.out.println("Enter 5 or less characters to be rearranged:");
  9. String str = Keyboard.readString();
  10. int check = 0;
  11. int not=0;
  12. char[] ch2 = {'a','b'};
  13. char[] ch3 = {'a','b','c'};
  14. char[] ch4 = {'a','b','c','d'};
  15. char[] ch5 = {'a','b','c','d','e'};
  16. ArrayList combos = new ArrayList();
  17. if (str.length()==1)
  18. combos.add(str);
  19. if (str.length()==2)
  20. {
  21. for(int x = 0; x<2;x++)
  22. for (int y = 0; y<2; y++)
  23. {
  24. if(x!=y)
  25. {
  26. ch2[0] = str.charAt(x);
  27. ch2[1] = str.charAt(y);
  28. if( combos.size()!=0)
  29. while (check<combos.size())
  30. {
  31. if (String.valueOf(ch2) == String.valueOf(combos.get(check)))
  32. not = 1;
  33. check++;
  34. }
  35. if (not != 1)
  36. combos.add(String.valueOf(ch2));
  37. check = 0;
  38. not = 0;
  39. }
  40. }
  41. }
  42. if (str.length()==3)
  43. {
  44. for(int x = 0; x<3;x++)
  45. for(int y = 0; y<3;y++)
  46. for (int z = 0;z<3;z++)
  47. {
  48. if (x!=y && x!=z && y!=z)
  49. {
  50. ch3[0] = str.charAt(x);
  51. ch3[1] = str.charAt(y);
  52. ch3[2] = str.charAt(z);
  53. if (combos.size()!=0)
  54. while(check<combos.size())
  55. {
  56. if (String.valueOf(ch3) == String.valueOf(combos.get(check)))
  57. not = 1;
  58. check++;
  59. }
  60. if(not!=1)
  61. combos.add(String.valueOf(ch3));
  62. check =0;
  63. not = 0;
  64. }
  65. }
  66. }
  67. if (str.length()==4)
  68. {
  69. for(int x = 0; x<4;x++)
  70. for(int y = 0; y<4;y++)
  71. for (int z = 0;z<4;z++)
  72. for (int a = 0; a<4;a++)
  73. if (x!=y && x!=z && x!=a && y!=z && y!=a && z!=a)
  74. {
  75. ch4[0] = str.charAt(x);
  76. ch4[1] = str.charAt(y);
  77. ch4[2] = str.charAt(z);
  78. ch4[3] = str.charAt(a);
  79. if (combos.size()!=0)
  80. while(check<combos.size())
  81. {
  82. if (String.valueOf(ch4) == String.valueOf(combos.get(check)))
  83. not = 1;
  84. check++;
  85. }
  86. if(not!=1)
  87. combos.add(String.valueOf(ch4));
  88. check =0;
  89. not = 0;
  90. }
  91. }
  92. if (str.length()==5)
  93. {
  94. for(int x = 0; x<5;x++)
  95. for(int y = 0; y<5;y++)
  96. for (int z = 0;z<5;z++)
  97. for (int a = 0; a<5;a++)
  98. for (int b = 0; b<5; b++)
  99. if (x!=y && x!=z && x!=a && x!=b && y!=z && y!=a && y!=b && z!=a && z!=b && a!=b)
  100. {
  101. ch5[0] = str.charAt(x);
  102. ch5[1] = str.charAt(y);
  103. ch5[2] = str.charAt(z);
  104. ch5[3] = str.charAt(a);
  105. ch5[4] = str.charAt(b);
  106. if (combos.size()!=0)
  107. while(check<combos.size())
  108. {
  109. if (String.valueOf(ch5) == String.valueOf(combos.get(check)))
  110. not = 1;
  111. check++;
  112. }
  113. if(not!=1)
  114. combos.add(String.valueOf(ch5));
  115. check =0;
  116. not = 0;
  117. }
  118. }
  119. for (int x = 0; x<combos.size(); x++)
  120. System.out.println(combos.get(x));
  121. System.out.println("Number of combinations:" + combos.size());
  122. }
  123.  
  124. }
Thanks again
Last edited by shdwdrgn517; Oct 27th, 2004 at 8:21 pm. Reason: Line of code not needed
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Having trouble with comparing strings and arrays

 
1
  #3
Oct 27th, 2004
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: shdwdrgn517 is an unknown quantity at this point 
Solved Threads: 0
shdwdrgn517 shdwdrgn517 is offline Offline
Newbie Poster

Re: Having trouble with comparing strings and arrays

 
0
  #4
Oct 27th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: shdwdrgn517 is an unknown quantity at this point 
Solved Threads: 0
shdwdrgn517 shdwdrgn517 is offline Offline
Newbie Poster

Re: Having trouble with comparing strings and arrays

 
0
  #5
Oct 27th, 2004
Nvm, I apparently didn't try .equals(), thought I did. Thanks anyway to those who tried to help
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC