HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING

HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING

import java.util.LinkedList; import java.util.*; import java.io.*; import java.lang.*;

public class LinkedListv4 {

char question;
Scanner console = new Scanner(System.in);
LinkedList listA = new LinkedList();

public void Switchers(){

            System.out.println("| a. Append a specified elements to the list");
            System.out.println("| b. Add an element at specified index");
            System.out.println("| c. Add a new elements in front of the list");
            System.out.println("| d. Add new elements with a specified value into the list alternately");
            System.out.println("| e. Remove the first element in the list");
            System.out.println("| f. Find an element at a specified position in the list");
            System.out.println("| g. Replace the elements at a specified index");
            System.out.println("| h. Print the list");
            System.out.println("| i. Print the list from a specified index");
            System.out.println("| j. Print the list in reverse order");
            System.out.println("| k. Remove all elements in the list");
            System.out.println("| l. Check for empty list");
            System.out.println("| q. Quit");



            question = console.next().charAt(0);
            System.out.println();



            switch (question) {
              case 'a': AppendElement();

              case 'b': AddElementSpecific();

              case 'c': AddElementFront();

              case 'd': EditExistingList();

              case 'e': RemoveFirstElement();

              case 'f': ViewElements();

              case 'g': ReplaceElement();

              case 'h': PrintList();

              case 'i': PrintListSpecific();

              case 'j': PrintReverseOrder();

              case 'k': ClearAll();

              case 'l': CheckEmptyList();

              case 'q': Quit(); 

              case 'Q': Quit(); break;

              default: System.out.println("Invalid Input"); 
                        System.out.println();
                        Switchers(); break;
}

}

            //1 --- DONE
            public void AppendElement() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Create/Store objects in an LinkedList container.                    |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    System.out.println("Please set maximum elements required");
                    System.out.println();
                    int Max = console.nextInt();

                    for (int i = 0; i < Max; i++) {

                        System.out.println("Please insert number for element "+ i);
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(new Double(element));
                    }

                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());
                            }

                            Switchers();

            }

            //2 --- DONE FIXED - ???
            public void AddElementSpecific() {



                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Create/Store objects in specified  LinkedList container.            |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    System.out.println("Please insert specified index :");
                    int index = console.nextInt();

                        System.out.println("Please insert number for element  :");
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(index,new Double(element));

                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());


                            }

                            Switchers();

                }

            //3 --- DONE
            public void AddElementFront() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Create/Store objects in front of LinkedList container.              |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                        int i= 0;
                        System.out.println("System will store element to the first index, Insert new element please  :");
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(0,new Double(element));


                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());


                            }

                            Switchers();
                }

            //4 --- DONE - ???
            public void EditExistingList() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Add new elements with a specified value into the list alternately   |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    Iterator IteInitial = listA.iterator();
                            while (IteInitial.hasNext()) {
                                System.out.println(IteInitial.next());
                            }

                    System.out.println("Please choose desirable index to modify elements");
                    System.out.println();
                    int SpecIndex = console.nextInt();
                    listA.remove(SpecIndex);

                        System.out.println("Please insert new number for element "+ SpecIndex);
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(SpecIndex,new Double(element));

                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());
                            }

                            Switchers();                        

                }

            //5 --- DONE
            public void RemoveFirstElement() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("|      Remove fisrt element in the LinkedList container.              |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    Object object = listA.removeFirst();
                    System.out.println(object + " has been removed from the first index of LinkedList");

                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    Iterator Ite = listA.iterator();
                    while (Ite.hasNext()) {
                    System.out.println(Ite.next());


                            }

                            Switchers();

                }

            //6 --- DONE
            public void ViewElements() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Find an element at a specified position in the list                 |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    Iterator Ite = listA.iterator();
                    while (Ite.hasNext()) {
                    System.out.println(Ite.next());
                    }

                    System.out.println("Select any element in the list to review index position :");
                    double element = console.nextDouble();
                    System.out.println();

                    int NotNegOne = listA.indexOf(element);

                    if (NotNegOne != -1) {
                      System.out.println("element "+element+" is at index "+listA.indexOf(element));
                    } 

                    else {
                      System.out.println("LinkedList does not contain "+element);
                    }

                    Switchers();

                }


            //7 --- DONE
            public void ReplaceElement() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Replace the elements at a specified index                           |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    Iterator IteInitial = listA.iterator();
                            while (IteInitial.hasNext()) {
                                System.out.println(IteInitial.next());
                            }

                    System.out.println();
                    System.out.println("Please choose desirable index to modify elements");
                    System.out.println();
                    int SpecIndex = console.nextInt();
                    listA.remove(SpecIndex);

                        System.out.println("Please insert new number for index "+ SpecIndex);
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(SpecIndex,new Double(element));

                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());
                            }

                    Switchers();

                }

            //8 --- DONE
            public void PrintList() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("|      Print List in the LinkedList container.                    |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    if (listA.isEmpty()){
                    System.out.println("Linked list is empty");
                    }
                    else{
                     System.out.println (listA);
                    }

                    Switchers();

                }

            //9 --- DONE
            public void PrintListSpecific() {

                                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("|      Print List from specified index in the LinkedList container.   |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    try {

                    int indexSize = listA.size();
                    int realIndexSize = indexSize - 1;
                    System.out.println("Total Index in LinkedList (counting from O) "+realIndexSize);
                    System.out.println();
                    System.out.println();
                    System.out.println("Please choose desirable index to view its element");
                    System.out.println();
                    int SpecIndex = console.nextInt();
                    System.out.println();
                    System.out.println("The element is "+listA.get(SpecIndex)+" at index "+SpecIndex);

                    }

                    catch ( Exception e )  {
                        System.out.println();
                        System.out.println("LinkedList does not contain index requested");  
                    }


                    Switchers();

                }

            //10 --- DONE
            public void PrintReverseOrder() {

                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Reverse the list in the LinkedList container.                       |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    ListIterator itr = listA.listIterator();

                    System.out.println("Iterating through elements of Java LinkedList using ListIterator in forward direction...");
                    while(itr.hasNext())
                    {
                    System.out.println(itr.next());
                    }

                    System.out.println("Iterating through elements of Java LinkedList using ListIterator in reverse direction...");
                    while(itr.hasPrevious())
                    {
                    System.out.println(itr.previous());
                    }


            Switchers();

                }

            //11 --- DONE
            public void ClearAll() {

                System.out.println("+---------------------------------------------------------------------+");
                System.out.println("|      Clear entire LinkedList                                        |");
                System.out.println("+---------------------------------------------------------------------+");
                System.out.println();

                listA.clear();
                System.out.println("All element has been removed");
                System.out.println();
                System.out.println();

                if (listA.isEmpty()){
                    System.out.println("Linked list is empty");
                    }
                    else{
                     System.out.println (listA);
                    }

                Switchers();

                }

            //12 --- DONE
            public void CheckEmptyList() {

                if (listA.isEmpty()){
                    System.out.println();
                    System.out.println("Linked list is empty");
                    System.out.println();
                    }
                    else{
                     System.out.println (listA);
                    }

                Switchers();

                }

            //13 ---DONE
            public void Quit() {

                System.exit(0);
                }


public static void main (String args[]) {

    LinkedListv4 MYlinkList = new LinkedListv4();
    MYlinkList.Switchers();
}

}

View Answers

September 19, 2011 at 1:05 PM

Do modifications:

public void Switchers(){
              boolean exit=false;
              do{
            System.out.println("| a. Append a specified elements to the list");
            System.out.println("| b. Add an element at specified index");
            System.out.println("| c. Add a new elements in front of the list");
            System.out.println("| d. Add new elements with a specified value into the list alternately");
            System.out.println("| e. Remove the first element in the list");
            System.out.println("| f. Find an element at a specified position in the list");
            System.out.println("| g. Replace the elements at a specified index");
            System.out.println("| h. Print the list");
            System.out.println("| i. Print the list from a specified index");
            System.out.println("| j. Print the list in reverse order");
            System.out.println("| k. Remove all elements in the list");
            System.out.println("| l. Check for empty list");
            System.out.println("| q. Quit");


            System.out.print("Enter your choice: ");
            question = console.next().charAt(0);
            System.out.println();

            if(question=='a'){
                 AppendElement();
            }

            else if(question=='b'){
                 AddElementSpecific();
            }

            else if(question=='c'){
                 AddElementFront();
             }
            else if(question=='d'){
                 EditExistingList();
             }
            else if(question=='e'){
                 RemoveFirstElement();
             }
            else if(question=='f'){
                 ViewElements();
            }
            else if(question=='g'){
                 ReplaceElement();
            }
            else if(question=='h'){
                 PrintList();
            }
            else if(question=='i'){
                 PrintListSpecific();
            }  
            else if(question=='j'){
                 PrintReverseOrder();
            }
            else if(question=='k'){
                 ClearAll();
            }
            else if(question=='l'){
                 CheckEmptyList();
            }
            else if(question=='q'){
                 Quit();
                 exit=true;
            }
            else if(question=='Q'){
                 Quit();
                 exit=true;
            }

          }
               while(!exit);

}









Related Tutorials/Questions & Answers:
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO.... Print the list"); System.out.println("| i. Print the list from..."); System.out.println("| q. Quit"); question = console.next().charAt(0
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO.... Print the list"); System.out.println("| i. Print the list from..."); System.out.println("| q. Quit"); question = console.next().charAt(0
Advertisements
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO.... Print the list"); System.out.println("| i. Print the list from..."); System.out.println("| q. Quit"); question = console.next().charAt(0
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO.... Print the list"); System.out.println("| i. Print the list from..."); System.out.println("| q. Quit"); question = console.next().charAt(0
Use if, if else,for,while,do wile ad switch in same Java Class
Use if, if else,for,while,do wile ad switch in same Java Class  hi how to use if, if else,for,while,do wile ad switch in same class? very urgent... "); System.out.println("5. Exit"); boolean quit = false; do{ System.out.print("Please
The while and do
While and do-while      ... terminates. Have a look at do-while statement now. ADS_TO_REPLACE_3 Here is the syntax: do { statement(s) } while (expression); Lets
How do I change the while loop in this code to the range with range list style display page for a resultSet() in jsp?
How do I change the while loop in this code to the range with range list style display page for a resultSet() in jsp?  this is count record code...); while (rs.next()) { rows =rs.getInt(1); %> <
while and do while
while and do while  hello, What is the difference between a while statement and a do statement?   hello,ADS_TO_REPLACE_1 A while... should occur. A do statement checks at the end of a loop to see whether the next
php do while syntax
php do while syntax  How to create a do while loop in php. What is the syntax
While and do-while
While and do-while      ...; } } There are two categories of nested classes i)  Static classes ii) Inner... StaticNestedClass { ... } class InnerClass { ... } } i
do-while loop
do-while loop  how many times will the following loop get executed and what will be the final value of the variable I after execution the loop is over. int I = 5; do{ I + = 3; System.out.println(""+I); I=I+1; }while(I>=9
php do while example
php do while example  Simple example of do while loop in PHP
php do while loop
php do while loop  Difference between do while loop and simple loop in PHP
php do while break
php do while break   receiving a Fatal saying cannot break/continue.. please suggest. Thank U
php do while false
php do while false   Is there any difference between FALSE and false
while & do..while Loop
while & do..while Loop while loop The while loop iterate until provided... : Value of j 1Value of j 2Value of j 3 The do...while statement The do-while... : do   {   code to be executed;   } while (condition
How do i do the coding for 'leaving a comment' in java
How do i do the coding for 'leaving a comment' in java  i am designing a webpage.In my webpage i want to add the option of adding a comment by the readers of the page.how do i do
programes on do... while
programes on do... while   write a program to print reverse...; for (int i = 0; i <= num; i++) { int r = num % 10...; i = 0; } return reversedNumber
Do-while loop in Java
Do-while loop in Java In this section you will learn about do-while loop in Java. do-while loop is similar to while loop but the difference is do-while loop... condition repeatedly and run at least once. For that we need a do-while loop so
How do I switch to data science career?
How do I switch to data science career?  Hi, I am beginner in Data...: How do I switch to data science career? Try to provide me good examples or tutorials links so that I can learn the topic "How do I switch to data
Class.forName will do while loading drivers
Class.forName will do while loading drivers  What Class.forName will do while loading drivers
Do While Loop in Java
(condition); In this example you will see how to use the do-while loop...Do while loop in Java There are three loops most commonly used in Java e.g..... Do while loop is java is slightly different with While loop as in While loop
do-while Loop in Java
do-while Loop in Java      ... and processes once at least. For that you have to use the do while loop in  after that check the condition then we use the do-while loop statement. do-while loop
PHP Do While Loop
Do-While Control Structure: Do-While loop is another type of loop which runs... within. We generally use Do-While loop  when the iteration must run at least once despite of the validity of the variable. In Do-While loop we need
PHP Do While Loop
Do-While Loop Function PHP: PHP Do-While loop method is another type of loop... the code within. We generally use Do-While loop  when the iteration must run at least once despite of the validity of the variable. In Do-While loop we need
While and do-while
While and do-while      ... and the loop terminates. Have a look at do-while statement now. ADS_TO_REPLACE_3 Here is the syntax: do { statement(s) } while (expression); Lets
do while loop
do while loop       In java a do while loop is a control flow statement that allows a certain code... with a condition associated with the while keyword. The code inside the do construct
Do..While Loops
3.10.2. Do…While Loops The do...while statement always execute..._TO_REPLACE_2 while (condition); e.g. Here we define i = 1, and then increment i with 1...; . $i . “<br />”;ADS_TO_REPLACE_5 } while ($i<=5); ?>
How do I become a data scientist without coding experience in India?
How do I become a data scientist without coding experience in India?  ... for the tutorials to learn: How do I become a data scientist without coding... can learn the topic "How do I become a data scientist without coding
I want to change my user name on your website ,how can i do this
I want to change my user name on your website ,how can i do this  I want to change my user name on your website ,how can i do this   You can create another account very easily with different username
How do I upgrade mysql?
How do I upgrade mysql?  How do I upgrade mysql
how do i solve this problem?
how do i solve this problem?  Define a class named Circle... the Circle object is data fields. A public method named changeColour(String) to change... objects. The first object must be created using the default constructor, while
how do i solve this question?
how do i solve this question?  1.Define a class named Circle... changeColour(String) to change the value of the Circle objectââ?¬â?¢s colour... be created using the default constructor, while the second object must be created
How can I do it? .click();
How can I do it? .click();  I have a very unusual problem. I want...("a"); x.click(); </script> So it's click on an element witch one Id's is "a", but I want that it make mouseup in this element. How can I do it, because if I write
How do I change a large string into hex and then into byte so that SHA1 can be applied to it?
How do I change a large string into hex and then into byte so that SHA1 can be applied to it?  I work with cellphones and deal with MEID numbers..., not the string representing the MEID. If this is true then how do I change my string
Java while coding - Java Beginners
Java while coding  Java loop and function coding question? How can I... * 19; I'd to use a method (function) to do the computation I'd like... boolexit=true; do { BufferedReader br = new BufferedReader(new
How do I decompile Java class files?
How do I decompile Java class files?  How do I decompile Java class files
How do I initialize a byte array in Java?
How do I initialize a byte array in Java?  How do I initialize a byte array in Java
How do I compare strings in Java?
How do I compare strings in Java?  How do I compare strings in Java
How do I do this program? I'm new to Java programming...
How do I do this program? I'm new to Java programming...  Suppose you want to print out numbers in brackets, formatted as follows: [1] [2] [3] and so on. Write a method that takes two parameters: howMany and lineLength
How do i retain values in the drop down - Struts
How do i retain values in the drop down  Hi, I have a jsp page... with the drop down. My problem is whenever i do this the values in the drop down gets reset while the others in the text boxes don't. How do i retain the values
How do I compile the registration form?
How do I compile the registration form?  How do I compile the registration form as stated at the bottom of the following page (URL is below). Do I need ANT? If so, please give instructions. I am a student. http
How do I get started with Bootstrap
How do I get started with Bootstrap  Hi, I am new to Bootstrap and want to learn it in. What all things I should know before starting Bootstrap? How do I get started with Bootstrap? Thanks
How do I get started with Bootstrap
How do I get started with Bootstrap  Hi, I am new to Bootstrap and want to learn it in. What all things I should know before starting Bootstrap? How do I get started with Bootstrap? Thanks
how i do url encoding process - JSP-Servlet
how i do url encoding process  hai to all how i do the url encoding., when i passing data from one jsp page to another page,while passing some... post method., but here i want some of the url encodings plz help me., thanx
how do you change the message box to a specific color
how do you change the message box to a specific color  How do you change the message box color to a specific color? I can get the color to change with each input but i cant give it a specific color. Example I'm trying to change
How do I choose a data science course?
How do I choose a data science course?  I am thinking of getting trained in data science. How do I choose a data science course? There are many... How do I choose a data science course? Thanks
How do I learn Spring Framework?
How do I learn Spring Framework?  Hi, I have completed my Engineering in 2017. During my college days I learned Core Java, Advanced Java, JDBC, JSP.... How do I learn Spring Framework? Thanks
How do I learn Spring Framework?
How do I learn Spring Framework?  Hi, I have completed my Engineering in 2017. During my college days I learned Core Java, Advanced Java, JDBC, JSP.... How do I learn Spring Framework? Thanks
How do I learn Spring Framework?
How do I learn Spring Framework?  Hi, I have completed my Engineering in 2017. During my college days I learned Core Java, Advanced Java, JDBC, JSP.... How do I learn Spring Framework? Thanks

Ads