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

  • Thread starter Thread starter sunbuster
  • Start date Start date
  • Tags Tags
    Sort
Click For Summary
The discussion revolves around a Java program that attempts to sort the characters of a user's name input in alphabetical order. The original code is criticized for its complexity, particularly in how it converts the input string into a character array. A more efficient approach is suggested, using the `toCharArray()` method, which simplifies the process and accommodates strings of varying lengths. Additionally, recommendations are made to improve the output method by employing a loop for printing characters instead of individual print statements. Overall, the focus is on enhancing code efficiency and readability.
sunbuster
Messages
10
Reaction score
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
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 3 ·
Replies
3
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K