Java how to transform functional code into methods -


something awesome object oriented programs can make efficient , quick programming, did not learn how use it. far know basics of how construct programs in script style fashion, never use methods/objects, question how convert code use methods, can learn how this, googled how, watched videos on how still dont understand, need real world examples.

here's script style code:

string[] studentroster =          {              "1,john,smith,john1989@gmail.com,20,88,79,59",             "2,suzan,erickson,erickson_1990@gmailcom,19,91,72,85",             "3,jack,napoli,the_lawyer99yahoo.com,19,85,84,87",             "4,erin,black,erin.black@comcast.net,22,91,98,82",             "5,adan,ramirez,networkturtle66@gmail.com,24,100,100,100"          };  (int k = 0; k < studentroster.length; k++)     {         string s2 = studentroster[k];         string [] parts2 = s2.split(",");         string email2 = parts2[3];          string invemail2 = email2;           boolean emailfound = false;         boolean emailfound2 = false;         boolean enotfound;         boolean enotfound2;          (int = 0; < invemail2.length(); i++)         {             char emailfind = invemail2.charat(i);               if (emailfind == '@')             {                  emailfound = true;             }               else             {                 enotfound = false;             }               if (emailfind == '.')             {                 emailfound2 = true;             }              else             {                 enotfound2 = false;             }          }          if (emailfound && emailfound2)         {             system.out.println(invemail2 + " " + "is valid email");         }         else         {             system.out.println(invemail2 + "is invalid");         }     } 

all program checks invalid emails looking indexes missing '.' , or '@', however, how use methods code? how use object oriented programming make cleaner? using 1 class, how use 2 classes achieve same thing? , thanks.

here's way convert code oo , start thinking in oo terms:

1) "entity" working on - email address, need email class

public class email { { 

2) can build email string first needs validation - class have constructor takes string , parses email instance have isvalid() method

public class email { string address = null;

public email(string input) {     // parse input address }  public boolean isvalid() {     return address != null; }  public static void main(string[] args) {     string[] studentroster = {              "1,john,smith,john1989@gmail.com,20,88,79,59",             "2,suzan,erickson,erickson_1990@gmailcom,19,91,72,85",              "3,jack,napoli,the_lawyer99yahoo.com,19,85,84,87",             "4,erin,black,erin.black@comcast.net,22,91,98,82",              "5,adan,ramirez,networkturtle66@gmail.com,24,100,100,100"      };      (int k = 0; k < studentroster.length; k++) {         email email = new email(studentroster[k]);         if (email.isvalid()) {             system.out.println(email.address + " valid email");         } else {             system.out.println(email.address + " invalid email");         }     } } 

}

3) alternatively, have static containsemail() method takes string , returns true/false


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -