Arraylist - Using switch statement to add string

I am new to programming and my professor has given an assignment that requires us to:

"declare on arraylist with the size of 5. Use switch statement to add string values to your arraylist. Retrieve the contents of your arraylist. Check the size of each element. If the element length is less than 8 rerun the program, otherwise count the consonants of each element."

I've done some research to understand some factors of an ArrayList; to start off, I did this:

import java.util.ArrayList;

public class izeOfArrayList {

  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();

    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");

    int totalElements = arrayList.size();

    System.out.println("ArrayList contains...");
    for(int index=0; index < totalElements; index++)
      System.out.println(arrayList.get(index));

  }
}

This code just gets the number of elements currently stored in my ArrayList, and prints out each element. I have three questions:

  • How can I add String values using switch statement?
  • How can I retrieve the contents of my ArrayList?
  • How can I check the size of each element in my ArrayList?

Answers


"declare on arraylist with the size of 5. Use switch statement to add string values to your arraylist. Retrieve the contents of your arraylist. Check the size of each element. If the element length is less than 8 rerun the program, otherwise count the consonants of each element."

Let's decode line by line:

declare on arraylist with the size of 5.

ArrayList<String> myList = new ArrayList<>(5);

Our ArrayList needs to be defined as a list of Strings, so we put those in the angle brackets. The constructor takes a starting size, which is specified as 5.

Use switch statement to add string values to your arraylist.

Completely unintelligible. switch statements are used in flow of control; we can decide to add string values based on some condition, but we cannot generate input with switch statements, and no conditions are specified. This following code is (seemingly) valid for this instruction:

String values = "values";
switch (values) {
    case "values":
    default:
        myList.add(values);
}

Retrieve the contents of your arraylist.

This you have already (mostly) written up:

int totalElements = myList.size();

for(int index = 0; index < totalElements; index++)
    String tempElem = myList.get(index); //get access to the individual elem

    //here we're going to do something with the current string (probably)

}

Check the size of each element.

I'm assuming that by the 'size of each element', your professor is looking for the length of each String.

int tempElemLength = tempElem.length();

String objects have a length method, it returns an int.

If the element length is less than 8 rerun the program, otherwise count the consonants of each element.

This, while at first glace seems reasonable, is again unintelligible. Here's a possible interpretation of this line:

if (tempElemLength < 8) {
    main(null);
} else {
    int tempElemNumConsonants = countConsonants(tempElem);

    //consonants are counted and now what?
}

Here is a complete response to your assignment as it is currently defined:

import java.util.ArrayList;

public class SizeOfArrayList {

    public static void main(String[] args) {
        ArrayList<String> myList = new ArrayList<>(5);

        String values = "values";
        switch (values) {
            case "values":
            default:
                myList.add(values);
        }

        int totalElements = myList.size();

        for (int index = 0; index < totalElements; index++)
            String tempElem = myList.get(index);

            int tempElemLength = tempElem.length();

            if (tempElemLength < 8) {
                main(null);
            } else {
                int tempElemNumConsonants = countConsonants(tempElem);

                //consonants are counted and now what?
                //guess print them out?
                System.out.println('Item ' + index + ': ' + tempElem + ' -> number of consonants: ' + tempElemNumConsonants);
            }
        }
    }
}

This is a solution to your problem as it has been provided; I will bet money that this is not the solution to your homework problem.


In another school of thought, if the focus of the assignment is basic use and understanding of ArrayLists and I was your professor, the assignment that I would have intended to give my students would be as follows:

Declare and ArrayList with the size of 5. Prompt the user for values until they enter 'quit'; use a switch statement to add all String values into the ArrayList that aren't just a number from [0-9]. Loop over each element in the ArrayList; if the length of any String element is less than 8, alert the user then restart the program. If all of the lengths are valid, sum up the consonants of each element. Print out each word and the consonant count, along with a final tally of the number of words along with the total number of consonants.

While I do know that this does not help you with the initial question, I hope it might be able to help you understand what your professor is trying to ask of you.


Need Your Help

MM/DD comparison issue in SQL

sql sql-server tsql sql-server-2012

I am using below SQL query to get data by mentioning date range without year.

Difference between login.event and logout.event in liferay

events action hook liferay

I hooked up these two events with one class and my question is How can I recognize when the class is called by login.event and when by logout.event.