First, launch NetBeans and close any previous projects that may be open (at the
ID: 3703021 • Letter: F
Question
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "StringSlicer" (without the quotation marks) that uses methods to:
Get a String from the user at the command line
Populate an ArrayList of Character data (the wrapper class), with each char in the String represented as a separate Character element in the ArrayList
Output each Character to the command line, each on a separate line
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<Character> 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<Character> makeArrayList(String userString) {
ArrayList<Character> list = new ArrayList<Character>();
for(int i=0; i<userString.length(); i++){
list.add(userString.charAt(i));
}
return list;
}
public static void printArrayList(ArrayList<Character> listOfCharacters) {
for(int i=0; i<listOfCharacters.size(); i++){
System.out.println(listOfCharacters.get(i));
}
}
}
Output:
Enter the string:
Hai how are you
H
a
i
h
o
w
a
r
e
y
o
u
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.