How to Fix Errors in Alphabetically Sorting Full Names in Java?

  • Thread starter sunbuster
  • Start date
  • Tags
    Sort
In summary: Name.length -1; i++)for(int j=0; j<fullName.length -1; j++){if(fullName[j]>(fullName[j+1])){fullName[j] = fullName[j+1];fullName[j+1] = temp;}}}In summary, the user types in a name into a text field and the code keeps making errors. They need to sort the characters in the string into alphabetical order.
  • #1
sunbuster
10
0
Code:
import javax.swing.*;
import java.util.Arrays;

public class sortSelection
{
	public static void main(String args[])
	{
		String inputString;
		char temp;
		
		inputString = JOptionPane.showInputDialog("Enter Your Name");
		
		char a = inputString.charAt(0);
		char b = inputString.charAt(1);
		char c = inputString.charAt(2);
		char d = inputString.charAt(3);
		char e = inputString.charAt(4);
		char f = inputString.charAt(5);
		char g = inputString.charAt(6);
		char h = inputString.charAt(7);
		char k = inputString.charAt(8);
		char l = inputString.charAt(9);
		char m = inputString.charAt(10);
		char n = inputString.charAt(11);
		char o = inputString.charAt(12);
		char p = inputString.charAt(13);
		char q = inputString.charAt(14);


		char[] fullName = {a, b, c, d, e, f, g, h, k, l, m, n, o, p, q};
		
		for(int i=0; i<fullName.length -1; i++)
			for(int j=0; j<fullName.length -1; j++)
			{
			if(fullName[j]>(fullName[j+1]))
				{
					temp = fullName[j];
					fullName[j] = fullName[j+1];
					fullName[j+1] = temp;
				}
			}		
		System.out.print(fullName[0]);
		System.out.print(fullName[1]);
		System.out.print(fullName[2]);
		System.out.print(fullName[3]);
		System.out.print(fullName[4]);
		System.out.print(fullName[5]);
		System.out.print(fullName[6]);
		System.out.print(fullName[7]);
		System.out.print(fullName[8]);
		System.out.print(fullName[9]);
		System.out.print(fullName[10]);
		System.out.print(fullName[11]);
		System.out.print(fullName[12]);
		System.out.print(fullName[13]);
		System.out.print(fullName[14]);
		
		}
}

=>>> Here is my code. It keeps make errors when I type the others name. What should I do?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
That is, by far, the most convoluted way to turn a string into an array of chars that I have ever seen.

Are you trying to sort the characters in a String containing a name into alphabetical order? I'm wondering because I can't think of a reason anyone would ever want to do that. :smile:

First thing is to get that char array in a less painful way. Something like this would be preferable:

char[] fullName = inputString.toCharArray();

That will also work with strings of any length. There's documentation for the String class and the rest here, and I suggest you use it:

http://download.oracle.com/javase/6/docs/api/index.html

You'll also want to print it out after in a bit of a better way. At least use a for loop.
 

1. How does sorting fullnames alphabetically work?

Sorting fullnames alphabetically involves arranging names in a list in alphabetical order, based on the first letter of the last name. This can be done manually or with the use of computer programs.

2. Why is sorting fullnames alphabetically important?

Sorting fullnames alphabetically is important for organizing and categorizing large amounts of data, such as in a database or directory. It allows for easy identification and retrieval of specific names.

3. What is the difference between sorting fullnames alphabetically and sorting by first name?

Sorting fullnames alphabetically sorts by the first letter of the last name, while sorting by first name sorts by the first letter of the first name. This can result in a different order of names, especially when there are multiple people with the same first name but different last names.

4. Can special characters or accents affect the alphabetical sorting of fullnames?

Yes, special characters and accents can impact the alphabetical sorting of fullnames. They are typically treated as separate characters and can change the order of names. For example, "Lévy" would come before "Levy" in alphabetical sorting.

5. Are there any exceptions to sorting fullnames alphabetically?

Yes, there can be exceptions to sorting fullnames alphabetically depending on the specific requirements or preferences of the sorting system. For example, some systems may choose to ignore certain prefixes such as "de" or "van" in last names when sorting alphabetically.

Similar threads

  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
848
  • Engineering and Comp Sci Homework Help
Replies
2
Views
975
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top