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 4 of 4

Thread: Switch problem. How do I return to my menu?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Switch problem. How do I return to my menu?

    I have written this program, a menu with a switch. When any of the options have been executed, except from 11 (End), I would like to get the menu displayed again. Now, when I run the program it stops after the task is finished. I have to run the program again, and my list is empty.

    Any suggestions?? Thank you

    package oblig1;

    import java.util.Scanner;

    class Menu {
    public static void main(String[] args) {

    SingleLink l = new SingleLink();

    String [] options = {
    "Delete an element first in the list",
    "Add an element in the end of list",
    "Delete an element in the end of list",
    "Delete an element with given value from the list",
    "Add an element after an element with given value",
    "Add an element in front of an element with given value",
    "Print the length of the list",
    "Count Number of elements with given value in the list, \n\tand print the Number ofelement",
    "Print the whole list",
    "Delete whole list",
    "End"

    };

    System.out.println("What would you like to do? \n");
    for (int i = 0; i < options.length; i++) {
    System.out.println((i+1) + ":\t" + options[i] + " ");

    }

    Scanner input = new Scanner(System.in);
    int option= input.nextInt();

    while (option != 11)
    switch(option) {

    case 1:
    l.remove(null);
    break;

    case 2:
    System.out.println("What value should the element have? ");
    int value = input.nextInt();
    l.insertLast(value);
    break;

    case 9:
    //
    break;

    case 10:
    l.findSize();
    int a = l.findSize();
    l.deleteAll();
    System.out.println("Number of elements Deleted : " + a);
    break;

    case 11:

    }
    }

    }
    Last edited by jonny007; February 28th, 2012 at 10:53 AM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Switch problem. How do I return to my menu?

    I think you can add:

    for (int i = 0; i < options.length; i++) {
    System.out.println((i+1) + ":\t" + options[i] + " ");
    }
    option= input.nextInt();

    before every break.
    Last edited by luck999; February 28th, 2012 at 11:27 AM.

  3. The Following User Says Thank You to luck999 For This Useful Post:

    jonny007 (February 28th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Switch problem. How do I return to my menu?

    Thanks .... ! This solved it

    Genius....

  5. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Switch problem. How do I return to my menu?

    Quote Originally Posted by luck999 View Post
    I think you can add:

    for (int i = 0; i < options.length; i++) {
    System.out.println((i+1) + ":\t" + options[i] + " ");
    }
    option= input.nextInt();

    before every break.
    Code re-use is your friend! If you have to copy+paste anything, your probably doing it wrong.

    @Jonny Try requesting the users choice once at the beginning of the loop, declaring and or initializing any variables your going to use outside.

Similar Threads

  1. Problem with actionlistener/drop-down-menu (for a queue)
    By blueroselara in forum AWT / Java Swing
    Replies: 17
    Last Post: January 25th, 2012, 11:59 PM
  2. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  3. problem with help menu.
    By Toggo in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 12th, 2010, 05:13 PM
  4. Help - Return to menu screen
    By throzen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2009, 01:44 PM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM

Tags for this Thread