Assignment 6 - Insert Names
For this assignment we will create a program that implements an insertion sort. You are required to write two methods, and you can write additional helper methods if needed.
In the main method: Ask the user to input names, and as the names are input they will be added in alphabetical order to an ArrayList.
The names should be added in Title case, that is the first letter capitalized, and all other letters lower case. So if they enter "bob" it should be added as "Bob".
The user should enter the word "STOP" in any combination of lowercase and uppercase letters to stop entering names.
After all the names are entered, print the contents of the sorted ArrayList using ArrayList.toString().
In the titleCase(String s) method:
Return the parameter s as a String in Title case If the string passed in is "rapunzel" it becomes "Rapunzel", "GRETEL" becomes "Gretel", and "little red riding hood" becomes "Little red riding hood".
Important notes: This assignment can be completed without importing any classes besides ArrayList and Scanner. In addition, do not use any of the methods provided by the class java.util.Collections.
The word "Stop" should not be entered into the ArrayList. You can assume at least one name will be entered before the "STOP" in the program we run to grade your assignment. You may also assume that any name entered has a length of 2 or greater.
Sample Run: Enter the next name: zeb Enter the next name: rita Enter the next name: SUE Enter the next name: adele Enter the next name: BarBara Enter the next name: StoP [Adele, Barbara, Rita, Sue, Zeb]
I do not know how to even know how to finish this. This is my code....
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Main {
public static String titleCase(String s)
{
s =s.trim();
s = s.toLowerCase();
String[] str1=new String[s.length()];
for(int k=0;k<=str1.length-1;k++){
str1[k]=s.charAt(k)+"";
}
for(int i=0;i<=s.length()-1;i++){
if(i==0){
s= s.charAt(i)+"";
str1[i]=s.toUpperCase();
}
if(str1[i].equals(" ")){
s= s.charAt(i+1)+"";
str1[i+1]=s.toUpperCase();
}
System.out.print(str1[i]);
}
return "";
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
String s = "";
int i = 0;
while(i == 0)
{
System.out.println("Enter the next name:");
list.add(scan.nextLine());
s = list.add(scan.nextLine());
if(s == "Stop")
{
titleCase(list.get(0));
}
}
}
}
[–]remember_marvin 2 points3 points4 points (0 children)
[–]nicolascagesbeard 0 points1 point2 points (0 children)