Tuesday, July 14, 2009

I need to get this code working with spaces...?

//Collects three different names and sorts them alphabetically





import java.util.Scanner;





public class NameSorter


{


public static void main(String[] args)


{


Scanner in = new Scanner(System.in);





//Prompt user


System.out.println("This program will take three names inserted, and sort them alphabetically in ascending order");





System.out.println(" ");


System.out.println("Please enter the names of three people (one per line) to be sorted alphabetically:");





//Collect names from user


System.out.println("First person: ");


String a = in.next();


System.out.println("Second person: ");


String b = in.next();


System.out.println("Third person: ");


String c = in.next();





System.out.println(" ");


System.out.println("Alphabetical order:");





//Begin sorting...





The sorting works (I can't fit it all here). Everything works. I just need to get it to work with spaces in the names entered by the user. Ideas? Thanks!

I need to get this code working with spaces...?
It is hard to offer ideas when we don't know how your code is actually deciding how to sort. Perhaps you could replace your current description with the actual code?





-Eric





-EDIT-


When you say you want it 'to work with spaces in the names' - do you mean that it gives you an error if you use a space in a name, or that the results are incorrect?





Would it be an acceptable solution to strip spaces out of the name when comparing them (but not when printing them)?





Should Eric A == Erica in sorting order? should A L Fonso == Alfonso?
Reply:umm if you mean you want spaces between the names in the output, just do:





System.out.println(a + " " + b + " " c);





or if you wanted to do it vertically, just do:





System.out.println(a);


System.out.println();


System.out.println(b);


System.out.println();


System.out.println(c);

artificial flowers

No comments:

Post a Comment