Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Enforcing a format on a string?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Enforcing a format on a string?

    Alright, so let's say I have this string. I want to make it so that the string for this variable has to follow a certain format.

    For example, "AAA-B", where the A's are a digit 0-9, the - is always in the same spot, and B is a letter but only from A-M. All of this for a single instance variable.

    Is this possible? How could I go about doing this.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Enforcing a format on a string?

    I would say that it's possible, but I don't understand how you'd apply it. You start off with, "let's say I have this string." What is the original content of the string you have? How is it decided what the string object you have will become in the format you've shown?

    If you're asking how to generate a series like:

    000-A through 000-M, then
    001-A through 001-M, then
    002-B through 002-M, then
    etc.

    It could definitely be done. You should make an effort to accomplish that and then come back when you need help.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Enforcing a format on a string?

    The string will be created based on user input, I'll ask them for them to enter it with a reminder of the format. However; as much as I can tell the user it has to be in that format, I need to create something so that if they entered it as AAA-C or AA-AC instead of AAA-B, it'll throw a custom exception (that I'm creating). The issue is that I just don't know how I can make it check to make sure A = Number 0-9, the - always at the index 3, and C = Character A-M, or else it'll throw my exception.

    (Sorry if my descriptions or statements are a bit off, it's been a long tiring week already)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enforcing a format on a string?

    how I can make it check to make sure A = Number 0-9, the - always at the index 3, and C = Character A-M
    Are you looking for a regex
    or can you code a loop or if to do the validation char by char?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Enforcing a format on a string?

    I can code a loop/if to do the validation, mostly. I just worry with that because, if I were to do Character.isDigit at the index, wouldn't it throw my exception once it got to the - or the B, because it's not a digit?

    And I have no idea about the limitations of what it can be (I.E. A-M) as I've never done or experienced that kind of problem..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enforcing a format on a string?

    The code would look at each section of the String differently: 3 digits, one - and one letter
    The validation might be done in a single if statement if the specific wrong char didn't need to be identified. No need for a loop.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Enforcing a format on a string?

    So should I have 3 separate if statements? One for each of the things I'm checking? How could I go about doing this? (I haven't had much experience with this kind of issue until recently).

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enforcing a format on a string?

    Does the specific char that is wrong need to be reported
    or is it enough to say there is an error in the String?

    How many if statements to use depends on the response to the above.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Enforcing a format on a string?

    It is enough to say that there is an error in the string. It shall throw an exception if there is that explains to the user the correct format that needs to be entered if they enter it wrongly formatted/with wrong numbers/letters (for ex "N"). It won't be reported where their error is.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enforcing a format on a string?

    enough to say that there is an error in the string
    Ok, then a single if statement something like this:
    if(firstCharIsOk && secondCharIsOk && continue with expressions to test each char to last char) {
      // String is OK
    }else {
      //  String has bad character
    }
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Enforcing a format on a string?

    Right.

    Here's what I have so far

    if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
          {
             status = false;
          }
          return status;

    But I'm surely writing something wrong as I'm getting errors with it. Is it possible to do a isDigit test inside the charAt?

    What should I do for the letters A-M check? I can't really make a comparison to see if the character is not N-Z right?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enforcing a format on a string?

    I'm getting errors
    Please copy the full text of the error messages and paste it here.

    Looks like you should read the API doc for the classes and methods you are using to see how to code them.

    What should I do for the letters A-M check
    char values can be treated the same as int values. Use the < and > operators to test if a char is less than 'A' or greater than 'M' just like you would do with int variable you wanted to range check. The values for the chars don't have any undefined holes in the range of values. for example: 'A' + 1 = 'B' for all the letters
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Enforcing a format on a string?

    The errors are:

    Employee.java:105: error: ')' expected
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                          ^
    Employee.java:105: error: illegal start of expression
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                  ^
    Employee.java:105: error: not a statement
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                 ^
    Employee.java:105: error: ';' expected
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                   ^
    Employee.java:105: error: ')' expected
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                                     ^
    Employee.java:105: error: illegal start of expression
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                                             ^
    Employee.java:105: error: not a statement
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                                            ^
    Employee.java:105: error: ';' expected
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                                              ^
    Employee.java:105: error: ';' expected
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                                                                          ^
    Employee.java:105: error: ';' expected
          if(num.charAt(0.isDigit()) && num.charAt(1.isDigit()) && num.charAt(2).isDigit()) && num.charAt(3) = "-")
                                                                                                                  ^
    10 errors

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enforcing a format on a string?

    Read the rest of post#12

    Note:
    = is the assignment operator
    == is the equality tester
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Enforcing a format on a string?

    Great, I got it. Thank you.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Enforcing a format on a string?

    Glad you got it working.
    regex will probably also do what you want and I'm sure you will learn about regex later.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. NumberFormat to Java String Format
    By zng690 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2014, 04:57 PM
  2. Percentage with String.format
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 24th, 2013, 10:11 AM
  3. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  4. Replies: 1
    Last Post: March 15th, 2013, 03:04 AM
  5. [SOLVED] Enforcing an If/Else if statement
    By Actinistia in forum Loops & Control Statements
    Replies: 6
    Last Post: March 4th, 2011, 12:24 PM