java - working with constructor and accessor method -


package learning;  public class matchscore {      private string matchnumber;     private string killsinmatch;     private string deathsinmatch;      public void setmatchnumber(string namein){         matchnumber = namein;     }     public string getname(){         return matchnumber ;     }      public void setkillsinmatch(string killsin){         killsinmatch = killsin;     }     public string getkillsinmatch(){         return killsinmatch;     }     public void setdeathsinmatch(string deathsin){         deathsinmatch = deathsin;     }     public string getdeathsinmatch(){         return deathsinmatch;     }      public void totalstatus(double stats){         system.out.printf("this game %s ", matchnumber);         system.out.printf("you killed-this many %s", killsinmatch);         system.out.printf("but died-this many time %s", deathsinmatch);     }  } 

this ^ constructor method. have created sets match number, kill number, , death number. these variables have created, aren't coming games or anything.

package learning;  import java.io.file; import java.io.ioexception; import java.util.scanner;  public class matchscores {     public static void main(string args[])              throws ioexception {                 scanner diskscanner =                         new scanner(new file("matchscoress.txt")); //the file has in package, in case learning folder.                  (int games = 1; games <= 5; games++) {                     checkoutscores(diskscanner);                         }                     diskscanner.close();                         }              static void checkoutscores(scanner ascanner) {                 matchscore amatch = new matchscore();                 amatch.setmatchnumber(ascanner.nextline());                 amatch.setkillsinmatch(ascanner.nextline());                 amatch.setdeathsinmatch(ascanner.nextline());                 ascanner.nextline();         }     } 

this ^ accessor method. have made file has match number, kill number, , death number. error "exception in thread "main" java.util.nosuchelementexception: no line found @ java.util.scanner.nextline(unknown source) @ learning.matchscores.checkoutscores(matchscores.java:22) @ learning.matchscores.main(matchscores.java:15)". when remove "ascanner.nextline(); program doesnt give me error, doesnt bring me match number , etc.. well.

i'm starting learn java, , im on chapter accessor , constructor methods. awesome!! thanks.

you seem bit confused. claim entire class accessor method, , class constructor method. accessors functions in class, , constructors.

if have variable foo, accessors in form:

void setfoo(<type> value) {     foo = value }  <type> getfoo() {     return foo; } 

hence, in code, setmatchnumber(), getname(), setkillsinmatch(), getkillsinmatch(), etc accessors (althoug getname() should called getmatchnumber()).

a constructor method declared same name class contained into, without return type in declaration. example, matchscore class may have

public matchscore(string num, string kills, string death) {     matchnumber = num;     killsinmatch = kills;     deathsinmatch = death; } 

(btw, fields , local variables start non-capitalized, should matchnumber, killsinmatch , deathsinmatch instead).

now, specific exception: looks matchscoress.txt file has 3 lines, hence first 3 nextline() invocations succeed, , fourth throws exception.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -