954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

array of program prob.

import java.util.*;

public class PhoneDirec{
	private int MAX=10;
	private String [] validNames={"Gary","Sam","John","Jude","Alyssa","Matt","Eric","Susie","Alics","Sara"};
	
	PhoneDirec(){
	//allocate memory to names
	 validNames=new String[MAX];
	}// accept 10 names
	//I suppose to enter 10 names
 	public void accept(){
      Scanner console = new Scanner(System.in);
     for(int n = 0;n< names.length;n++)
       names[n] = console.nextLine();   
      }
		/*I also to suppose to search a names and if 
		names I search is valid it should display the names
		*/
		
		public void search(){
		String strNames;
	 String namesEntered;
		boolean validNames=false;
       System.out.println("Enter names");
		 namesEntered=console.nextLine(strNames);//this part have an error
	for(int x=0;x<validNames.length;++x)
	{
		if(namesEntered == validNames)//this part have an error

		 {
		 validNames=true;
		 }
		}
	if(validNames)
	 {
	 System.out.println("The names entered"+namesEntered);
	 }
	 
	 else
	 {
	 System.out.println("The names you entered is invalid");
	 
		 	public void display search(){
	/* i dont know what to do next
	can someone help to this part*/
		 }
		 	}

can some one help me this i I well really be happy if someone can help me this i was really stuck in here

churva_churva
Junior Poster
135 posts since Aug 2010
Reputation Points: 1
Solved Threads: 0
 

You need to loop through the array of valid names testing each one against the entered name. If you find a match you can stop searching. If you reach the end of the loop and you haven't found a match then the name is invalid.
Compare Strings by using the String equals method.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

yeah but how i dont how to loop ..can you help me edit code

churva_churva
Junior Poster
135 posts since Aug 2010
Reputation Points: 1
Solved Threads: 0
 
yeah but how i dont how to loop ..can you help me edit code

You do know how to loop you did it in your program already. Something like the following should work for you but I've left the else statement free for you; its more the if statement you want:

String [] validNames = {"Gary","Sam","John","Jude","Alyssa","Matt","Eric","Susie","Alics","Sara"};
		 
		 Scanner in = new Scanner(System.in);
		 
		 System.out.println("What name are you searching for?");
		 String name = in.next();
		 
		 for( int i = 0; i < validNames.length; i++)
		 {
			 if( validNames[i].equals(name) )
			 {
				 System.out.println(name+" exists at position "+i);
			 }
			 else{
				 
			 }
		 }


Does this help you at all?

Katana24
Junior Poster
180 posts since Mar 2010
Reputation Points: 13
Solved Threads: 14
 

Yes you do, you have at least 2 in your code already.
Around line 29 you try to compare namesEntered with the whole validNames array, but you should just compare with the i'th element of that array.
And note my previous about comparing Strings.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

@Katana.
Please observe the rules for DaniWeb. Do not give the O/P a coded solution to his problem - that will teach him nothing, except how to cheat at homework.
If you want to help, give him some guidance so he can learn to use the reference material, and will remember what he has learned.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
@Katana. Please observe the rules for DaniWeb. Do not give the O/P a coded solution to his problem - that will teach him nothing, except how to cheat at homework. If you want to help, give him some guidance so he can learn to use the reference material, and will remember what he has learned.

My apologies :P

Katana24
Junior Poster
180 posts since Mar 2010
Reputation Points: 13
Solved Threads: 14
 

OK man. No worries.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Its really helps the code that you recorrect my code and one i want to ask you again. i want to search three times.not just once..how would i change it...

churva_churva
Junior Poster
135 posts since Aug 2010
Reputation Points: 1
Solved Threads: 0
 
Its really helps the code that you recorrect my code and one i want to ask you again. i want to search three times.not just once..how would i change it...

So you want to do the same thing 3 times? Can I ask why - when once would yield the answer?
If you want to do it three times then you could be it inside another for loop - called a nested for loop.
Its easy to make - just use the same for loop structure but the second value like - "is i less then 3?"

for( int i = 0; i < 3 ; i++)
{
// Rest of code
}


Be careful of how nested for loops work. This website will help you understand them: http://mathbits.com/mathbits/java/looping/nestedfor.htm

Happy Coding

Katana24
Junior Poster
180 posts since Mar 2010
Reputation Points: 13
Solved Threads: 14
 

@ katana please help me with my program theres really something wrong with it.an i really don't it could you help me..

/* This program would use console/GUI,*/
/* It should 
1 adding of object
2 display of object
3 updating of object
 4 deleting of object
 
*/

import java.util.*;

public class EmployeeList{

	private int [] Age;
	private String [] EmpfirstName;
	private String [] Emplast;
	private String [] Adrss;
	static Scanner console= new Scanner(System.in);
	
  public EmloyeeList(){
	// what should i put this part
   }
	/* I want to enter the first name of the employee please check if the method is 
	  right*/
	public void EmpfirstName(String EmpfirstName){
	for(int i=0;i<EmpfirstName.length;i++)
	 System.out.println("Enter Employee first name");
	 EmpfirstName=console.nextLine();
	 System.out.println(EmpfirstName[i]);	
		}
	/*I want to enter the last name of the employee here same please if the is right*/
	 public void EmplastName(String  EmplastName){
	 	for(int i=0; i< EmplastName.length;i++) 
		System.out.println("Enter Last Name");
		 EmplastName=console.nextLine();
		 System.out.println( EmplastName[i]);
		 }
		 	/*I want to enter the Address of the employee here same please if the is right*/

		 	public void Adrss(String  Adrss){
			for(int i=0;i< Adrss	.length;i++)
			System.out.println("Enter your Address");
			 Adrss=console.nextLine();
			 System.out.println( Adrss	[i]);
			 }	/*I want to enter the last name of the employee here same please if the is right*/

		public void Age(int age){
		 for(int=i;i<Age.length;i++)
		 System.out.println("Enter your Age");
		 Age=console.nextInt();
		 System.out.println(Age[i]);
		 }
		/* Please check if it is right because i was really confused
		thanks ...*/
		
		}
churva_churva
Junior Poster
135 posts since Aug 2010
Reputation Points: 1
Solved Threads: 0
 

churva churva, can you please tell me where/how you are learning java? Are you self studying or being tutored? Or are you in an institute?

Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

I'm in school but..I dont really get it right away when my teacher discussing,because my teacher just give simple program example..then he well let us solve difficult programs for us to solve thats why it is difficult for me to solve right on my own....i hope you can help me..

churva_churva
Junior Poster
135 posts since Aug 2010
Reputation Points: 1
Solved Threads: 0
 

Teachers are not there to be your enemy. I'm sure if you asked your teacher to sit down with you and try to explain what you do not understand he will be quite happy to do so. Even if he already explained what you need to do and you still do not understand, do not feel intimidated to just ask for extra help! He cannot say no, and you cannot get into trouble just for not understanding something!

Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

But im shy to do that yeah maybe next time i well try to ask

churva_churva
Junior Poster
135 posts since Aug 2010
Reputation Points: 1
Solved Threads: 0
 

You're confusing the main class with user-defined classes.

You currently have one main class. Main classes typically do not need constructors (the public [classname]() thing), and they typically have a public static void main(String[]args) method - the main method.

Make those changes, make sure you have all the right braces in the right places, and it should work.

coil
Posting Whiz in Training
273 posts since Aug 2010
Reputation Points: 27
Solved Threads: 56
 

You're confusing the main class with user-defined classes.

You currently have one main class. Main classes typically do not need constructors (the public [classname]() thing), and they typically have a public static void main(String[]args) method - the main method.

Make those changes, make sure you have all the right braces in the right places, and it should work.

coil
Posting Whiz in Training
273 posts since Aug 2010
Reputation Points: 27
Solved Threads: 56
 

I took a look at your program and its filled with errors.

First - your arrays don't have size. When you say this:

for( int i = 0 ;i< EmpfirstName.length; i++ )


But when you declare that array you don't specify how large it should be, nor do you specify the other arrays length. Because you are just starting off with java i think that, to keep things simple, you should make the array 10 elements long, like this:

private String [] EmpfirstName = new String[10];


Or however long you want. By doing this, your code:

i < EmpfirstName.length;


will actually work in the methods that follow, once you add in the brackets to indicate that your calling a method.

i < EmpfirstName.length();


I would also change "Scanner console" to something like "input" or "in"
Your code is also filled with spelling mistakes.

Katana24
Junior Poster
180 posts since Mar 2010
Reputation Points: 13
Solved Threads: 14
 

Sorry that length() thing is wrong :P

Katana24
Junior Poster
180 posts since Mar 2010
Reputation Points: 13
Solved Threads: 14
 

Ok - assuming I have understood what you want to do, I recommend revamping your code and getting rid of the constructor, for the meantime, until you understand it completely - (im not trying to be condescending if I seem like i am :D )

I think you should use the following pattern to get the people's forename's, surname's, age etc:

public static void EmpfirstName()
		{
			System.out.println("Please enter the employee's forename's names: ");
			
			for( int i = 0; i < EmpfirstName.length; i++ )
			{
				EmpfirstName[i] = in.next();
			}
			
		}


Also the variables and arrays should be declared as static too:

private static String [] EmpfirstName = new String[5];


Next, use this method to print the contents of the array, to verify that the names and what-not have been written to the array successfully.

public static void printArray(String [] myArray)
	  {
		  for( int i = 0; i < myArray.length; i++)
		  {
			  System.out.println("At position "+i+": "+myArray[i]);
		  }
	  }


You will notice that the method doesn't have any parameters - it doesn't need them. I hope this helps abit :P

Katana24
Junior Poster
180 posts since Mar 2010
Reputation Points: 13
Solved Threads: 14
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: