0

Please help me with my homework!:) Here's the instruction, Write a java program that accepts ten letters(small letters), then it will sort them alphabetically, using one dimension array.
Please help, I'm dead if I won't get this correct.
Tnx

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by peter_budo
0

Here is the short code that I've made last night

import javx.swing.*;
public class Sort{
  public static void main(String[] args){
     char[]letter = new char[10];

     for(i = 0; i < 10; i++){
     buffer[i] = 'a';
     JOptionPane.showInputDialog(null, "Enter a String: ");
     }
  }
}

well this program accepts ten letters but when you also input numbers it accepts it also but the real problem is I don't know how to sort it out.
Please help me!
:)
tnx

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

0

It doesn't accept anything, array values that you been supposed to get from user? You just hard coded buffer[i] = 'a'; . Also reconsider if you wish to use JOptionPane (as at current time is just showing pop-ups and not storing values)

Edited by peter_budo: n/a

0

One way would be to go through the array and compare two items in it and swap them if needed. Look up how to use .compareTo and swap

0

well how do we sort char variables anyway? without using any package

0

Well you do it by buffer[i].compareTo(buffer[i+1]) (in API) that would be normally used in bubble sort or any other. For fast painless solution I would go with Arrays.sort(buffer) (in API)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.