Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Elegant validation?

Elegant validation?

Scheduled Pinned Locked Moved Java
questionhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neo10101
    wrote on last edited by
    #1

    Given the following code:

    private String name;
    private List<String> years;
    private List<String> studyChoices;

    private void doCheck() {
        if (!name.isEmpty() || name == null) {
    
        }
        for (String year : years) {
            if (!year.isEmpty() || year == null) {
    
            }
        }
        for (String studyChoice : studyChoices) {
            if (!studyChoice.isEmpty() || studyChoice == null){
    
            }
        }
    }
    

    How do I condense this code into a more elegant and OO solution? Maybe put it all in one list and check for each item in that list? I would like something like: 'If any of these parameters were not passed in or if they are empty, output an error message, else give the desired output'. Basically I have to write a Servlet that gets form parameters from the request and checks if all parameters were provided. If they were, it is then supposed to write an output page. Any ideas? Thanks

    L T 2 Replies Last reply
    0
    • N Neo10101

      Given the following code:

      private String name;
      private List<String> years;
      private List<String> studyChoices;

      private void doCheck() {
          if (!name.isEmpty() || name == null) {
      
          }
          for (String year : years) {
              if (!year.isEmpty() || year == null) {
      
              }
          }
          for (String studyChoice : studyChoices) {
              if (!studyChoice.isEmpty() || studyChoice == null){
      
              }
          }
      }
      

      How do I condense this code into a more elegant and OO solution? Maybe put it all in one list and check for each item in that list? I would like something like: 'If any of these parameters were not passed in or if they are empty, output an error message, else give the desired output'. Basically I have to write a Servlet that gets form parameters from the request and checks if all parameters were provided. If they were, it is then supposed to write an output page. Any ideas? Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      First thing is that all your tests are the wrong way round (and logically wrong I suspect), you should check the variable is not null before using it as a reference to a method thus:

      if (name == null || name.isEmpty()) {
      // or maybe you really mean
      if (name != null && !name.isEmpty()) {

      I am not sure how you would condense the two loops any more as they appear to be independent of each other.

      speaking as ...

      1 Reply Last reply
      0
      • N Neo10101

        Given the following code:

        private String name;
        private List<String> years;
        private List<String> studyChoices;

        private void doCheck() {
            if (!name.isEmpty() || name == null) {
        
            }
            for (String year : years) {
                if (!year.isEmpty() || year == null) {
        
                }
            }
            for (String studyChoice : studyChoices) {
                if (!studyChoice.isEmpty() || studyChoice == null){
        
                }
            }
        }
        

        How do I condense this code into a more elegant and OO solution? Maybe put it all in one list and check for each item in that list? I would like something like: 'If any of these parameters were not passed in or if they are empty, output an error message, else give the desired output'. Basically I have to write a Servlet that gets form parameters from the request and checks if all parameters were provided. If they were, it is then supposed to write an output page. Any ideas? Thanks

        T Offline
        T Offline
        TorstenH
        wrote on last edited by
        #3

        Is this entered from a GUI or is this object coming from some API? For a GUI you can also use a GUI sided validation. You can e.g. use a KeyListener and validate the entered value right away. For an API side validation I would suggest a Validation Layer which has to be passed before the data is used. Your code - as Richard already stated - is a bit mixed up. Here is a valid suggestion:

        private void doCheck() {
        if (name != null && name != "") { // always check first for null
        for (String year : years) { // years only need to be checked when name was ok
        if (year != null && year != "" ) {
        try{
        int i = Integer.parseInt(year);
        // you can check if the number has a valid value here
        // (e.g. between 1900 and 2012 (now-18 for adult check)
        }
        catch(Exception oException){
        /* not a valid number */
        }
        }
        }
        for (String studyChoice : studyChoices) {
        if (studyChoice!=null && studyChoice != ""){
        /*Check for valid study course here - I suggest an Enum for the fixed range of valid values*/
        }
        }
        }
        else{ /*not even a valid name is given*/ }
        }

        regards Torsten When I'm not working

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups