944,164 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7552
  • Java RSS
Oct 27th, 2004
1

Having trouble with comparing strings and arrays

Expand Post »
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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
shdwdrgn517 is offline Offline
5 posts
since Oct 2004
Oct 27th, 2004
0

Re: Having trouble with comparing strings and arrays

I figured I might as well post the code so i can get some other suggestions on it too...
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
shdwdrgn517 is offline Offline
5 posts
since Oct 2004
Oct 27th, 2004
1

Re: Having trouble with comparing strings and arrays

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 27th, 2004
0

Re: Having trouble with comparing strings and arrays

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
shdwdrgn517 is offline Offline
5 posts
since Oct 2004
Oct 27th, 2004
0

Re: Having trouble with comparing strings and arrays

Nvm, I apparently didn't try .equals(), thought I did. Thanks anyway to those who tried to help
Reputation Points: 11
Solved Threads: 0
Newbie Poster
shdwdrgn517 is offline Offline
5 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Newbie needs help with Streams pls
Next Thread in Java Forum Timeline: Help with problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC