1

My assignment is to ask a user for the number of strings he would like to input. Then i will prompt him to right the strings. Then i am supposed to print the stings in alphabetical order. I got most of the assignment done, just need a sorting algorithm such as Bubble sort to finish it. this is my code.

import java.io.*; 
public class sorting
{


private static BufferedReader stdin=new BufferedReader(new InputStreamReader( System.in));

  public static void main(String[] arguments) throws IOException
  {
     System.out.println("How many strings would you like to enter?");
     int stringCount = Integer.parseInt(stdin.readLine());
     String[] stringInput = new String[stringCount];
     for(int i = 0; i < stringCount; i++)
     {
         System.out.print("Could you enter the strings here: ");
         stringInput[i] = stdin.readLine();
     }  
     //Now how do i use a sorting algorithm?
  }
}
2
  • en.wikipedia.org/wiki/Bubble_sort. You'll sort the array, doing an alphabetical string comparison. You don't need to sort the characters in the array, just the strings within the array. Commented Aug 2, 2011 at 0:23
  • [Apparently there was an unstated requirement that he NOT use anything in 'java.util'. See stackoverflow.com/questions/6917833/… ] Commented Aug 2, 2011 at 23:10

6 Answers 6

8

Arrays.sort().

Sign up to request clarification or add additional context in comments.

2 Comments

do i ad a ; to the arrays.sort() and do I do anything else because nothing happened when i run the project
Read the API reference I linked you to. (How would a statement Arrays.sort(); know which array to sort?)
3

Use Rhino as a Javascript parser so you can include jQuery into your project. Then sorting becomes trivial as you can just load the Strings into a <table> and run this nifty plugin on it.

^ DO NOT DO THIS. (well if you do, post the source and tell us what grade you got on the assignment ;)

No really, just write bubble sort by yourself. It's not that long. You probably learned the pseudocode in class. If you need an additional reference, take a look at the Wikipedia article on it. If there's something in particular that you don't understand about the algorithm, post a specific question and we'll help you out. Other than that, you look like you're on the right track so far :)

1 Comment

I almost downvoted this, until I saw "the do not do this please"... Then I laughed!!!
2

If this wasn't an exercise you'd just use Arrays.sort(stringInput)

Comments

1
import java.io.*;
import java.util.*;
public class sorting
{
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args) throws IOException
    {
         System.out.print("How many strings would you like to enter? ");
         String[] stringInput = new String[Integer.parseInt(input.nextLine())];
         for(int i = 0; i < stringInput.length; i++)
         {
             System.out.print("Could you enter the strings here: ");
             stringInput[i] = input.nextLine();
         }  
         Arrays.sort(stringInput);
         for(String s : stringInput) System.out.println(s);
    }
}

Comments

0

It seems to me that you are not being asked to sort the string array, but to print the strings in alphabetical order, which is likely to be a whole lot uglier but a whole lot simpler.

Once the user has typed all of the input strings, you might choose to iterate over the array stringCount times finding, in each iteration, the lowest-valued string; printing it; and then clearing it so that you won't see it in the next iteration.

BUT if you really are being asked to apply a bubble sort (or any kind of sort) on the array, well, that's a whole 'nother question, namely: how do I write a bubble sort for an array of strings. That's a tricky problem for anyone because the strings in the array are of differing lengths: they cannot just be swapped blindly but have to be written into some temp array somewhere that somehow knows how to accomodate their various lengths.

EDIT: Oh, wait a sec: maybe Java knows all about variable-length strings and how to handle them. If so, I take it all back.

6 Comments

Why is bubble sorting an array of Strings tricky..? You can bubble sort any ordered set that has a well-defined total ordering.
Could you help me with stringCount you mentioned. How would i use it in my program. Could you specify a little more because i just started a few days ago and don't know the sorting algorithms. My teacher told us to use any type of sorting algorithm
@tskuzzy -- to a C programmer like me, it's as I said in my answer. However, I think maybe Java makes this trickiness disappear.
@Vishal1949 -- stringCount is the variable in your program that dictates the number of strings the user types.
|
0
  1. Just use the Set str = TreeSet();
  2. then loop through the string str.add("z");
  3. when you iterate the str variable it is automatically sorted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.