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

    Default Check if a string contains characters entered in a array

    I would determine if a string entered by the user contains only characters in the table lettresAleatoires. If a letter is different, it rejects what he wrote and asked to enter another string. Here's what I wrote until now, the code in the method verifierSiValide () is missing and this is the method I want to audit.

    Java Code:
       private final static char[] lettresAleatoires =
        {
            'R', 'B', 'O', 'V', 'J', 'F', 'I', 'N'
        };
     
       private static void demanderCombinaison() throws IOException
        {
            do
            {
                System.out.println("Entrez votre combinaison pour l'essai " + essai
                        + " <enter> pour arręter");
                CombinaisonEntree = r.readLine();
            } while (CombinaisonEntree.length() != 4);
        }
     
        private static void verifierSiValide()
        {
        }

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

    Default

    In verifierSiValide you could go through every character in the entered line and check whether it is present in lettresAleatoires.

    I don't think lettresAleatoires should be an array of chars. You could use a String in stead.

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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by venerik View Post
    I
    I don't think lettresAleatoires should be an array of chars. You could use a String in stead.
    Why not use an array?


    To the original poster, should the method verifierSiValide return void, or would it be better to return a boolean so you could use it's result elsewhere in your program?

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

    Default

    Quote Originally Posted by Fubarable View Post
    Why not use an array?
    It is not necessary to use an array of course but using a String and indexOf(characterToValidate) on that string makes neat code.

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

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Why not use an array?
    Because arrays are so Fortranesque and are soo old fashioned and impose 'mechical' step by step solutions. Regular expressions and Strings allow for a one line decision method instead of all those silly loops.

    kind regards,

    Jos

Similar Threads

  1. Replies: 13
    Last Post: 09-23-2010, 07:21 PM
  2. check string inside string
    By alacn in forum New To Java
    Replies: 8
    Last Post: 07-07-2010, 12:52 PM
  3. Replies: 1
    Last Post: 04-30-2010, 03:21 PM
  4. Creating Array of Characters From a String
    By ngc0202 in forum New To Java
    Replies: 6
    Last Post: 07-30-2009, 07:30 AM
  5. Replies: 2
    Last Post: 04-28-2008, 10:26 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
  •