Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Array and Menu

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Exclamation Array and Menu

    Hi i made a speed dial program
    i made it so the user can input names using an array .. so it has an input from 0 -9
    now i have two sections for the menu "input names" and "search names"
    i can get the user to input names, but it wont .. link? with the second option on the menu so i cant show what the user chose as names from the first option
    here is my code:

    import java.awt.*;
    import hsa.Console;
    import java.io.*;
    import java.text.*;
    import java.util.Random;
    public class speeddial
    /*
     
    *Module 12 Assignment
    *July 20 2011
    *Question Number 2
    */
    {   
      static Console c = new Console ();
      public static void main (String[] args)
       {
            int choice;
            do
            {
                //MENU
                c.print("Main Menu");
                c.print("\n1. Enter Speed Dial Data");
                c.print("\n2. Speed Dial Search");
                c.print("\n3. Exit Program\n");
                c.print("Please enter a choice (1-3)\n");
                choice = c.readInt();
                if ((choice < 1) || (choice > 3))
                c.print ("\nPlease choose a number between 1-3 only");
                if (choice == 1) option1();
                if (choice == 2) option2();
                if (choice == 3) option3();
             }
             while (choice != 4);
     
       }
           //Option 1
           public static void option1()
    {
           String name[] = new String [10];
            c.println("\nEnter Speed Dial Data\n");
            c.print("Enter names for speed dial in the order you would like them to be in (0-9)\n"); 
                for (int i = 0; i < 10 ; i++)
                { 
                name[i] = c.readLine();
                c.print("Please enter the next name");
                c.print("\n");
                }
                c.print("\n");
     } 
     
           //Option 2
           public static void option2()       
           {
           c.print("Search Speed Dial Names\n");
           c.print("The name you have selected for speed dial number 0 is " + name[0] + "\n");       
           }
     
           //Option 3
           public static void option3()
           {
           c.print("\nThank You for using Speed Dial.\n\n");
           }
     
    }

    some one pleaseeee help!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Array and Menu

    The name array has been declared inside the option1 method. Therefore you cannot access it outside that method. What you should do is make use of Objects and declare the array as an instance variable.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Array and Menu

    is it not possible to do it with an array?
    cuz the whole point of the assignment is to use arrays ...

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Array and Menu

    Yes you use the array but variables have scope. If you don't know what that is then research it.

    Other problems I see.

    When user selects option 1 they enter all their data. Then it goes back to the menu. If user selects option 1 again then all the data they just entered is discarded as you create a new array. This can be corrected if you do as suggested above and use an instance variable.

    The option1 method forces user to enter 10 names. What if they only have 4? What if they have 20?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Array and Menu

    umm well it asks the user to enter 9 names cuz thats how many digits a phone has
    and thats how many our teacher sed to put

    but i was also wondering .. do u think its possible to make it so that right when the user enters all 9 names.. it takes them automatically to option 2?
    is that possilble and do u think that will work?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Array and Menu

    Quote Originally Posted by nitwit3 View Post
    umm well it asks the user to enter 9 names cuz thats how many digits a phone has
    and thats how many our teacher said to put
    Actually it has 10 and you program is asking for 10 but if I only want to enter 2 values why does your program force me to add a further 8 values?

    but i was also wondering .. do u think its possible to make it so that right when the user enters all 9 names.. it takes them automatically to option 2?
    is that possilble and do u think that will work?
    Yes it is possible but that is not the point of the exercise.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Array and Menu

    yea i understand ....... im just completely stuck ... i dont think i should use the instance variable because the the instructions clearly state that i have to use an array variable ... so im just .. really confused and have no idea what i can do now!

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Array and Menu

    You clearly do not understand what an instance variable is. It is a variable that belongs to an instance of the class (an object). Instance varaibles can be int, long, double, String, array, Point, Foo, any type you want. Whereas you have declared your array as a local variable. It is local to the method it has been declared in.
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Array and Menu

    The first thing I would do is declare your array globally since this is a small class. I wouldn't do this when you get into larger classes as it can get confusing if you have to many. Once you declare it globally and have populated the array all methods in the class will have access to it. In option2 you never ask the user to input a number to get the correct name that they want. Once you get what number they want you have to subtract it by one to get the proper index of the name. Indexes start at 0 not 1, so if the user enter 1 and you don't subtract 1 the second name in the array with be printed. In the c.print line you have names[0] this will always show the first name. You need to put in a the index that you want to print.

    something like

    c.print("Search Speed Dial Names\n");
    int speedDialNum = c.readInt();
    speedDialNum -= 1; //this will give you the proper index


    then print names[speedDialNum]


    two other things i noticed is that in you do while loop it continues until 4 is enter yet you tell the user to enter 3 to exit and you are actually setting the size of the array to 10 names not 9

  10. #10
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Array and Menu

    Quote Originally Posted by banny7 View Post
    The first thing I would do is declare your array globally since this is a small class.
    Java doesn't have global variables. I expect you mean declare it at class scope, i.e. as a class variable.

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Array and Menu

    Yea i just always called them global because one of my teacher did and it just got stuck in my head

Similar Threads

  1. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM
  2. problem with help menu.
    By Toggo in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 12th, 2010, 05:13 PM
  3. Menu of Menus Help
    By Strivelli in forum Java Theory & Questions
    Replies: 10
    Last Post: June 5th, 2010, 02:40 PM
  4. popup menu
    By antonio69 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 04:24 AM
  5. Help with Menu/BinSearch
    By jhar131 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 19th, 2010, 07:38 PM