Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

data getting lost  RSS feed

 
neelima mullapudi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii
i am entering data from user into a map called emplist but my data is not getting saved that is user has to enter data everytime n then do some other actions . am giving the code
my client class

my ListManager class
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi neelima,

Welcome to CodeRanch!

Please UseCodeTags. I've added those for you.

I haven't gone through complete code, but I haven't understood your question. What do you mean by 'getting saved'? Do you want to persist those values even when you start program next time?

Further, why your ListManager class does not manage List, but Map? Shouldn't the name be MapManager? Same goes for employeeList (which is actually a Map).

Please be more clear about what output it is currently giving, and what output are you expecting.

Also, please UseRealWords ('n' is not substitute for 'and').
 
neelima mullapudi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean to say that user has to enter same data every time the program is executed .the previously entered values are getting lost
and regarding the names of the classes am sorry for it
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you recording the data?
More seriously, why have you written static so often? Unless you have a good reason for writing static, that is a serious mistake.
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neelima,

You are using memory variables to store data. Life of a memory variable stays till the life of the program. To save them permanently you need some persistence storage like files, database etc.
 
neelima mullapudi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thank you
and in the same code i want to assign an employee id automatically ie the user need not enter the id
and for the same i am using another function after the do..while statement

public static int getEmpId()
{
return empid++;
}

but i am not able to generate ids..
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should it be after do while or should you call it when a new employee record is being added?
 
neelima mullapudi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have kept it after do..while
and am calling it every time an employee is being added but its not happening so
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the changed code i.e from where are you calling this method?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
neelima mullapudi wrote:and in the same code i want to assign an employee id automatically ie the user need not enter the id

Then you will certainly have to persist that as well (or at least the last generated value for a session). If you don't, your sequence will restart at 0 whenever you restart the program.

Frankly, it seems to me that you would be better off using a database, because these sorts of things are exactly what they were designed for (particularly sequence numbers).

Winston
 
neelima mullapudi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the code i am using where i am appending the auto increment of employee id at the end of the do .. while but it is not happening so can you say me where i am wrong .


public class Main {

/**
* @param args
*/
static int empid=200;
public static void main(String[] args) {
// TODO Auto-generated method stub
Developer emp = null ; Role r;Department dpt = null;
boolean flag=true;
float sal = 0;
int id = 0,dId=0,deptid = 0;
String name,dept,desg,role = null,rname;
Department k;
do{
System.out.println("ADD DETAILS ACC TO CHOICE");
System.out.println("ENTER 1 TO ADD DEPARTMENT");
System.out.println("ENTER 2 TO ADD EMPLOYEE");
System.out.println("ENTER 3 TO ADD MANAGER");
System.out.println("ENTER 4 TO GET THE EMPLOYEES LIST");

System.out.println("ENTER 99 TO EXIT");
Scanner choice = new Scanner(System.in);
int ch = Integer.parseInt(choice.next()) ;

switch(ch)
{
case 1 :System.out.println("ENTER DEPARTMENT name and id ");
name =choice.next();
dId=choice.nextInt();

k=new Department(name,dId);
ListManager.setDepartment(dId,k);
System.out.println("department added");
System.out.println(ListManager.getDeptList().size());
break;

case 2:System.out.println("ENTER EMPLOYEE NAME,DEPARTMENT,ROLE,DEPT_ID ");
System.out.println("enter the number of employes to be inserted");
int n = choice.nextInt();
int eid;
for(int i=0;i<n;i++)
{
eid=getEmpId();
System.out.println("id is "+eid);
name=choice.next();
dept =choice.next();

if (ListManager.deptList.containsKey(deptid))
{
emp=new Developer(eid,name,dept,role);
ListManager.setEmployee(deptid, emp);

emp.setDept(dpt);
emp.setRole(role);
System.out.println("Employee added "+ emp.getRole());
}
else {
System.out.println("add department first");
System.out.println("please... exit and add a department");
}
}
role=choice.next();
if(ListManager.roleList.containsKey("manager"))
deptid=choice.nextInt();
else
System.out.println("there is no manager ... please find a manager");
dpt=ListManager.getDepartment(deptid);

System.out.println(ListManager.getEmpList().size());
break;

case 3:System.out.println("ENTER DETAILS OF THE MANAGERS ie NAME DEPARTMENT ID");
name = choice.next();
dept = choice.next();
id= choice.nextInt();
dpt =ListManager.getDepartment(id);
emp = new Developer(id,name,dept,"manager");
ListManager.setEmployee(id, emp);
//ListManager.removeEmployee(d);
emp.setDept(dpt);
System.out.println("Manager added "+ emp.getRole());

//System.out.println("NO SUCH EMPLOYEE EXITS");

break;
case 4:System.out.println("UR EMPLOYEES ");
System.out.println(ListManager.getEmpList().size());
break;
case 99:System.out.println("THANK YOU");
flag=false;
break;
}
}while(flag);


}
public static int getEmpId()
{
return empid++;
}
}

 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code gives you the employee id, but are you setting this to employee object?
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . .

Please UseCodeTags. . . .
You have already been told. Please edit your last post, because people won’t read such a large block of unformatted code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!