Results 1 to 7 of 7
Thread: Array Problem
- 03-28-2011, 08:06 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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
- 03-28-2011, 08:12 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Sounds like your program layout is not quite right.
It should be like this
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.Java 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
- 03-28-2011, 08:59 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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
- 03-28-2011, 04:20 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
what about switch - case ?
http://download.oracle.com/javase/tu...ts/switch.htmlLast edited by SHE; 03-28-2011 at 06:02 PM.
- 03-28-2011, 04:29 PM #5
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
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.
- 03-28-2011, 06:47 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
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.
Java 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; } }Last edited by SHE; 03-28-2011 at 06:56 PM.
- 03-29-2011, 04:03 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
array problem
By aizen92 in forum New To JavaReplies: 26Last Post: 12-21-2010, 05:42 AM -
Array problem please help
By newToJava3 in forum New To JavaReplies: 2Last Post: 12-15-2010, 12:38 PM -
Array Problem?
By noobgrammer in forum New To JavaReplies: 4Last Post: 06-19-2010, 11:25 PM -
Problem with Array Use
By Mike90 in forum New To JavaReplies: 1Last Post: 06-02-2010, 02:45 PM -
array problem
By jabo in forum New To JavaReplies: 2Last Post: 03-31-2010, 09:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks