Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete Java program called StringSlicer that uses methods to: 1. get a

ID: 3814484 • Letter: W

Question

Write a complete Java program called StringSlicer that uses methods to: 1. get a String from the user at the command line. 2. populate an ArrayList of Character data (the wrapper class), with each char in the String represented as a separate Character element in the ArrayList, and 3. output each Character to the command line, each on a separate line. This is what was provided to get started: package stringslicer; import java.util.Scanner; import.java.util.ArrayList; public class StringSlicer { public static void main(String() args) { String stringFromUser = getUserString(); ArrayList characterArrayList = makeArrayList(stringFromUser); printArrayList(characterArrayList); } public static String getUserString() { public static ArrayList makeArrayList(String userString) { public static void printArrayList(ArrayList listOfCharacters) { **Notes: 1. In the method makeArrayList - make an array list. 2. Once the array list is created, populate it with the individual characters of the string that is passed into the method. So, go through all the positions of the string, get the character at each position, and add it to the array list.

Explanation / Answer

StringSlicer.java

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

public class StringSlicer {
   public static void main(String[] args) {
       String stringFromUser = getUserString();
       ArrayList characterArrayList = makeArrayList(stringFromUser);
       printArrayList(characterArrayList);
   }

   public static String getUserString() {
   Scanner scan = new Scanner(System.in);
   System.out.println("Enter the string: ");
   String s = scan.nextLine();
   return s;
   }

   public static ArrayList makeArrayList(String userString) {
       ArrayList list = new ArrayList();
       for(int i=0; i<userString.length(); i++){
           list.add(userString.charAt(i));
       }
       return list;
   }

   public static void printArrayList(ArrayList listOfCharacters) {
           for(int i=0; i<listOfCharacters.size(); i++){
               System.out.println(listOfCharacters.get(i));
           }
   }

}

Output:

Enter the string:
Hello java
H
e
l
l
o

j
a
v
a

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote