Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSF-Questions HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING

Have Programming Question? Ask it here!
 
 
 


badrul hisham
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
1 Answer(s)      2 years and 11 months ago
Posted in : Java Server Faces Questions


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();
}

}


Advertisement
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
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); %> <
The while and do
While and do-while      ...-while statement now. Here is the syntax: do { statement(s) } while (expression); Lets tweak an example of do-while loop
while and do while
while and do while  hello, What is the difference between a while statement and a do statement?   hello, A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do
While and do-while
While and do-while      ... of nested classes i)  Static classes ii) Inner classes ( Non-static... { ... } class InnerClass { ... } } i) Static Nested Classes
php do while syntax
php do while syntax  How to create a do while loop in php. What is the syntax
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 loop
php do while loop  Difference between do while loop and simple loop in PHP
php do while example
php do while example  Simple example of do while 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
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
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
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
do-while Loop in Java
; while(condition); In this example you will see how to use the do-while loop... do-while Loop in Java      ... and processes once at least. For that you have to use the do while loop in  after
Do While Loop in Java
); In this example you will see how to use the do-while loop statement. The values...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
Class.forName will do while loading drivers
Class.forName will do while loading drivers  What Class.forName will do while loading drivers
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
of the variable within the loop. PHP do while loop Example: <?php $i=34; do... of $i is not valid but do-while loop runs at least one time despite...Do-While Loop Function PHP: PHP Do-While loop method is another type of loop
While and do-while
While and do-while      ... at do-while statement now. Here is the syntax: do { statement(s) } while (expression); Lets tweak an example of do-while loop
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... the condition is true. Syntax do { code to be executed; } while (condition); e.g.... reaches to either 5 or less than 5. <?php $i=1; do { $i++; echo &ldquo
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
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 compare strings in Java?
How do I compare strings in Java?  How do I compare strings in Java
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 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
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 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 generate random number?
How do I generate random number?  In my Java program there is a requirement of generating random numbers between two given numbers. How do I write code for generating the random number? Thanks   Hi
how do i begin a two dimensional array?
how do i begin a two dimensional array?  I'm new to java programming and need to create a two dimensional array that enters exactly what is entered in the first dimension and then the first non-white space character of what
how do i grab the url in php?
how do i grab the url in php?  I want to grab the 'entire' url, including any special characters like & and #, from a site. I then want... # = %23 I tried URL encoding but it only grabs the part until the #, which
how do i write a java program for this??
how do i write a java program for this??  ‚??Ask the user if they have a dog. If ‚??yes‚??, ask the user how old is and compute the dog‚??s age in human years. Display the output. If ‚??no‚?? ask the user their age and compute
how I do select from select in hql
how I do select from select in hql  select RN from ( select rownum RN, dbid from ( select * from profile p where type = 1912 and name like 'a%' order by globalid ) ) where db_id = 259; this is the sql The below hql
PureStack question---i dont know how to do this
PureStack question---i dont know how to do this  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from... the queue g. H and I join the queue h. G leaves the queue i. H and I leave
PureStack question---i dont know how to do this
PureStack question---i dont know how to do this  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from... the queue g. H and I join the queue h. G leaves the queue i. H and I leave
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 to do dynamic ally placeholder using properties or some else - JSP-Servlet
how to do dynamic ally placeholder using properties or some else   dear sir, how to use and declare a dynamic place holder in java? i have to send... And Regards HR while sending this mail i have to read a excel file in that specified
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
Advertisements
 

 

 

DMCA.com