Results 1 to 6 of 6
  1. #1
    RarkMowe is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Using a loop to prevent a program from closing? Help greatly appreciated :)

    Hi guys, I've been working on this assignment I got and have hit a roadblock. My program prompts the user to enter a number between 0-6, each with a corresponding option. I want the program to keep running until the user selects 0, the exit option. I'm just wondering how I would achieve this? Here's my code below so far. Any help would be greatly appreciated :)







    package ca1;

    import java.util.ArrayList;
    import java.util.Scanner;

    public class MainClass extends UserInput {

    public static void main(String[] args) {
    Scanner keyb = new Scanner(System.in);
    UserInput ui = new UserInput(); // Declaring object to store user input
    Scanner input = new Scanner(System.in);

    ArrayList<String> library = new ArrayList <>();
    Song s;

    //initial prompt Menu
    System.out.println("Welcome");
    System.out.println("........ \n");
    System.out.println("Press 0 to Exit\n");
    System.out.println("Press 1 to View All Songs\n");
    System.out.println("Press 2 to Add a Song\n");
    System.out.println("Press 3 to Remove a Song\n");
    System.out.println("Press 4 to Edit a Song\n");
    System.out.println("Press 5 to Count Songs\n");
    int opt=input.nextInt();





    //add to array list
    library.add("Track 1");
    library.add("Track 2");
    library.add("Track 3");
    library.add("Track 4");
    library.add("Track 5");



    while (opt > 0){


    //if statements

    if(opt==0)
    {
    System.out.println("Goodbye!");
    }


    else if (opt==1)
    {
    System.out.println("Your Library:");
    System.out.println(library);

    }


    else if(opt==3)
    {
    System.out.println("Please name your new track");
    System.out.println(library);
    }



    else if(opt==4)
    {
    System.out.println("Please select what you would like to remove from your library");
    System.out.println(library);
    }

    else if(opt==5)
    {
    System.out.println("What song would you like to edit?");
    System.out.println(library);
    }


    else if(opt==6)
    {
    System.out.println(library);

    }


    else
    {
    System.out.println("Incorrect Entry");
    }


    }


    }

    }

  2. #2
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: Using a loop to prevent a program from closing? Help greatly appreciated :)

    Use a do-while statement. While the variable or boolean is true, run the loop.

  3. #3
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Using a loop to prevent a program from closing? Help greatly appreciated :)

    use code tags and read the forum rules please.

  4. #4
    Arbalest is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Using a loop to prevent a program from closing? Help greatly appreciated :)

    too many if, else if try
    Java Code:
    switch(opt){
    case 1 : System.out.println("whatever"); break;// like this
    case 2 : System.out.println(); break; 
    case 3 : System.out.println(); break
    case 4 : System.out.println(); break;
    default : sdfsdfsdsdf break;

  5. #5
    RarkMowe is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Using a loop to prevent a program from closing? Help greatly appreciated :)

    Quote Originally Posted by pakupakuman View Post
    Use a do-while statement. While the variable or boolean is true, run the loop.
    Worked perfectly, thank you!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Using a loop to prevent a program from closing? Help greatly appreciated :)

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 05-11-2012, 04:22 PM
  2. Replies: 5
    Last Post: 12-08-2011, 07:54 PM
  3. Replies: 3
    Last Post: 11-05-2011, 03:52 PM
  4. Java Calculator Help Would Be Greatly Appreciated
    By TommyR in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 09:30 PM
  5. Prevent escape from closing a SWT dialog
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 07:58 PM

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
  •