import java.util.*;
public class EmployeeNames{
	private String EmpYee [];
	private int count;
	int x;
 
 public EmployeeNames(int size){
 	String EmpYee []=new String [size];
	count=0;
	 }
  public EmployeeNames(){
  this(10);
   }
	
	public   boolean isFull(){
 	return count= EmpYee.length;
 }
 	  public boolean isEmpty(){
	   return count=0;
	  }
	 public void add(int x){
	   if(! isFull())
			EmpYee[count++]=x;
		else
		  	System.out.println("\n Sorry! Elements of Array is full:");
	  }
	     public int search(int x){
		 
		  boolean flag=false;
		     for(int i=0;i<this.count;i++)
			      if(names.equals(EmpYee[i])){
					   namesWasFound=true;
						 x=i;
						}
					}
				if(flag=false)
					x=-1;
					
				 
					return x;
				}
					}
			public void display(){
				for(int i=0;i<EmpYee.length;i++)  
				   System.out.println(EmpYee[i]+ "");
					}
			 
					
		public static void main(String [] args)}{
					int choice;
			String names;
			String num2;
			EmployeeNames e= new EmployeeNames();
			Scanner s= new Scanner (System.in);
			System.out.print("Welcome");
			  do
			  		{
						System.out.println("\n[1]ADD NAMES\n[2] SEARCH NAMES\N[3]DISPLAY NAMES\n[4]EXIT");
						System.out.println(" Enter Choice");
						choice=s.next();
						switch(choice)
							{
								case 1: System.out.println("\n Enter Names");
											num2=s.next();
											e.add(num2);
											break;
								
								case 2: if(!(e.isEmpty())){
										     System.out.println("\nEnter names to search");
											  names=s.next();
											  System.out.println(names+"is in position"+m.search(num2));
											}
											else
											  System.out.println("\n There no names to search:\n Add names first:");
											break;
								case 3: System.out.println("\n Names Entered are:");
											e.display();
											// System.out.println();
								case 4: System.out.println("\nThank You!!:");
										break;
								default: System.out.println("\n Please enter choices given in menu:");
											break;

 
											}while(!(choice==4));
 
													}
												 }

Can someone here teach how to get the position of the array...

what do you mean, the position of the array?
do you mean the position of a certain element in the array?

[ada][dasd][sfafsaf][sfsfaf]

and i want to ...ada....
i want to get the position of ada in the array...can you give the syntax according to my code for as a giude..

since you're using an array of String objects, there's not really an indexOf(String el) method, so I think the easiest way to do this, is going over the entire array, starting with the first element, and comparing each element with the one you want to find.

once find, break out of the loop. off course, if that element occurs several times, you'll only have one (most likely the first) occurence of this element found.

so what should i do...can you give the syntax on how to do it..thnks..

ehm ...
if you know how to run through a loop (for, while, do ... doesn't really matter which one)
you know all there is to know

repeat the next steps for every element in your array (or until you've found the element you're looking for)
look at the Nth element:
element equals yourElement? yes: save the location and break no: next element

The difinition of the two methods from the line 15 to line 20 has some problem.

The line 16 should be:
return count== EmpYee.length;
The line 19 should be:
return count==0;

@stultuske: idon't get it.
@tong1:it look ok..

hey what if i also wanted to add on my code ..that the user also allow to enter employee status what would i add to my program?

I've got the same problem

@stultuske: idon't get it.
@tong1:it look ok..

as for my part:

// declare a new Array, which can contain 5 String objects
String[] arr = new String[5];
// start a for-loop, that will cycle over all elements
// notice that the first element's index is not 1, but 0
String a = "";
for ( int i = 0; i < 5; i++ ){
  a += "a";
  // put a in the next empty place in the array
  arr[i] = a; 
}

// now you have "a";"aa";"aaa";"aaaa";"aaaaa"
// so, now to search "aaa";
String search = "aaa";
int location = -1;

for ( int i = 0; i < 5; i++ ){
  if ( arr[i].equals(search)){
    location = i;
    break;
  }
}

if ( i != -1 )
  System.out.println("The String was found on index " + location);
else
  System.out.println("The String was not in the array");

As for Tong1 's remark:
it doesn't look OK, and he's right.
in those places, you'll need to change the '=' to '=='

hey what if i also wanted to add on my code ..that the user also allow to enter employee status what would i add to my program?

I would suggest you to create an object Employee, which contains the variables
name (a String object) and status (sort depends on how you whish to implement it.

In this link:
http://www.daniweb.com/forums/thread310512.html
you asked the same thing. You got the answer for the question you posted here and said you understood it and then asked other questions. After all of my suggestions which you claimed you figured them out and have fixed your code, now you post the same initial code, without implementing any of my suggestions.

I see the same errors that I told you to fix and you said that you fixed them. What was all that about?


Here another time what you need to do:
You already have this code:

public int search(int x){
		 
		  boolean flag=false;
		     for(int i=0;i<this.count;i++)
			      if(names.equals(EmpYee[i])){
					   namesWasFound=true;
						 x=i;
						}
					}
				if(flag=false)
					x=-1;
					
				 
					return x;
				}
					}

If you want the name, why don't you pass that as argument? Where is names defined? Just pass the name as argument, loop the array until you find it. Then return the index that it was found. The place the name was found in the array. If it is not found then return -1. You already do most of those parts in your code.

And look back at the answers given to you. You still don't initialize the right EmpYee array variable.
And every time you have a new question post your updated code.


As for your PM:

If you want to add a "Civil Status" attribute or an "Age" attribute for the employee, start a new thread, because you still have issues with this code and I see no reason why we should mixed them together. If we all start telling you how to fix this code and how to add an Employee object with those attribute it will get confusing since this code still doesn't work.

And most important:
I clearly explained at that thread, that after so many PMs that you send me, I have never given you the code. I was given you the same reply: "Post your questions at the forum". I concluded by saying that I will never send you the code by PM no matter how many times you PM me. And today you sent me yet another PM asking the same thing which I have given you the same answer. When are you going to understand that no matter how many PMs you send, I answer only questions asked at the forum.

You can report such PMs as a violation of the forum rules:

Keep It Organized
Do not ask anyone (member or moderator) for help by email or PM

I already solve the problems to code the i have posted recently...but i really want to add on my code..on how to add the civil status and the age of the employee..but i dont know how can some one help..

In the other thread you asked the same thing and received answers.

You said once that can not use 2 different classes as your teacher has instructed, but at your PM you asked how to use 2 classes. One that has those attributes that you want and the other the main.

Now which one is it?

i really have to use to classes .But can you tell how to do it i would like to add on my code to add civil status and also the age...i know how to create two classes but i don't how to create main page in this two class so that i could able to test them if this is work....

ehm... maybe you should take your notes from the first classes where you actually had to code something.

with a desktop application, only a main class can be run, and it 's this kind of class you need

As explained before create a separate class, EmployeeDet for example.

class EmployeeDet  {
   private String name = null;
   private String civilStatus = null;
   // add more if you want.

   public EmployeeDet() {
   }

   public EmployeeDet(String na, String civSt) {
     // give the values of the arguments to the attributes of the class (name)
   }

   // write get, set methods to get/set the values of the attributes of the class
}

For more information about creating classes with constructors and get/set methods read some tutorials.

Now that you have your class just instantiate it. You don't need a main method to run it. Just use it in any other method or class like you do with the java classes. EmployeeDet doesn't need a main method. You will call the constructor and its methods in the one main method you have created, like you call any other method.
In your code, you do this: at the main: EmployeeNames e= new EmployeeNames(); You could have done that even if the EmployeeNames class didn't have a name. If EmployeeNames was in another class and the main to a different one you still could call it.

So in your case, you have in the EmployeeNames class this: private String EmpYee []; That is an array of Strings and you save the name inside. Now you will use an array of EmployeeDet:

private EmployeeDet [] employees = null;

....

// and in the constructor:
employees = new EmployeeDet[size];

employees is an array and you created it with the above code. But employees is not an array, it is a EmployeeDet. So you need to create that too:

employees[count++]=new EmployeeDet();

You must read the name, the civil status, ... and pass them as arguments and create the EmployeeDet in order to add it to the array:

employees [count++]=new EmployeeDet(name, civilSt); // read from the keyboard

// OR

employees[count]=new EmployeeDet();
employees[count].setName(....);
employees[count].setCivilStatus(....);

count++;

employees[count] is an EmployeeDet so you can call its methods. Those methods need to be created at the EmployeeDet class as instructed.

For searching just loop the array and compare the argument name, with each name of the employees array.

search(String name) {
 ....
   if (names.equals(employees[i].getName())) {

   }
 ....
}

-- PS --
That is all you need to know based on your question. Everything has been covered and there is no room for misinterpretation. The next step would be to do some studying to understand those suggestions and use them to update your code. For any more questions post your updated code. And since you say that you have fixed all the issues you had, those shouldn't be a problem

yeah..i already got it what are you trying to say..but what i want to know now how to update the object in array can you give me the syntax on how to do it..please thanks..

When you were adding elements to the array you were doing this: employees[count++]=new EmployeeDet(name, civilSt); where the name and civilSt were read from the keyboard.

You will do the same, read the new name, civilSt from the keyboard. The only difference is that this time you will not add the EmployeeDet to that array after the previous one entered but you will change the value of one already been inserted.

Assuming that you have the name to be unique and since you use that for searching then.
Ask the user to enter the name whose employee you want to update.
Loop the array and find the position in the array of the employee with that name.

But wait you already have that. You have the method search, which takes as argument the name and returns the position that the employee with that name has in the array. int index = search(name); Where name is the name you want to search (entered from keyboard) and index is the position of the array

Anyway regardless how you find where the employee with that name is,

After you have entered the name of the employee
Found where that employee is (position index)
Ask for the user the new values.
Then change the value of that array's element employees[index]=new EmployeeDet(newName, newCivilSt);

public void update(int pos,String x,String y){
		if(!isEmpty()){
			emp[pos]=x;
			empc[pos]=y;
			System.out.println("done");
		}
						e.delete(pos1);break;
		case 5: System.out.println("\nEnter Employee Pos: ");
                        pos1=s.nextInt();
						 
				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;

		else
		System.out.println("array is empty");
/*And to display this..is like this

please check....can i just send to you my code I cannot post it the forum...

e.delete is not includend

First of all you are mixing the if in the method with the else at the switch. You do this:

public method () {

  if () {

  }

}


else {

}

which is wrong. The if closes in the method and the else that you have has no if. Also the delete is also wrong. It is outside the method and it belongs to another case.

Have one method:

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 then call it:

case 5: System.out.println("\nEnter Employee Pos: ");
                        pos1=s.nextInt();
						 
				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;

Also the nextInt will not work.
When you call the nextInt it reads the number you enter, but when you call the nextLine, it reads whatever you enter and the new line character.
So if you enter a numnber : 12 and press enter: <enter>

Enter Employee Pos
12<enter>

The next int will read only the 12. The enter will not be taken into account.Then when you call the nextLine to read the name, if you give a name and press <enter> it will not read the name, but the previous enter. Because when you entered the number previously you only read the number, and didn't change lines.

So the right code would be:

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;

Also you need to name your variables differently because they are confusing.

okie..its actually working....but when I update the employee name and civil status...the results of the employee name has been the result of the civil status and the results of civil status has been the results of the employee name..in short there results exchanging itself...why..

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

You will not add anything. I told you what to do. If you read the name, then put it into the array that you store the names, not the civil statuses.

what is the reason i have to put nextLine();
e.delete(pos1);break;
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;
thanks

e.delete(pos1);break;
		case 5: System.out.println("\nEnter Employee Pos: ");
                        pos1=s.nextInt();
 			 s.nextLine();//What the reason I should put nextLine();this place
				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;
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.