java.util.scanner - How do you input data into a constructor from scanner? -
i trying take constructor(string, string, double) , set value in scanner input. appreciated. code list below.
my programs can assign values put in, want able assign them keyboard , use 1 constructor method accomplish this. thanks
i have broken 2 classes:
import java.util.scanner; public class employeetest { public static void main(string [] args) { scanner input = new scanner(system.in); employee employee1 = new employee("james", "ry", 3200); employee employee2 = new employee("jim" , "bob" , 2500.56 ); system.out.println("what employyee 1's first name? "); employee1.setfname(input.nextline()); } } class employee { private string fname; private string lname; private double pay; public employee(string fname, string lname, double pay) { this.setfname(fname); this.lname = lname; this.pay = pay; system.out.println("employee " + fname +" "+ lname + " makes $" + + pay + " month , 10% raise, new pay $" + (pay * .10 + pay)); } void setfname(string fn) { setfname(fn); } void setlname(string ln) { lname = ln; } void setpay (double sal) { pay = sal; } string getfname() { return getfname(); } string getlname() { return lname; } double getpay() { if (pay < 0.00) { return pay = 0.0; } return pay; } public string getfname() { return fname; } public void setfname(string fname) { this.fname = fname; } }
you can modify employeetest
class like
class employeetest { public static void main (string...arg) { scanner s = new scanner(system.in); string[] elements = null; while (s.hasnextline()) { elements = s.nextline().split("\\s+"); //to make sure have 3 values //using want construct object if (elements.length == 3) { //call method creates object needed //createobject(elements[0], elements[1], double.parsedouble(elements[2])) //or, directly use these values instantiate employee object } else { system.out.println("wrong values passed"); break; } } s.close(); } }
Comments
Post a Comment