0

If look at the previous post I explain what you asked. Do you want me to sit and write exactly the same things that I explained or copy paste the exact same explanation to a new post?

0

so are you trying to say to me that I should right the variable properly so that it would not be confusing..so that the results of them well be inter change..i have this on my code... private String emp[];
private String empc[];
I should change the variable into
private String empname[];
private String empcivil[];
is that what you want to get...

0

When the variables are like that:
String emp[]
String empc[]

And you ask someone, what are those, they won't know. Even you wouldn't remember them after a week, or when you want to study your code after a while.

Like this: empname, empcivil, is very clear.

0

yes... ok I well do that way....what I'm asking why is it..if I am going to update the object I entered the results of the empname and the empcivil has been interchange....the resuls of the empname is going to be the results of empcivil,and the results of the empcivil is going to be the results of the empname....why is it like that..

/*This is the method I use to update the object I entered but why is it there results is exchanging...*/
	public void update(int pos,String x,String y){
		if(!isEmpty()){
			emp[pos]=x;
			empc[pos]=y;
			System.out.println("done");
		}
		else
		System.out.println("array is empty");

	}
/*And this I use to enter the obeject..*/
	case 5: System.out.println("\nEnter Employee Pos: ");
                        pos1=s.nextInt();
 								 s.nextLine();
				System.out.println(" Enter Employee Name: \n");
                  place1 =s.nextLine();
					 
				System.out.println(" Enter Employee Civil:\n ");
                   num1    =s.nextLine();
                        e.update (pos1,num1,place1);  break;
/*Please try to check what is wrong ...please...*/
0
case 5: System.out.println("\nEnter Employee Pos: ");
                        pos1=s.nextInt();
 			 s.nextLine();/*And also why you suggest me to put nextLine in this part...please try to explain...*/
				System.out.println(" Enter Employee Name: \n");
                  place1 =s.nextLine();
0

As I said before but you ignored me, the code does what you say it to do. If you read the name, then you need to update the array with the names with that value. You are calling the update method and what you pass as arguments are saved to the arrays. When you put the name to one of those arguments, you need to save that argument into the array of the names. If you enter the name of the employee and take that argument and put it into the array of civil statuses of course that is wrong.
So look what arguments you pass to the update method when you call it and what you do with those arguments in the method.

Probably because you don't assign the values correctly. When you read the name into a variable you must put that variable in the array that holds the names, not the civil status

As for the nextLine. Read my previous posts, which you keep ignoring. I give you solutions and suggestions and you ignore the explanations and copy only the code. I will not repeat my self. Read the explanation already given.

Why must I keep posting the same things over and over again, if you don't read them.

Edited by javaAddict: n/a

0

ok...what method would i use to clear all data being input..give the syntax on how to do it and please try to explain each detail why it is like this..or what please teach me...


@javaAddict thank you so much for replying always in my thread..your helping to learn new things thanks..

0

Are you saying you want to delete all the names and civil status entered to the arrays?
Then loop the arrays and give to all the elements null as value. Also you must reset the count variable to 0.

loop
  emp[i] = null;

count = 0;
0
public void clear(String emp, String civil)
for(int i=0;i<emp.length;i++)
  emp[i] = null;

count = 0;

can you give the correct syntax..on how

0

Why the code seems correct. Apart from some missing brackets, that is the logic. Surely you can add those on your own. You should know by now how to declare methods:

public void clear() {
  // your code
}

You also need to put null values to the civil status array.

0
public void clear(String emp, String civil)/*I don't have to put String empname and
empcivil should i erase  this*/for(int i=0;i<emp.length;i++)
  emp[i] = null;
   empcivil=null;
count = 0;/* is this correct if it is wrong would you mind correcting it please*/

thanks ....try to check and correct it and please explain..thanks again

0
public void clear(){
	 for(int i=0;i<emp.length;i++)
	 emp[i]=null;
 		count=0;
/* what condition should i add to make the clear method work on my code
please add what is lucking to my code..*/
case 6: System.out.println("\nClear All");break;


    }
please do check and add what is missing...
0

void means you don't return anything
but (just looking at this piece of code)
what do you want with that case statement?

0

Already explained in previous post how to declare methods. Have the body separately and then call that method in the case body. I have seen this error, with the same code in one of your previous posts which has been corrected.

And also from a previous post it has been explained that the code is correct. The only error was that you didn't have closing brackets anywhere. Code with closing brackets has been provided. Look at previous posts:

public void clear() {
  // your code
}

Put the code that you have. The logic has been explained. Don't randomly write stuff you don't understand


And try to study a bit. don't just copy the code.

Edited by javaAddict: n/a

0
private String empname[];
private String empcivil[];
private String empstat[];
private int age[];

Is this correct writing a program ..that has a four arrays in a class... please correct me..

0
import java.util.*;
class EmployeeList{
    private String emp[];
    private String empc[];

    private int count;
  public EmployeeList(int size){
        emp = new String[size];
        empc=new String[size];
        count=0;
    }
    public EmployeeList(){
        this(10);
    }
    public boolean isFull(){
        return count == emp.length;
    }
    public boolean isEmpty(){
        return count == 0;
    }
    public void add(String x,String y){
        if(!isFull()){
            emp[count] = x;
            empc[count]=y;
            count++;
        }
        else
            System.out.println("\n!!!Sorry the Array is Full");
    }
    public void search(String x){
        boolean flag = false;
          int position= -1;
        for(int i = 0; i < this.count; i++){
            if(this.emp[i].equals(x)){
                flag = true;
                System.out.println(x + " is found at the position " + i + ".");
                break;
            }

Does my code correct creating 2 arrays in one class actually i wanted to add an additional array for the empstat and for the age...please try to check if is correct..and please expalain if theres something wrong to my code...please

0

Yes it is correct to have as many arrays as you want. Just use them in the way you use the two arrays you have in the latest code you posted. I don't see any errors to that code.

I would suggest the search method to return the position the name was found:

public int search(String x){
        boolean flag = false;
          int position= -1;
        for(int i = 0; i < this.count; i++){
            if(this.emp[i].equals(x)){
                flag = true;
                System.out.println(x + " is found at the position " + i + ".");
position = i;
                break;
            }
    }
   return position;
}

If the position returned is -1 then the name was not found. Else it return the position of the array the name was found.

0

churva_churva , if the attribute
private int count;
in the definition of the class EmployeeNames represents a class-wide information, showing how many Employees in the list, it should be static.

Edited by tong1: n/a

0

churva_churva , if the attribute
private int count;
in the definition of the class EmployeeNames represents a class-wide information, showing how many Employees in the list, it should be static.

Actually in this case and with the way the class is declared, I believe that it shouldn't be static:

private String emp[];
private String empc[];

private int count;

If it was static and the user had 2 class instances then the count would apply to both instances which would be wrong. It should be static only if the arrays were static and were declared for use in the main method. If you had seen all the code, you would have seen that this class gets instantiated in a main method, therefor the count is part of the instance class so it doesn't need to be static.

0

ok but what im askin is..is that ok creating 2 or more arrays in one a class

private String emp[];
private String empc[];

private int count;
0

ok but what im askin is..is that ok creating 2 or more arrays in one a class

private String emp[];
private String empc[];

private int count;

Why? Is there a limit on how many arrays you can declare? You can use as many as you want.

0
import java.util.*;

public class EmployeeRecordTest{
	private String emp[];
	private int count=0;
	
	public EmployeeRecordTest(int size){
		emp=new String  [size];
		count=0;
	}
	public EmployeeRecordTest(){
		this(10);
		
	}
		 
    public boolean isFull(){
        return count == emp.length;
    }
    public boolean isEmpty(){
        return count == 0;
    }public void add(String x){
	if(!isFull())
	emp[count++] = x;
	else
	System.out.println("\n SORRY! Elements of Array is full.");
	}
	   public void add(String x ){
        if(!isFull()){
            emp[count] = x;
           
            count++;
        }
        else
            System.out.println("\n!!!Sorry the Array is Full");
    }
	 
    public void delete(int pos){
		if(!isEmpty()){
		for(int i=pos;i<count;i++){
		emp[i]=emp[i+1];
 
                }
		count--;
		System.out.println(emp[pos]+" deleted ");
		}
		else
		System.out.println("array is empty");
  
  }	public void update(int pos,String x){
		if(!isEmpty()){
			 	  emp[pos]=x;
			System.out.println("done");
		}
		else
		System.out.println("array is empty");

	}    public void display(){
        for(int i = 0; i < count; i++)
            System.out.print(emp[i]+"  is "+"  ");
            System.out.println();
    }
	
 
 public static void main(String [] args){
        int choice;
        String name, cstat,mstat;
		  int old;
        String place1;
		  int pos1;	
		 
		  EmployeeRecordTest e= new  EmployeeRecordTest();
        Scanner s=new Scanner(System.in);
		  do{
            System.out.println("[1] Add Name\n[2]Search Name\n[3]Dislay Name\n[4]Delete\n[5]Update\n[6]exit");
            System.out.print("Enter choice:");
            choice= s.nextInt();
            s.nextLine();
				
					switch(choice){
					
						case 1:System.out.println("Enter name");
								name=s.nextLine();
								System.out.println("Enter Employee Civil Status");
								cstat=s.nextLine();
								System.out.println("Enter Employee Status");
								mstat=s.nextLine();
								System.out.println("Enter Employee Age");
								old=s.nextInt();
								e.add(name,cstat,mstat,old );
								break;
						case 2: System.out.println("The data you entered are:");
									e.display();
									System.out.println();
									break;
						case 3:System.out.println("Enter the position of the array you want to delete");
								pos1=e.nextInt();	
									e.delete(pos1);break;
						case 4:System.out.println("Enter the position of the array you want to update");
								pos1=s.nextInt();
								System.out.println("Enter the name of the employee");
								name=s.nextLine();
								System.out.println("Enter the civil status of the employee");
								cstat=s.nextLine();
								System.out.println("Enter the employee status");
								mstat=s.nextLline();
								e.update(name.cstat,mstat,old);break;
						case 5: System.out.println("Thank you");break;
						default:System.out.println("Please enter the choices given in the menu");break;
						}
							}while(!(choice==5));
							
						
					}
			
			}

What is wrong to this is my second class

import java.util.*;

	public class EmployeeRecord{

	 
	private String name;
	private String civilstat;
	private String empstat;
	private int 	age;
	private int count;

	public EmployeeRecord(String name,String civilstat,String empstat,int age){
		
		this.name=name;
		this.civilstat=civilstat;
		this.empstat=empstat;
		this.age=age;
	}

	//Setters

	public void setName(String name){
		this.name=name;
	}
	public void setCivilstat(String civilstat){
		this.civilstat=civilstat;
	}
	public void setempstat(String empstat){
		this.empstat=empstat;
	}
	public void setAge(int age){
		this.age=age;
	}

	// Getters


	public String getName(){
		return name;
	}
	public String getCivilstat(){
		return civilstat;
	}
	public String getEmpstat(){
		return empstat;
	}
	public int getAge(){
		return age;
	}

		}
		// end of getters and setter

that is my first class what is wrong to that can some one correct me to that

0

Your second class is ok. But it doesn't need a count variable.

But you do not use it at the first class, the main.
Since you want a class that has all the attributes of an employee, you need to create an array of EmployeeRecord. In your code you have an array of strings: String emp[] . Now you will have an array of EmployeeRecord. You will replace that array: emp, with an array of type EmployeeRecord. Wherever you have the emp you will put the new array. Then you will make the necessary changes:

// defining an array
EmployeeRecord [] records = null;

// initializing the array
records = new EmployeeRecord[size];

'records' is an array. Use it like any other array: records.length will get you the length. records[0] will get you the first element.

When you initialize the array you create only the array. Its elements are null. If you want to use them you need to create those as well:

records = new EmployeeRecord[size];



// records is an array
// records[0] is a EmployeeRecord. Meaning that you can do these:

records[0] = new EmployeeRecord();
records[0].setName("name");
records[0].setAge(10);

//OR 
EmployeeRecord er = new EmployeeRecord();
er.setName("name");
er.setAge(12);
records[1] = er;

//OR 
EmployeeRecord er = records[1];

So replace everywhere the String array emp with the EmployeeRecord array and do exactly what you already do, with some changes. For example, when you want to add, you will ask the user the name, the age and whatever you need and you will pass those as parameters:

public void add(String name, int age, Stirng civilstat, ..... ){ // and the rest
	if(!isFull()) {
          records[count] = new EmployeeRecord();
          records[count].setName(name);
          records[count].setAge(age);
          records[count].setCivilStatus(civilstat);
          // And the rest
          
           count++;
        }
	else
	System.out.println("\n SORRY! Elements of Array is full.");
	}

For printing just loop the array and print the name, the civil status of each element.
For searching have as argument the name, loop the array and compare the argument name with the name of each element of the array

0
records = new EmployeeRecord[size];



// records is an array
// records[0] is a EmployeeRecord. Meaning that you can do these:

records[0] = new EmployeeRecord();
records[0].setName("name");
records[0].setAge(10);

//OR 
EmployeeRecord er = new EmployeeRecord();
er.setName("name");
er.setAge(12);
records[1] = er;

//OR 
EmployeeRecord er = records[1];

where should i do this way in the main method...

public void add(String name, int age, Stirng civilstat, ..... ){ // and the rest
	if(!isFull()) {
          records[count] = new EmployeeRecord();
          records[count].setName(name);
          records[count].setAge(age);
          records[count].setCivilStatus(civilstat);
          // And the rest
          
           count++;
        }
	else
	System.out.println("\n SORRY! Elements of Array is full.");
	}

where should i do this in the first class should i have to make another class for the main method

0

In the code I posted I already showed where to put that code. In the same way you define the String array emp, that you have, you will replace it with the array of EmpleoyeeRecord.

It doesn't need a main method to use it. The String is also a class. It doesn't have a main. But you created, called it and used it.

You can create and call any class from anywhere. The main method is only when you want to run the .class file. Apart from that any class can be called and used like any other (example: String).

So just replace that array with the new array of type EmployeeRecord. wherever you have the emp you will put the EmployeeRecord

0

I have never replied to your PMs. In all of my replies I was telling that I answer only to the forum. Why you think that I would do now. And you have already posted your entire code here. Now suddenly you fear that somebody might see it.

Not to mention that completely ignored my previous post.

Your code that you posted here works. You have 2 files. One is the EmployeeRecord that represents one employee and the other is the one with the main. Why did you mix them in the PM. Create an array EmployeeRecord in the main class and call its methods. You can have many different class files and create and call their methods from wherever you want in any class you want.

String is a class. You called it like this:

String s = "aa";
int length = s.length();

You use that String class in your main class.
The same for the EmployeeRecord

EmployeeRecord emp = new EmployeeRecord();
emp.setName("my name");

So in your EmployeeRecordTest instead of having a String array:

private String emp[];

...

emp = new String[5];

...

emp[count] = name;
count++;

Have the EmployeeRecord class:

EmployeeRecord [] emps = new EmployeeRecord[size];

...

emps[count] = new EmployeeRecord();
emps[count].setName(name);
count++;

You don't need to change anything in the structure of the EmployeeRecord.

You have been given examples on how to make an array of your EmployeeRecord class and use it. Given suggestions on what to do and in your PM you ignored them. The code in your PM had nothing to do with what was said previously. It is as if you don't read my codes and tutorials.
Study the previous posts and do what was suggested.

If you don't understand them do some studying on your own. Don't expect to blindly copy the code here and that it will work. That you will get the finished code. You have been given enough examples. More explanations than most text books on how to use arrays. The above code is more than enough to solve your problem. All you have to do is study it, because all I did in this post is repeat my previous post;

So don't expect me to repeat my self again and give explanations that were already given because you are too lazy to try them. I will comment new code but not code that ignores me.

And I have reported your PM, because you kept on sending me repeatedly PMs even if I told you I don't answer to these questions.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.