5

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 "; 
bakoyaro
  • 2,550
  • 3
  • 36
  • 63
chuck finley
  • 459
  • 3
  • 8
  • 16

8 Answers8

7

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
Community
  • 1
  • 1
Naveed
  • 41,517
  • 32
  • 98
  • 131
  • 1
    its not homework its last year project that I am working on again for practice. – chuck finley Jun 02 '11 at 10:26
  • 3
    @sibghatuk - the same principle applies. You will **learn** more if we give you hints and you work out the complete answer for yourself. That is what you are trying to do ... isn't it? – Stephen C Jun 02 '11 at 10:33
4

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

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
2

I would not perform the comparison on the raw strings. Instead, first convert each String to a List<Integer> using String.split() and Integer.parseInt() on each result. Then sort() the lists into ascending order, and then it becomes very easy to compare them.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
2

Try like this.

String one = "1 2 3  4 5 ";
String two = " 3 2 1 4 5 ";
Set<String> a = new HashSet<String> (Arrays.asList(one.trim().replaceAll("\\s*"," ").split(" ")));
Set<String> b = new HashSet<String> (Arrays.asList(two.trim().replaceAll("\\s*"," ").split(" ")));
boolean ret = (a.size() == b.size()) && a.containsAll(b);
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
0

You could tokenize/split the strings based on the spaces, then loop through the resulting tokens which would be the numbers themselves.

f1dave
  • 1,267
  • 4
  • 20
  • 31
0

You split the strings on whitespace (using either String.split or StringTokenizer) and then convert each of the tokens into a number. Place all the numbers in string one into a HashSet. Do the same for string two. Now all you have to do is to check whether each entry in the first HashSet also occurs in the second one.

MAK
  • 26,140
  • 11
  • 55
  • 86
  • MAK - The OP mentions the possibility of duplicates. This means that your solution won't always work. – Stephen C Jun 02 '11 at 10:36
  • @Stephen C: That's true. But he can always check for duplicates while inserting into the set. The question looked like homework, so I didn't want to give too much away. – MAK Jun 02 '11 at 17:57
0

I would at first parse numbers as integers, put them to the set and compare them:

  import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;


public class StringsCompare {

    private static String one = "1 2 3 4 5 6"; 
    private static String two = " 3 2 1 5 6 4 ";

    public static void main(String[] args) {
        StringsCompare sc = new StringsCompare();

        System.out.println(sc.compare(one, two));
    }

    private boolean compare(String one, String two) {

        SortedSet<Integer> setOne = getSet(one);    
        SortedSet<Integer> setTwo = getSet(two);

        return setOne.equals(setTwo);
    }

    private SortedSet<Integer> getSet(String str) {
        SortedSet<Integer> result = new TreeSet<Integer>();

        StringTokenizer st = new StringTokenizer(str, " ");

        while (st.hasMoreTokens()) {
            result.add(Integer.valueOf(st.nextToken()));
        }

        return result;
    }
}
Jiří Šitina
  • 306
  • 4
  • 10
-1

Try to parse the strings in int.

Integer.parseInt(One).intValue() == Integer.parseInt(two).intValue()

I'm not sure what you're trying to do but my guess is that you'd better to use arrays.

patapizza
  • 2,398
  • 15
  • 14