How to instantiate an object which is in a different class in java -
i trying create program saves of data given user save file , read file when user wants know information back.
now problem arises, when try call object instantiated in different method after call load() function.
import java.io.fileinputstream; import java.io.serializable; import java.io.fileoutputstream; import java.io.objectinputstream; import java.io.objectoutputstream; import java.util.scanner; import java.util.arraylist; public class aibob { //array list of users public static arraylist<user> userlist = new arraylist<user>(); public static void main (string args[]){ scanner scanner = new scanner(system.in); system.out.println("hello! name bob, existing user or want make new account? "); string userquantifier = scanner.nextline(); string[] check = {"exist","new"}; if(userquantifier.contains(check[0])){ system.out.println("can tell me name please?"); load(); newuser.load(); // don't know how this. } else{ newuserpref(scanner); } string givenname = scanner.nextline(); for(int = 0; i<=10;i++){ if(userlist.get(i).getusername().contains(givenname)){ } } } }
this load() function:
@suppresswarnings({ "unchecked", "unused" }) public static void load(){ try{ // open file read from, named savedobj.sav. fileinputstream savefile = new fileinputstream("saveobj.sav"); // create objectinputstream objects save file. objectinputstream save = new objectinputstream(savefile); // restore. // readobject() returns generic object, cast // original class type. // primitive types, use corresponding reference class. arraylist <user> userlist = new arraylist<user>(); userlist = (arraylist<user>) save.readobject(); // close file. save.close(); // closes savefile. } catch(exception exc){ exc.printstacktrace(); // if there error, print info. } }
this newuserpref method save takes place
public static void newuserpref(scanner scanner){ system.out.println("hello! name bob, name? "); string gname = scanner.nextline(); //create new user, assign name , user number user newuser = new user(gname); //takes user input , saves user-name. userlist.add(newuser); // creates new user in user class. system.out.println("hi "+newuser.getusername()+"! can tell me how old you?"); int userage = scanner.nextint(); newuser.setage(userage); system.out.println("if lactose intolerant, please write y/n "); string input = scanner.next(); newuser.lactosefinder(input); system.out.println("if want food glutten free, please write y/n "); string otherinput = scanner.next(); newuser.gluttenfinder(otherinput); system.out.println("if vegetarian or non-vegetraian, please write y/n "); string dainput = scanner.next(); newuser.vegornonveg(dainput); system.out.println("if vegan, please write y/n "); string jainput = scanner.next(); newuser.vegan(jainput); newuser.save(); try{ // catch errors in i/o if necessary. // open file write to, named savedobj.sav. fileoutputstream savefile=new fileoutputstream("saveobj.sav"); // create objectoutputstream put objects save file. objectoutputstream save = new objectoutputstream(savefile); // save. save.writeobject(userlist); // close file. save.close(); // closes savefile. } catch(exception exc){ exc.printstacktrace(); // if there error, print info. } }
this user class's save , load method
public void save(){ try{ // catch errors in i/o if necessary. // open file write to, named savedobj.sav. fileoutputstream savefile=new fileoutputstream("savenewobj.sav"); // create objectoutputstream put objects save file. objectoutputstream save = new objectoutputstream(savefile); // save. save.writeobject(getusername()); save.writeobject(getage()); save.writeobject(getlactoseintolerant()); save.writeobject(getglutten()); save.writeobject(getvegornonveg()); save.writeobject(getvegan()); // close file. save.close(); // closes savefile. } catch(exception exc){ exc.printstacktrace(); // if there error, print info. } } public void load(){ try{ // open file read from, named savedobj.sav. fileinputstream savefile = new fileinputstream("savenewobj.sav"); // create objectinputstream objects save file. objectinputstream save = new objectinputstream(savefile); // restore. // readobject() returns generic object, cast // original class type. // primitive types, use corresponding reference class. username = (string) save.readobject(); age = (integer) save.readobject(); lactoseintolerant = (boolean) save.readobject(); glutenfree = (boolean) save.readobject(); vegetarian = (boolean) save.readobject(); vegan = (boolean) save.readobject(); // close file. save.close(); // closes savefile. } catch(exception exc){ exc.printstacktrace(); // if there error, print info. } }
this first post didn't know how code restriction there on website or should put code in 1 place or not. confused whether save/load function works or not. comment helpful. also, saved 1 file, hoping there way save different users in different files. thank in advance.
there few problems point out.
first, scope of variables.
in newuserpref()
method, declared newuser
user object: user newuser = new user(gname);
in main method, tried use newuser
again: newuser.load();
however, main method not able access newuser
because local within newuserpref()
. instead can declare static member object static user newuser;
accessible entire class, , instantiate necessary. following example, though should decide instantiate it:
public static void main (string args[]){ ... if(userquantifier.contains(check[0])){ system.out.println("can tell me name please?"); load(); newuser = new user(/*parameters*/); newuser.load(); } ... } public static void newuserpref(scanner scanner){ ... string gname = scanner.nextline(); newuser = new user(gname); ... }
second, naming conventions.
though isn't major issue cause errors, standard name variables , methods camelcase
. start doing in future; people reading code.
hope helps, , let me know if need clarification.
Comments
Post a Comment