Jump to content

How to store data in array and return match using Java??

- - - - -

  • Please log in to reply
2 replies to this topic

#1
joeyyu22

joeyyu22

    Newbie

  • Members
  • Pip
  • 2 posts
Hello, I need some help with a question. I not a very good programmer myself so plese go easy on me. I'm a year 1 student in diploma in Information Technology.

The question goes like this

Write an application that will read in people names and their ages. Store them in two arrays. Then do a search of names to find the corresponding age. Follow these steps to write the programme:
a) Create a class BasicsApp4.java with a main() method
b) Add a readAge() method
c) Add a readAges() method
d) Add a readName() method
e) Add a readNames() method that will call readName method to store 5 names into an array called nameArray
f) In the main method, do the followings:
-Declare ageArray and nameArray with size of 5
-Call/Invoke readNames() and readAges() methods to read in 5 names and ages
-Ask the user to enter a name and search for the age
-Display name and age if found, else display name not found
-Ask user to enter an age and display all the names with the same age

Here is what i done so far, I'm not very sure if where i had gone wrong and i did not know how to continue.

public class BasicApp4 {


	public static void main(String[] args) {

		// TODO Auto-generated method stub

		int[]ageArray = new int[5];

		String[]nameArray = new String[5];


		readName(nameArray);

		


	}



	private static void readName(String[] nameArray) {

		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);

		System.out.print("Enter name: ");

		name = sc.nextLine();

		

		

       

	}


	private static int readAge(int[] ageArray) {

		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);

		int age;

		

		do {

		System.out.print("Enter age: ");

	    age = sc.nextInt();

		}

		while ((age<0)||(age>100));

		return age;

		

	}

	private static void readAges(int[] ageArray) {

		// TODO Auto-generated method stub

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

			

		}

		ageArray[0]=readAge(null);

	}


}

Edited by Alyn, 30 October 2011 - 03:21 AM.
added code tag


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Judging by the question I would create the methods like so:
private static String readName(){
    //read and return name;
}

private static void readNames(String[] names){
   //use readName() in here .. 5 times (loop)
}

private static int readAge(){
    //read and return age;
}

private static void readAges(int[] ages){
   //use readAge() in here .. 5 times (loop)
}

public static void main(String[] args){
   //readNames(...);
   //readAges(...);
    /*
   -Ask the user to enter a name and search for the age
   -Display name and age if found, else display name not found
   -Ask user to enter an age and display all the names with the same age
*/

}

Note that in your current private static void readName(String[] nameArray) { method, the variable "name" does not exist and that won't compile.

#3
joeyyu22

joeyyu22

    Newbie

  • Members
  • Pip
  • 2 posts
Thank you. I understand what i need to do now. Thank you very much.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users