I was writing a program that involves the console inputting integers and strings. For my project i prompt the user to enter the number of strings he would like. Then i prompt the user to enter the strings so i could order them alphabetically. I am supposed to use any sorting algorithm but since im new to programming i dont know any.
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];
String message = "";
for(int i = 0; i < stringCount; i++)
{
System.out.print("Could you enter the strings here: \n");
stringInput[i] = stdin.readLine();
message = message + stringInput[i] + ", ";
}
System.out.println("So you entered:\n" + message);
int j;
boolean flag = true; // set flag to true to begin first pass
String temp = null; //holding variable
while ( flag )
{
flag = false; //set flag to false awaiting a possible swap
for (j=0; j<stringInput.length; j++){
if(stringInput[j].compareTo(stringInput[j+1]) > 0)
{
temp = stringInput[j];
stringInput[j] = stringInput[j+1];
stringInput[j=1] = temp;
flag = true;
}
}
}
System.out.println("The sorted strings are:" + temp);
}
public static void BubbleSort(String[] stringInput)
{
int j;
boolean flag = true; // set flag to true to begin first pass
String temp = null; //holding variable
while ( flag )
{
flag = false; //set flag to false awaiting a possible swap
for (j=0; j<stringInput.length; j++){
if(stringInput[j].compareTo(stringInput[j+1]) > 0)
{
temp = stringInput[j];
stringInput[j] = stringInput[j+1];
stringInput[j=1] = temp;
flag = true;
}
}
System.out.println("The sorted strings are:" + temp);
}
}
}
This is my code. I was using bubble sort but it didnt go to well. Could someone help.
P.S. I cant use Java.util at all.