1. Tes Deals
Groups

    java programmer, whitespaces matching

    currently having a major problem with whitespaces and it detects a whitespace only if the keyboard if pressed once and if you press it more than once it does not work. i have setup a do while loop to try and contain invalid data but its only working for characters and not white spaces...help would be greatly thanked.

    do{
    /*if salesPersonName is null or just spaces then this
    piece of code runs, displaus output and asks for input
    which salespersoname = input 2
    */
    if (salesPersonName.matches("^[^a-z,^A-Z,\\s]$")){
    System.out.println("Sales person's name can not"
    + " be empty or have space, must contain "
    + "a-z characters");
    System.out.println("Enter Sales Person Name;");
    salesPersonName = input2.nextLine();
    }
    //keeps looping until input is not null or contains just spaces
    } while (salesPersonName.matches("^[^a-z,^A-Z,\\s]$"));

    6 Comments

    Is there not a trim function you can use?

    That's because your regex is matching a single space, slap a + on it to match more than one

    Original Poster

    do i add the plus after \\s or at the start before ^ ? cheers

    Original Poster

    i got it thanks to your good self, just need to find a way to loop nulls from return key, i think \\r is for returns

    Why don't you use salesPersonName.contains(" ");
    Post a comment
    Avatar
    @
      Text
      Top Discussions
      1. How dangerous is Donald trump?31147
      2. ❅☁☁❅ I want☼to talk☼about the☔WEATHER☔no politics☃no religion❅☁☁❅18846183
      3. Microsoft Gamescom - Possibly more free games / Project Scorpio913
      4. Just heard this...2 ★★★★★★★★★★★★★★ congrats to all on 392k ★★★★★★★★★★★★★★7764260

      See more discussions