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.

singh_soorma94 commented: Thanks. your question helped me solve my problem setting up the console I/O +0

Recommended Answers

All 5 Replies

it didnt go to well.

Can you copy and paste the programs output here and add some comments about what is wrong.

package sorting;
import java.io.*; 

public class Sorting
{
    private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );// this is for retrieving an input

    public static void BubbleSort(String[] stringInput)/* this is where bubble sort comes into handy but doesnt work at all and nothing runs it goes to the next thing*/
    {
        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++){/* this is the part where i am totally lost */
                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 doesnt even run when i want it to.
    }
    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());// retrieving the numeber of string the user wants to input
        String[] stringInput = new String[stringCount];// Creating an array that contains stringCount strings
        String message = "";// using this so i can println
        for(int i = 0; i < stringCount; i++)// using for loop so that user could input as many times as he want
        {
	    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);// this will show what the user in
    }
}

Can you copy and paste the program's output here and add some comments about what is wrong.

Can you copy and paste the program's output here and add some comments about what is wrong.

package sorting;
import java.io.*; 

public class Sorting
{
    private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );// this is for retrieving an input

    public static void BubbleSort(String[] stringInput)// this is where bubble sort comes into handy
    {
        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++){/* this is the part where i am totally lost */
                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 doesnt even run when i want it to.
    }
    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());// retrieving the numeber of string the user wants to input
        String[] stringInput = new String[stringCount];// Creating an array that contains stringCount strings
        String message = "";// using this so i can println
        for(int i = 0; i < stringCount; i++)// using for loop so that user could input as many times as he want
        {
	    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);// this will show what the user in
    }
}

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.

Yor are almost correct except 2 logic mistakes..

In line no 17 :

if(stringInput[j].compareTo(stringInput[j+1]) > 0)

You are comparing with index j+1 . And you have used your for loop

for (j=0; j<stringInput.length; j++)

which is not matchable for comparision. It will throw ArrayIndexOutOfBoundsException at last.

So, reduce your array length in to -1 in for loop condition as like this.

for (j=0; j<stringInput.length-1; j++){

Before the end of main method call your BubbleSort method by passing required input.

In line no 26, you have printed temp variable which contains only 1 value.
Using for loop print your stringinput array variable which has been sorted..

I have modified your code here ..

import java.io.*; 

public class Sorting
{
    private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );// this is for retrieving an input

    public static void BubbleSort(String[] stringInput)// this is where bubble sort comes into handy
    {
        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-1; j++){/* this is the part where i am totally lost */
                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:" );// this doesnt even run when i want it to.
        for(int i=0;i<stringInput.length;i++){
            System.out.println(stringInput[i]);
        }
    }
    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());// retrieving the numeber of string the user wants to input
        String[] stringInput = new String[stringCount];// Creating an array that contains stringCount strings
        String message = "";// using this so i can println
        for(int i = 0; i < stringCount; i++)// using for loop so that user could input as many times as he want
        {
	    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);// this will show what the user in
        BubbleSort(stringInput);
    }
}
commented: It really helped. The program started giving me outputs. Thanks +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.