-
October 28th, 2011, 10:44 AM
#1
beginner-urgent java help needed
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);
}
}
-
October 28th, 2011, 01:47 PM
#2
Re: beginner-urgent java help needed
I not a very good programmer myself
Part of your problem is not the programming it's reading and implementing the basic instructions.
e) Add a readNames() method that will call readName method to store 5 names into an array called nameArray
Where is this method?
-Call/Invoke readNames() and readAges() methods to read in 5 names and ages
Where are these calls and why is there a call to readName()?
Why is your readAges method calling readAge and passing null. If you don't need to pass the array in (and you don't) your readAge method shouldn't have any parameters. Why, also, does the method have a for loop which does nothing and then after the loop you call readAge(), surely this call should be inside the loop so you get 5 ages.
Once you have readAge() and readAges() working apply the same logic to readName() and readNames().
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|