Results 1 to 7 of 7

Thread: Array Problem

  1. #1
    Aiquoc is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Sounds like your program layout is not quite right.
    It should be like this
    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
    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.

  3. #3
    Aiquoc is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default 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

  4. #4
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Last edited by SHE; 03-28-2011 at 06:02 PM.

  5. #5
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    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.

  6. #6
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    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.

  7. #7
    Aiquoc is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. array problem
    By aizen92 in forum New To Java
    Replies: 26
    Last Post: 12-21-2010, 05:42 AM
  2. Array problem please help
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 12-15-2010, 12:38 PM
  3. Array Problem?
    By noobgrammer in forum New To Java
    Replies: 4
    Last Post: 06-19-2010, 11:25 PM
  4. Problem with Array Use
    By Mike90 in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 02:45 PM
  5. array problem
    By jabo in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 09:54 AM

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
  •