-
Array Problem
Any suggestions on how to do this? I have a menu:
E - enter person
S - show person
X - exit
Enter Choice: e
Name: John
Age: 20
Sex: M
1. John 20 Male
Enter Choice: e
Name: May
Age: 20
Sex: F
2. May 20 Female
Enter Choice: s
You entered:
1. John 20 Male
2. May 20 Female
Enter Choice: x
I tried it and I used for loop for entering the name but my problem is whenever I asks the name age and sex, it will asks me again to enter the name age and sex without asking me the choice.
example:
Enter Choice: e
Name: May
Age: 20
Sex: F
Name: May
Age: 20
Sex: F
This happens until the array fills up. Any suggestions
Thanks
-
Sounds like your program layout is not quite right.
It should be like this
Code:
Display menu
if choice is add person, call add person method
return to menu
if choice is show person, call show person method
return to menu
Notice that this means that your add person and display person codes should be in their own separate methods. Also make a separate method for displayMenu.
-
Array Problem
Ah ok, thanks. Ill try that.
What if I dont need to go to the menu after entering a person, instead I would ask the user to input what action he/she wants. So that I don't need to show the menu al over again (I can show it when the user requests it).
Example
Enter action (? show actions): ?
N new person
S show people
? show menu
X exit
Enter action (? show menu):n
name: John
age: 20
sex: m
Enter action (? show menu):n
name: May
age: 20
sex: f
Enter action(? show menu):?
N new person
S show people
? show menu
X exit
Enter action(? show menu):s
You entered:
1. John 20 Male
2. May 20 Female
Enter action(? show menu):x
Is this possible using for loop, because when I tried it, it continously asked me about the name age sex, and after the loop is filled, then asks me to "Enter action (? show menu)" - it should asks me this everytime like in the example.
Thanks
-
-
You can use a while loop as well.
While condition should remain true, until user enters x or chooses to see menu.
if user chose to see menu then call the method again just after while loop.
-
am still beginner as you ,so i take the users questions here and i try to solve them by myself to improve my skills ,
.. so sorry i did not complete it , am still have to complete the ArrayList with three
parameter(name,Gender,age) which i think you can do it by yourself .. and the case to exist , i could not figure it out.
so you can take my work (Maybe) it will help you.
Code:
public static void main(String args[]) {
java.util.ArrayList<String> People = new java.util.ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("Please Enter E to Enter new person or to Show a person or X to exist");
String input = input.nextLine();
char m = input.charAt(0);
switch (m) {
case 'E' :People.add("Mohammed");
break;
case 'S':
System.out.println(People);
break;
case 'X':
System.out.println("");
break;
}
}
-
OK thanks Ill try using while