Write a program that reads names in standard form (i.e. Bob Loblaws) and prints them in the form
looks like I'm going to need to use charAt or indexOf to identify blank spaces, but I am still working on trying to figure out how to use this, to get the String to return just the last name and the first initial of any other names.
The ArrayList class might be helpful for storing any amount of inputs into an array
Ok thanks! I'll do some research into that and try it
yup :). ArrayList uses this import: ``` import java.util.ArrayList; ``` or ``` import java.util.*; ``` To create an ArrayList object, you declare it like so: ``` ArrayList<String> alVar = new ArrayList<>(); //after java 7, you can leave the diamond ^^ there blank - though ArrayList<String> alVar2 = new ArrayList<String>(); //is also proper syntax ``` ``` //import JOptionPane boolean done = false; while(!done) { String in = JOptionPane.showInputDialog("Enter names"); if(in.equals("ZZZ")) done = true; else { alVar.add(in); } } //now to show all inputted names for(int i = 0; i < alVar.size(); i++) System.out.println(alVar.get(i)); ``` That's just an intro to the class - you can find more of it's methods online-- such as it's size() method, get() method, add() method, remove() method, add(string, pos) method for inserting, set() method for replacing.
oops *add(pos, string)
Since each name is separated by spaces, you can use either the String class's split method to divide each part of a name into a String array, - or you could use the StringTokenizer class(tokenizer requires the java.util.StringTokenizer; import). From there you can separate the last name, and turn all of the other names into just their first char value, then display them.
Join our real-time social learning platform and learn together with your friends!