Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two strings of ints in java

I have two Strings that contain numbers and I want to see if the second string contains the same numbers as the first String, whether they are in order or not. If it has any number repeating than report false. Is there anyway in java other than using .charAt() because its not working for number after 10?

String one = "1 2 3 4 5 "; 
String two = " 3 2 1 4 5 ";
String three = "3 2 1 4 4 "; 
like image 759
chuck finley Avatar asked Jun 02 '11 10:06

chuck finley


2 Answers

Looks like homework. So these steps you can follow:

  • Trim both strings
  • Convert both strings into ArrayList using space separator
  • Sort both arrays numerically
  • Compare both arrays
like image 50
Naveed Avatar answered Sep 30 '22 08:09

Naveed


You can use Scanner.nextInt() to read numbers from the string, add them to a Set, and see if set1.equals(set2) is true.

like image 31
Denis Tulskiy Avatar answered Sep 30 '22 09:09

Denis Tulskiy