Question

In: Computer Science

Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and...

Lab Text Manipulation

Inside the main method, do the following:

  • Create an ArrayList of strings and call it parks.
  • Read in the names of national parks from the user until the user enters done(or DONE,
    or dOnE, .. ) Keep in mind, that the names of some national parks consist of more than one word, for example, Mesa Verde.
    As you read in the national parks, add them to the list.
  • Next, we are going to build a string based on the elements in the list parks. Since the text keeps changing as we add one park at a time, we use class StringBuilder for this task.
    • Use a StringBuilder called sb to create the string  nationalParks .
    • Loop through all the elements of the list  parks and add them one at a time.
      The resulting string should have the following format:
      Favorite National Parks: {park1} | {park2} | . . . | {parkN}
      The parks are separated by a space, a vertical bar, and another space. However, there is no vertical bar after the last element. {park1}, {park2}, {parkN} are the various list elements with updated spelling.
    • Create a private method to update the spelling.
      We can't control whether the user enters the park names in uppercase or lowercase letters. However, we can change the names to a spelling where all letters are lowercase except for the first letters of each individual word. In order to make those changes, create a private method called updateSpelling. It has the following method header:
      private static String updateSpelling(String text)
      E.g.: When you pass the string "MESA VERDE" the method returns "Mesa Verde"
      E.g.: When you pass "yEllOwstOnE" it returns "Yellowstone"
      E.g.: Passing "black canyon of the gunnison" returns "Black Canyon Of The Gunnison"
      E.g.: Passing "Denali" returns "Denali"
    • When you are done building the specified string in SringBuilder, print it.

The output depends on the information provided by the user.

Please enter your favorite National Park or DONE to stop: mesa verde
Please enter your favorite National Park or DONE to stop: black CANYON of ThE gunnisON
Please enter your favorite National Park or DONE to stop: DENALI
Please enter your favorite National Park or DONE to stop: yellowStone
Please enter your favorite National Park or DONE to stop: Done

Favorite National Parks: Mesa Verde | Black Canyon Of The Gunnison | Denali | Yellowstone

Solutions

Expert Solution

Screenshot

Program

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

public class TextManipulator {

   public static void main(String[] args) {
       //Object to read user input
       Scanner sc=new Scanner(System.in);
       //Create an ArrayList of strings and call it parks
       ArrayList<String>parks=new ArrayList<String>();
       //Read in the names of national parks from the user until the user enters done
       String name="";
       System.out.print("Please enter your favorite National Park or DONE to stop: ");
       name=sc.nextLine();
       while(!name.equalsIgnoreCase("done")) {
           parks.add(name);
           System.out.print("Please enter your favorite National Park or DONE to stop: ");
           name=sc.nextLine();
       }
       System.out.println();
       //Create Text
       StringBuilder nationalParks=new StringBuilder();
       for (int i=0;i<parks.size()-1;i++)
       {
            nationalParks.append(parks.get(i));
            nationalParks.append(" | ");
       }
       nationalParks.append(parks.get(parks.size()-1));
       //Display Formatted text
       System.out.println("\nFavorite National Parks: "+updateSpelling(nationalParks.toString()));
      
   }
   //Method to change first letter of a word into capital and all others are small
   private static String updateSpelling(String text) {
       //For temporary storage
       StringBuilder s = new StringBuilder();
       //For check the starting point of a word
        char ch = ' ';
        //Loop through each character of the text
        for (int i = 0; i < text.length(); i++) {
            //If a character's previous charcter space and next is a character then change upeercase
           //Because it is starting letter of a word
            if (ch == ' ' && text.charAt(i) != ' ') {
               s.append(Character.toUpperCase(text.charAt(i)));
            }
            //Check other letters and change into lower case
            else if(Character.isLetter(text.charAt(i))) {
               s.append(Character.toLowerCase(text.charAt(i)));
            }
            //Other than letters
            else {
               s.append(text.charAt(i));  
            }
            //Keep track of prev character  
            ch =text.charAt(i);
        }
        //Return string
        return s.toString();
   }

}

------------------------------------------------------------

Output

Please enter your favorite National Park or DONE to stop: mesa verde
Please enter your favorite National Park or DONE to stop: black CANYON of ThE gunnisON
Please enter your favorite National Park or DONE to stop: DENALI
Please enter your favorite National Park or DONE to stop: yellowStone
Please enter your favorite National Park or DONE to stop: Done


Favorite National Parks: Mesa Verde | Black Canyon Of The Gunnison | Denali | Yellowstone


Related Solutions

Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings,...
For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings, and improve your skills with for loops. The methods you will write will be part of a short trivia game, feel free to try it out after you finish.   import java.util.Scanner; public class Trivia { //TODO: isLeapYear //TODO: isPrime //TODO: aWord //TODO: reverse public static void main(String[] args){ Scanner answers = new Scanner(System.in); int score = 0; System.out.println("What year is a Leap Year?"); //...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT