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



Home Answers Viewqa Java-Beginners how to i convert this case to loop...please help.....

Have Programming Question? Ask it here!
 
 
 


badrul hisham
how to i convert this case to loop...please help.....
1 Answer(s)      2 years and 10 months ago
Posted in : Java Beginners


*/ 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 11:17 AM


Do modifications in Switchers() method:

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



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

              case 'b': AddElementSpecific();
                        break;

              case 'c': AddElementFront();
                        break;

              case 'd': EditExistingList();
                        break;

              case 'e': RemoveFirstElement();
                        break;

              case 'f': ViewElements();
                        break;

              case 'g': ReplaceElement();
                        break;

              case 'h': PrintList();
                        break;

              case 'i': PrintListSpecific();
                        break;

              case 'j': PrintReverseOrder();
                        break;

              case 'k': ClearAll();
                        break;

              case 'l': CheckEmptyList();
                        break;

              case 'q': Quit();
                        exit=true;
                        break;

              case 'Q': Quit();
                        exit=true;
                        break;

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

}
              }
              while(!exit);

}


Related Tutorials/Questions & Answers:
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....   */ import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....   */ import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
Advertisements
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....   */ import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....   */ import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import...(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder...(); case 'q': Quit(); case 'Q': Quit(); break
Please tell me how can i convert string to timer
Please tell me how can i convert string to timer  Please tell me how can i convert string to timer
How can I Convert my Image Files to Text Files? - IDE Questions
How can I Convert my Image Files to Text Files?  How can I Convert my Image Files to Text Files
Java : String Case Conversion
Java : String Case Conversion In this section we will discuss how to convert... : By using toLowerCase() method you can convert any upper case char/String...() method you can convert any lower case char/String into upper case char
Java String Case Converter
Java String Case Converter Here we are going to convert lowercase characters.... If there is a lowercase character then convert it into uppercase using Character.toUpperCase(ch[i... then convert it into lowercase using Character.toLowerCase(ch[i]) and append
how to convert jsp to exe
how to convert jsp to exe  hello sir, i am doing... to another system. so i want to convert this to exe. that will executed only internet... tomcat5.0,j2se5.0 and jsp,servlets i am using.this is doing in localhost only. now i want
hello there i need help
transaction? thats the problem. I dont know how to start this program because i am a beginner, and aside from that i am really eager to learn java please help me with the codes and please explain to me how it works. i only need to use
how to convert ACSII to HEX
how to convert ACSII to HEX  How to convert perticular ASCII... value of ~:00   The given code accepts the string and convert...() to convert it to Hex value. class ConvertAsciiToHex { public static void
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
How to convert NSString to NSInteger?
How to convert NSString to NSInteger?  Hi, Provide me code to convert NSString into NSInteger. Thanks   Hi, Use the following code example: NSString * s= @"100"; NSInteger i = [s intValue]; Thanks
how do i update my database with the help of update syntax in html <text/javascript>? How to write 'where' statement in this?
how do i update my database with the help of update syntax in html ? How... ActiveXObject("ADODB.Recordset"); alert('How are you!!'); cn.Open(strConn); alert('I am Fine!'); rs.Open(sqlmek, cn); alert('Fine
PHP Change Case
PHP String Change Case: In this current tutorial we will study how to change a string from normal text to all lower case text, to all upper case text and convert every first character to uppercase. To convert a string to all lowercase
How to convert a String to an int?
How to convert a String to an int?  I my program I am taking... format. Now I want to convert into int value. Tell me How to convert a String... contains the value 1025, then you can use the following code to convert the String
How to Convert String to Date?
How to Convert String to Date?  Hi, I am new in Java and learning the Date manipulation in Java. I have to write a program in Java which converts... have to convert it to Date object. Tell me How to Convert String to Date? Thanks
how to convert a jar file into .exe file
how to convert a jar file into .exe file  hi, I want convert my jar file into executable file,urgently please help me
i need a help in this please
i need a help in this please  The factorial of a nonnegative integer n is written n! (pronounced ‚?? n factorial‚??) and is defined as follows: n!=n...=input.nextInt(); long num=m; for(int i=m;i>1;i
I have need to help
I have need to help  Write a program that, for four points A, B, C and P, draws a triangle formed by ABC and a small cross showing the position of P; and displays a line of text indicating which of the following three cases
how to convert string to image in java
how to convert string to image in java  how to convert string to image in java? I know we need to convert image to a byte array at some point in my application but do not know how to convert a image into byte string. Please
i need to replace this if statement code with switch case code
i need to replace this if statement code with switch case code   i need to replace this code with switch case for(int i=0;i<100;i++){ if((i...){ switch(i/j){ case 3: System.out.println ("java
how to highlight the field in image,when i have entered into that corresponding field's textbox-any one help out
how to highlight the field in image,when i have entered into that corresponding field's textbox-any one help out  how to highlight the field in image,when i have entered into that corresponding field's textbox. Here webpage
Mysql Lower Case
Mysql Lower Case       Mysql Lower Case is used to convert the Upper Case letter to the Lower Case... 'Mysql Lower Case. To understand this example we create a table 'employee1
Mysql Lower Case
Mysql Lower Case       Mysql Lower Case is used to convert the Upper Case letter to the Lower Case. Understand With Example The section of this Tutorial illustrate
How to convert Mssql Procedure into Mysql Procedure.
How to convert Mssql Procedure into Mysql Procedure.  Hello Sir, I have a mssql procedure and i need it to be converted to mysql... AND @StartRow - 1 Plz sir help me to convert it into mysql
How to convert Mssql Procedure into Mysql Procedure.
How to convert Mssql Procedure into Mysql Procedure.  Hello Sir, I have a mssql procedure and i need it to be converted to mysql... AND @StartRow - 1 Plz sir help me to convert it into mysql
How to convert EBCDIC format value into ASCII format value in java
How to convert EBCDIC format value into ASCII format value in java  how to convert EBCDIC data format into ASCII format data using java Use Case:I... to convert EBCDIC data value into ASCII data values. Please help me to solve
How to convert a swing form to PDF
How to convert a swing form to PDF  Sir, I want to know about how convert a swing form containing textbox,JTable,JPanel,JLabel, Seperator etc swing menus to a PDF file using java code
How to convert string to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
i need help - Development process
in the background. please guide me how i can run my program in the background...i need help  hello, i need help regarding this program. public...()); } } } it is printing the result of ping in to a file,but if i want
i need help plz .... Quickly
i need help plz .... Quickly   how can i count how many numbers enterd by the user so the output would be like this Total number of Scores = .... this is my code :- import java.util.Scanner; public class SCORES { public
i need help - Development process
i need help  hello, i need help regarding this program. this program... IPAddress on command line, but i want to take it dynamically and it must store the result of ping in to a file . can anyone guide me how to do it please
HOW TO CONVERT THIS CODE INTO GUI
HOW TO CONVERT THIS CODE INTO GUI   System.out.println("\n\t UGANDA CHRISTIAN UNIVERSITY\n"); System.out.println("\n\tFACULTY OF SCIENCE AND TECHNOLOGY\n"); System.out.println("\n BACHELOR OF SCIENCE IN COMPUTER
How ro convert char into string??
How ro convert char into string??  Suppose i have one array like... char [] ch={'H','E','L','L','o'}; now i want to convert this arrar into string... is for java...but i want this in c# How ro convert char into string using recursive
i need help to solve this problem
i need help to solve this problem  Write a stack class ArrayStack.java implements PureStack interface that reads in strings from standard input.... H and I join the queue h. G leaves the queue i. H and I leave the queue
How to get filename in JTextArea in following case?
How to get filename in JTextArea in following case?  Hi, i'm trying to code a GUI in java, the following code is working but the filenam is not displayed in the JTextArea, why? [CODE] private void
how can create pop() method in this program ,Or how can delete image in it when i press pop button in runtime ??? please help me ...
how can create pop() method in this program ,Or how can delete image in it when i press pop button in runtime ??? please help me ...   ...[aray.length]; for (int i = 0; i < aray.length; i++) { intArray[i
How to convert date to time in PHP?
How to convert date to time in PHP?  Hi, programmer. The PHP... "date()" function was designed to format this timestamp using a custom format. I... to sharing with guys and girls who interested in PHP. Hopefully can help you! Best
 

 

 

DMCA.com