Results 1 to 2 of 2
  1. #1
    roud9 is offline Member
    Join Date
    Sep 2010
    Posts
    20
    Rep Power
    0

    Default Compare two strings

    Hello,

    I wrote a little program that lets you compare the characters and display the number that are well or poorly placed, but I would like if there is a character in string1 that is not in string2, it is ignored. For example if string1: ABCD and string2 : ANBD, I want the program to tell me "2 well placed and 1 misplaced", N being ignored because it is not in the two chains. Here is what I have now:

    Java Code:
    public class Main
    {
     
        static String chaine1 = "ABCD";
        static String chaine2 = "BACD";
        static int compteurBienPlaces;
        static int compteurMalPlaces;
     
        public static void main(String[] args)
        {
            for (int i = 0; i < chaine1.length(); i++)
            {
                char[] tableauChaine1 = chaine1.toCharArray();
                char[] tableauChaine2 = chaine2.toCharArray();
     
                if (tableauChaine1[i] == tableauChaine2[i])
                {
                    System.out.println("Le caractère " + tableauChaine1[i] + " est"
                            + " bien placé");
                    compteurBienPlaces++;
                }
                else if (tableauChaine1[i] != tableauChaine2[i])
                {
                    System.out.println("Le caractère " + tableauChaine1[i] + " est"
                            + " mal placé");
                    compteurMalPlaces++;
                }
            }
            System.out.println("Le nombre de caractères bien placés est de "
                    + compteurBienPlaces + " et " + compteurMalPlaces + " sont mal "
                    + "placés.");
        }
    }
    Thanks

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    I don't think you have to convert the Strings to character arrays. The String class has two neat methods you could use to solve your problem: charAt() and indexOf().

    Hope this helps,
    Erik
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. compare the remaining of Strings for equality
    By Cylab in forum New To Java
    Replies: 5
    Last Post: 08-19-2010, 03:42 PM
  2. compare the remaining of Strings for equality
    By Cylab in forum Advanced Java
    Replies: 6
    Last Post: 08-19-2010, 03:40 PM
  3. Compare between 2 Strings
    By ChaosINC in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 12:39 PM
  4. [SOLVED] Cant compare strings sent by client
    By christuart in forum Networking
    Replies: 15
    Last Post: 09-01-2008, 04:24 AM
  5. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 04:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •