variables - Java String Switch Statement Not Going To Proper Switch -
variables - Java String Switch Statement Not Going To Proper Switch -
i'm facing issue switch statement. i've printed out switch variable, , seems correct, print statement desired switch not working. switch statement in main ~20 lines bottom.
the test stub is:
"person#joe gaucho#28000 marguerite parkway#949-582-4800#gaucho@sb.edu;" "faculty#1234153524#1341244#1234150000#adjunct;employee#bgs210#20000#123455000;" "person#mickey mouse#disneyland#800 1disney#mmouse@disney.com;" "exit#"
the expected output is:
person name: joe gaucho address: 28000 marguerite parkway phonenumber: 949-582-4800 emailaddress: gaucho@sb.edu faculty officehours: wed jan 14 22:49:13 pst 1970, wed dec 31 16:22:21 pst 1969, wed jan 14 22:49:10 pst 1970 rank: adjunct employee office: bgs210 salary: 20000.0 datehired: fri jan 02 02:17:35 pst 1970 person name: mickey mouse address: disneyland phonenumber: 800 1disneyemailaddress: mmouse@disney.com
and output is:
person name: joe gaucho address: 28000 marguerite parkway phonenumber: 949-582-4800 emailaddress: gaucho@sb.edu person name: joe gaucho address: 28000 marguerite parkway phonenumber: 949-582-4800 emailaddress: gaucho@sb.edu employee noffice: bgs210 salary: 20000.0 datehired: 123455000 employee noffice: bgs210 salary: 20000.0 datehired: 123455000
this code:
import java.util.scanner; public class exercise10_2m { static class person{ string name; string address; string phonenumber; string emailaddress; person(){} person(string name, string address, string phonenumber, string email){ this.name = name; this.address = address; this.phonenumber = phonenumber; this.emailaddress = email; } @override public string tostring(){ homecoming "person\tname: " + name + "\taddress: " + address + "\tphonenumber: " + phonenumber + "\temailaddress: " + emailaddress + "\n"; } } static class pupil extends person{ string classstatus; // freshmen, sophomore, sophomore+, junior, or senior student(){} student(/*string str1, string str2, string str3, string str4,*/ string str5){ //super(str1, str2, str3, str4); classstatus = str5; } @override public string tostring(){ homecoming "student\tclassstatus: "+ classstatus + "\n" /*+ super.tostring()*/; } } static class employee extends person{ string office; double salary; long datehired; employee(){} employee(string str, double doub, long long1){ office = str; salary = doub; datehired = long1; } @override public string tostring(){ homecoming "employee\tnoffice: " + office + "\tsalary: " + salary + "\tdatehired: " + datehired + "\n"; } } static class faculty extends employee{ long[] officehours; string rank; // either adjunct, assistantprofessor, or professor faculty(){} // !!!! prepare /*faculty(long long1, string str){ officehours = long1; rank = str; }*/ @override public string tostring(){ homecoming "faculty\tofficehours: " //+ officehours[0] + "\trank: " + rank + "\n"; } } static class staff extends employee{ string title; staff(){} staff(string str){ title = str; } @override public string tostring(){ homecoming "staff\ttitle: " + title + "\n"; } } public static void main(string[] args){ scanner firstscan = new scanner(system.in); // scanner string firstinput; // parses scanner string[] myinput; // parses firstinput person myclass = new person(); // class instance string typeofperson = new string(); // holds value class boolean lcv = true; firstscan.usedelimiter(";"); do{ firstinput = firstscan.next(); myinput = firstinput.split("#"); typeofperson = myinput[0]; switch(typeofperson){ case "person": myclass = new person(myinput[1], myinput[2], myinput[3], myinput[4]); break; case "student": myclass = new student(/*scan.next(), scan.next(), scan.next(), scan.next(),*/ myinput[1]); break; case "employee": myclass = new employee(myinput[1], double.parsedouble(myinput[2]), long.parselong(myinput[3])); break; case "faculty": system.out.println("check"); //myclass = new faculty(); break; case "staff": myclass = new staff(myinput[1]); break; case "exit": lcv = false; break; } if(lcv == true){ system.out.print(myclass.tostring()); } }while(firstscan.hasnext() && lcv == true); } }
you need reset value of myclass
default value (commonly null
) @ end of each loop. you're getting repeated output because:
person
it updates myclass
new person
object. loop goes top, finds new faculty
myclass
isn't overwritten, ergo person
remains , outputted again. i've replicated behaviour in this ideone.
java variables switch-statement
Comments
Post a Comment