import java.util.*;
 public class Employee{
 
 private String emplo [];
 private int count;
 private int size=10;
 private String emp;
 public Employee(int size){
	String [] emplo=new String [10];
	 count=0;
	}
 
	 	public Employee(){
		 this(10);
			 }
		 public boolean isFull(){
		 return count== emplo.length;
		 }
			 public boolean isEmpty(){
			 return count==0;
				 }
			 public void add(int x){
			 if(isFull())
 			 emplo[count++]=x;
			  else
	 		 System.out.println("\n!!!Sorry the Array is Full");
			 }
		 public String search(int x){
		 boolean flag=false;
		 for(int i=0;i<this.count;i++){
		 if ( x.equals(emplo[i]) ){
		 flag=true;
 			x=i;
 		}
 	 
 			if(flag==false){
 			x=-1;
 }
			 return x;
		 }
 	}
		  public  void display(){
		 for(int i=0;i<emplo.length;i++ ) 
 			System.out.println(emplo[i]+"");
 			 System.out.println();
 }
 
   public static void main(String [] args){
   int choice;
  String num1;
  String num2;
 
  Employee e= new Employee();
  Scanner s=new Scanner(System.in);

 System.out.println("Welcome to this Application");

  	do
           {
	System.out.println("[1] Add Name\n[2]Search Name\n[3]Dislay Name\n[4]Exit");
                        	System.out.println("Enter choice:");
	choice= s.nextInt();
      
 		switch(choice){
		case 1:System.out.println("Enter Employee name");
		num2=s.nextLine();
		e.add(num2);break;
		case 2: if(!(e.isEmpty())){
		System.out.println("\nEnter name to search:");
		num1=s.nextLine();
 		System.out.println ( (emplo[i] + "is in "+e.search(num1)));
		}
			else
				
 				System.out.println("\n There are no name to search.\nAdd name first:");
				break;
				case 3: System.out.println("The names you entered are:");
				e.display();
				System.out.println();
				break;
				case 4: System.out.println("\nThank you");break;
				default:System.out.println("\nPlease enter the choices given in the menu:");break;
			}
 
			}while(!(choice==4));
 }

 	}

Can some one help me to solve this......I should input a name of the employee then it should return position of it in the array and it should display name been input..can someone check this its still have an error......

Recommended Answers

All 22 Replies

please send me the correct program of it..

In the constructor you are initializing locally a new String. You need to initialize the one declared globally as a class attribute. The String emplo []; declared at the beginning of your class.

In the code when adding elements you add an element when the array is full which is wrong.

And don't use the equals method to compare primitive ints. Use '=='. What you do in the search method is return the argument. But if it is not found you return -1. But what if you have -1 in the array. That would be confusing. You need to return the index of the array and if it is not found return -1. You can also add a return statement in the for. Once you found it return the index. If you go outside the for-loop meands you have found it so return -1.

For the last errors I mentioned, as well as others you need to read the compile errors. They are very self explanatory. Read them and follow the instructions. Also do some studying on how to declare methods and classes.

Don't post code without explaining what is wrong, what the errors are and without having read them

can you just edit my code and post it...please

commented: That's not how things work around here. -3

this program of mine..for example the user will input a name then it should display and it should add. and it well allow the user add again a name and it well display the location...this program should return string value thanks...

No I cannot edit your code. You have been told what you need to know in order to correct your program. And when you are done with my suggestions post your latest code.

And how many times do I have to tell you. You have sent me 4 or 5 PMs asking for the code. How many times do I have to tell you? Post your question at the forum. Can't you figure that out on your own? I was repeatedly telling you "start a new thread", "post your code at the forum" and you kept sending me PMs which I was doing you a favor to reply instead of ignoring you.

The suggestion you said to me makes my program more complicated....why don't you just help me how to solve it by editing my code...I also made an effort its just that i cannot figured it out...I want a help can...is that for you to answer thats a big favor im asking you I do an effort to that..so help me...

The suggestion you said to me makes my program more complicated....why don't you just help me how to solve it by editing my code...

I don't follow your reasoning. If he edits your code, by applying his suggestion(s) to it, then your code will be the same in complexity as if you'd done the edit yourself. At Daniweb we provide paths to the solution, but it's still your job to make a solution out of the hints given to you.

The suggestion you said to me makes my program more complicated....why don't you just help me how to solve it by editing my code...I also made an effort its just that i cannot figured it out...I want a help can...is that for you to answer thats a big favor im asking you I do an effort to that..so help me...

I told you exactly what you needed to do. It will not make the code more complex. For example in the add method I told you:
"
In the code when adding elements you add an element when the array is full which is wrong.
"
You have in the code:
if(isFull())
emplo[count++]=x;

I told you that you add the element when the list is full, which is wrong. How complicated would make the code correcting that?

In the search I told you to return the index not the element you search. All the changes are 1 line, ONE line.
You didn't bother to read them or try them.

The compile errors, which you didn't post, and you get, are very simple. Just read them and then read what you have written. When you declare a method to return a certain type, your return statement must return a variable of that type.

You made no attempt to fix your code with what I told you. It is as if you don't listen what I say.

commented: You've done enough effort. His lazyness is the bottleneck :P +8
     public void add(int x){
             if(isFull())
             emplo[count++]=x;//this part is the error what should i do to this method
              else
             System.out.println("\n!!!Sorry the Array is Full");
             }
         public String search(int x){
         boolean flag=false;
         for(int i=0;i<this.count;i++){
         if (  emplo[i].equals(x)){
         flag=true;
            x=i;
        }

            if(flag==false){
        x=-1;
 }
             return x;// this part is also the error
         }
    }
        switch(choice){
        case 1:System.out.println("Enter Employee name");
        num2=s.nextLine();
        e.add(num2);break;// this part too has an error
        cas
        System.out.println ( (emplo[i] + "is in "+e.search(num1)));//this part error
        System.out.println ( (emplo[i] + "is in "+e.search(num1)));//THIS PARTC:  
        System.out.println ( (emplo[i] + "is in "+e.search(num1)));//here error// what should i put make my program correct please change to make it correct 

because i well be the one to change that it well make more complicated because i dont how to change it because i think its correct ..so please correct it for me...

As explained, what you do here is this:

if(isFull())
emplo[count++]=x;

If the array is full you go and add the element x. Now since you wrote those methods I find it strange that you can't understand what needs to be done.


----
return x;// this part is also the error
----
As explained look at the compile errors. You have declared the method to be of type String and the x that you return is of type int. Those need to mach. Check the requirements what that method needs to return.


Post the new code. Look at the errors and use them to correct the code. Since you wrote all that code, I am surprised that you can't fix those little mistakes because they are very simple compare the complexity of the rest of your code.

I have already solve the problem to the code i have posted...then i just want to add something on my code...what if i also want to input the civil status of the employee and the age of the employee what should I add to my code...
Because the code i have right is just to input the name of the employee.... Please give the hints what should i right and please also put the correct syntax on how to do it...

You should create a separate Employee class in which you will hold all the information of the employee. Since you already hava an Employee class, I would suggest a different name.
Let's say you have this:

public class Emp {
  private String name = null;
  private int age = 0;

  public Emp() {

  }

  // Add an initializer constructor that takes as arguments the name and age

 // Add get,set methods for the name and age
}

Now that you have this you can replace in your code the array of String names with this:

public class Employee{
 
 private Emp emplo [];
 private int count;
 private int size=10;

....

 public Employee(int size){
	emplo=new Emp[10];
	 count=0;
	}
 
	 	public Employee(){
		 this(10);
        	}

....
}

Remember that emplo is an array and emplo is an Emp class. So now when you add elements to the array add Emp instances. Ask for the user to enter the name and the age, create an Emp instance and add that to the array.
The same for printing. Loop the array and print the attributes of each Emp element
For the search loop the array and compare the name of each Emp element with the name argument

can you jus help how to add it on my code because my teacher only want one class..

I already solved the errors to the program i just posted and I'm i just to add something on it.methods the methods that is have on my program i posted on here is only
the add method,search method display method...and i wanted to add a delete method and the method on how to update the object in the code..and i also want to add that the user could only input the names of the employee and also the civil status of the the employee..in just one class and i don't know i'm asking your suggestion and the syntax how to do it and please post the correct on how to add it

If you want only one class, then add more arrays. You have one for the name. Add one more for the age or the civil status. All the arrays must have the same length and when you add something to one of the arrays you must do the same to the others in order to make sure that all of them will have the same amount of data.

So you probably don't want to do this:
emplo[count++]=x

But this:

name[count]=na;
age[count]=ag;
count++;

Where na, ag are the arguments that the user enters. For updating just ask the user the name of the employee, search for that name, find at which position it is, and change the value of the elements of the array at that index.

can you just add it on my program i don't have any idea what saying how to do it...add the it on my program....please then i well the one to continue it..please javaaddict your expert into it..try to help because im just really new to java....

If you want only one class, then add more arrays. You have one for the name. Add one more for the age or the civil status

I already added it to your code. Check my example. And the suggestion is pretty clear to me (You have one for the name. Add one more for the civil status). The solution has already been given to you. Implement it.

If you wrote the code with the String array for the name, surely you can do the same for the rest.

i really dont get it..what i mean insert it into my code because i was so confuse to it...

You have sent me so many PMs I have lost count. They all ask the same thing and I have given to all the same answer. How many negatives replies must I send in order for you to understand that I will not send you code by PM? You think that by asking the same thing over and over again, it will make me change my mind and send you the code. Why?

Also I find very hard to believe that someone that has written that good initial code such as yourself, can't understand those simple suggestions I gave?

You have in your code an array to store the names. It is strange that you cannot add more arrays to store whatever you want, as I suggested.

No one here is going to give you the complete code. You already have that. And you have the solution, which I gave. Make the necessary changes that you can make, and post the updated code with your new questions.

I have no idea how you have put up with him this far javaAddict. He clearly does not want to learn and clearly did not write that code himself.

>>I have no idea how you have put up with him this far javaAddict. He clearly does not want to learn and clearly did not write that code himself.

That's the proof that he's worth his Featured Poster-badge :)

and clearly did not write that code himself.

That was my thought. That is why I mentioning all those hints, why he wrote all that code and couldn't understand those simple advices. I am with a friend now and he has a degree in Theological studies and he could figure out the solution and how to fix the errors!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.