Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Write a program that reads names in standard form (i.e. Bob Loblaws) and prints them in the form , (i.e. Loblaws, B). The program should prompt the user for names, halting when the user provides the name ZZZ. As examples, input of Santa Claus Michael J. Fox Madonna William Henry Richard Charles Windsor ZZZ Should produce the output of: Claus, S. Fox, M. J. Madonna Windsor, W. H. R. C.

OpenStudy (anonymous):

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.

OpenStudy (woodrow73):

The ArrayList class might be helpful for storing any amount of inputs into an array

OpenStudy (anonymous):

Ok thanks! I'll do some research into that and try it

OpenStudy (woodrow73):

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.

OpenStudy (woodrow73):

oops *add(pos, string)

OpenStudy (woodrow73):

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!