Results 1 to 16 of 16
  1. #1
    LordHamshire is offline Member
    Join Date
    Jul 2014
    Posts
    6
    Rep Power
    0

    Default How to Check String for Exactly 2 Capital Letters and 1 Space

    I started using Java a couple of days ago, so sorry if this question seems obvious. If you haven't guessed I want to see if the user is typing a full name or not, but I'm actually not too concerned with any more complexity than I mentioned in the title. It's ok if an input like "GLba b" comes out positive.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    LordHamshire is offline Member
    Join Date
    Jul 2014
    Posts
    6
    Rep Power
    0

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    It's for a text based game for my friends, lol. So I'm not too worried about that.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    Alright then ;) But its an interesting read, right? It totally shocked me to silence and made me feel stupid for a week. Also note that by providing the context for your question ("why am I asking this"), you immediately made it possible to determine what answers might actually apply. Something to remember for the next time.

    You have a few basic options.

    a) regular expressions to the rescue
    b) just loop through the characters of the String and count the characters you want to see

    a) requires you to learn regular expressions which is not easy but very valuable knowledge nonetheless, b) should be well within your range of capabilities with the API docs of the String and Character classes as your guide
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    LordHamshire is offline Member
    Join Date
    Jul 2014
    Posts
    6
    Rep Power
    0

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    I get the idea. Could I have a basic example of what that might look like?

    I've used simple loops:

    while (name.equals("") || name.equals(" "))
    {
    System.out.println(" Protocol requires crew identification.");
    Thread.sleep(0050);
    System.out.println(" I cannot allow you to access the ship's terminal otherwise.");
    System.out.print("\n Crew Indentification: ");
    name = userInputScanner.nextLine();
    }
    But it would be helpful to see what that would look like.
    Last edited by LordHamshire; 07-23-2014 at 03:40 PM. Reason: legibility

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    The Character class has a lot of is* methods, for example the isUpperCase(char) method. The String has a methods to get a char array, so run over that plus the static Character.is* methods, and we have a winner.

    See here: Character (Java Platform SE 7 )
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    It is so simplistic that any example I give is the code you will use. The generic forum term for that is "spoonfeeding" and it is very much frowned upon in this forum (and by me personally).

    Do it in small steps. Write some code which loops over the characters of a String first and prints them out individually. Then go from there.

    String (Java Platform SE 7 )

    Character (Java Platform SE 7 )
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    To use that, you could set two variables: hasTwoUppercaseChars and hasSpace. Then you would get something like this:
    Java Code:
    for ( char c: myChars ) {
       if ( Character.isUpperCase(ch) ) {
          hasTwoUppercaseChars++;
       }
       else {
       ...
    }
    
    if ( hasTwoUppercaseChars >= 2  && hasSpace) {
        //we cool!
    }
    Remember, especially when parsing Strings, for 200 developers, you probably get 200 different opinions. Pick on that works and tell the rest to piss off! :D

    Edit: this is not spoonfeeding! I know leechers when I see them :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    LordHamshire is offline Member
    Join Date
    Jul 2014
    Posts
    6
    Rep Power
    0

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    I find that I learn better when given an example that I can play around with, *shrug*. Thanks for the help :D.

    //we cool!

    I'll probably post a finished version of what I decided to do to make sure I'm not doing anything too stupid.
    Last edited by LordHamshire; 07-23-2014 at 04:08 PM.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    Quote Originally Posted by LordHamshire View Post
    I find that I learn better when given an example that I can play around with
    I used to think the same when I was starting out. But when I had spent years looking through other people's code, I realized that I had only learned how to duplicate other people's code without understanding how it actually worked. Nowadays I beg people to not repeat my mistakes, but it is a hard one to sell.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    Quote Originally Posted by LordHamshire View Post
    I find that I learn better when given an example that I can play around with, *shrug*. Thanks for the help :D.

    //we cool!
    You're not going to get an working example for *everything*. You should eventually be able to create a blank class, and start experimenting with for example character parsing. Once that works, and you understand what's happening, you can implement it in your code.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    LordHamshire is offline Member
    Join Date
    Jul 2014
    Posts
    6
    Rep Power
    0

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    Sorry I'm probably coming across as a little lazy at this point but I'm actually just really stupid and need help, lol:

    In your example, presumably its testing a array of char from a string, which isn't a problem but: if ( Character.isUpperCase(ch) )
    char refers to the current char the array is checking? What would I actually put there?

    Java Code:
    System.out.print("\n Crew Identification: ");
    name = userInputScanner.nextLine();
    char[] charArray = name.toCharArray();
    for ( char c: charArray ) {
    if ( Character.isUpperCase(ch) ) {
    hasTwoUppercaseChars++;
    }
    else {
    ...
    }
    if ( hasTwoUppercaseChars >= 2  && hasSpace) {
    ...
    }
    Maybe I'm just being stupid and interpreting the intent of the example wrong?

    I hope this doesn't come across as me trying to jam his code into mine, I actually just want to understand and learn these functions.


    Quote Originally Posted by gimbal2 View Post
    I used to think the same when I was starting out. But when I had spent years looking through other people's code, I realized that I had only learned how to duplicate other people's code without understanding how it actually worked. Nowadays I beg people to not repeat my mistakes, but it is a hard one to sell.
    I guess it's not that I want to see what they are doing to copy it exactly, I just interpret the information better by example mixed around with my own testing and research.
    Last edited by LordHamshire; 07-23-2014 at 04:42 PM. Reason: nervously trying to explain my way out of shame

  13. #13
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    I actually made a typo there. The for-loop uses variable c, and I accidently typed ch as parameter. Change the parameter to match the variable in the loop. Make sure you understand what's happening before you use this code where ever you want to paste it.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    Quote Originally Posted by gimbal2 View Post
    Alright then ;) But its an interesting read, right? It totally shocked me to silence and made me feel stupid for a week.
    Having known a few people who had changed their names by deed poll, including into some quite interesting ones, I can at least be smug about some of those...:)
    And, frankly, if Prince ever wanted to sign up for something I'd written, well...he can sod off, the awkward swine!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    LordHamshire is offline Member
    Join Date
    Jul 2014
    Posts
    6
    Rep Power
    0

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    Oh, I'm not actually using this code yet, I'm just playing around with it. Thanks for the reply.

    EDIT: So here is how I think I'm going to go about it...

    Java Code:
    		while (nameapproval == false) 
    			{
    				System.out.println();
    				System.out.print("\n Crew Identification: ");
    				name = userInputScanner.nextLine();
    				char[] charArray = name.toCharArray();
    				for ( char c: charArray ) 
    						{
    							if ( Character.isUpperCase(c) ) 
    									{
    										hasTwoUppercaseChars++;
    									}
    							if ( c == ' ' && cansetspace == true)
    									{
    										if (hasSpace == true) 
    											{
    												hasSpace = false;
    												cansetspace = false;
    											}
    										
    										hasSpace = true;
    									}
    						}
    					if ( hasTwoUppercaseChars >= 2  && hasSpace) {
    						nameapproval = true;
    					}
    					else 
    						{
    							if (name == "" || name == " ") 
    								{
    									System.out.println(" Protocol requires crew identification.");
    									Thread.sleep(0050);
    									System.out.println(" I cannot allow you to access the terminal otherwise.");
    								}
    							else
    								{
    									System.out.println(" ERROR: Identity not recognised.");
    								    Thread.sleep(0050);
    									System.out.println(" Please enter your full name as cataloged in the consortium database.");
    								}
    							name = "";
    							hasTwoUppercaseChars = 0;
    							hasSpace = false;
    							cansetspace = true;
    						}
    			}
    Last edited by LordHamshire; 07-23-2014 at 05:46 PM. Reason: entering in the code I'm currently using

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to Check String for Exactly 2 Capital Letters and 1 Space

    If you want to check for exactly two upper case letters and exactly one space, your solution is way too verbose (and I think it is wrong as well); something else, sleeping for 0050 (note the leading zeros) millis, makes your thread sleep for 40 (decimal) millis instead ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Converting first character of String variable to capital.
    By Augustus Longeye in forum New To Java
    Replies: 1
    Last Post: 03-07-2011, 01:33 PM
  2. Shifting String Letters
    By DreamNaut in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 08:12 PM
  3. counting letters in a string
    By beandip408 in forum New To Java
    Replies: 12
    Last Post: 09-29-2010, 01:44 PM
  4. increment letters from a string
    By iluvjava in forum New To Java
    Replies: 24
    Last Post: 03-22-2010, 05:18 PM
  5. validating a string for numbers and letters?
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 09:17 AM

Tags for this Thread

Posting Permissions

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