1

I'm trying to bubble sort string data that was input into an array in descending and ascending order.

The following is the code so far:

import java.util.*;
public class nextLineArray
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String names[]=new String[12];
        System.out.println("Enter the 12 names: ");
        //Load Array
        for(int i = 0; i < 12; i++)
        {
            names[i] = input.nextLine();

        }
        //Print initial list
        System.out.println("List of names via input:"+ names);

        //Print descending order list
        String descSort;
        descSort=bubbleSortDesc(names);
        System.out.println("Names listed sorted in descending order (via BubbleSort): "+descSort);

    }
    public static String bubbleSortDesc(String[] names)
    {
        String temp;
        int passNum, i, result;
        for(passNum=1; passNum <= 11; passNum++)
        {
            for(i = 0; i<=(11-passNum); i++)
            {
                result=names[i].compareToIgnoreCase(names[i+1]);
                if(result>0)
                {
                    temp=names[i];
                    names[i]=names[i+1];
                    names[i+1]=temp;
                }
            }
        }
        return names;

    }
}

When I try to return the sorted array to the main method it gives me the following error on the return line:

Incompatible Types

Our online instructor just started us out with using multiple methods and arrays at the same time and it is quite confusing...please excuse me if any of my mistakes appear to be obvious.

Edit: I have fixed the initial problem thanks to Alexandre Santos in the comments, I am now running into a problem when executing the program after inputting the data, instead of printing the strings in the array it prints out

[Ljava.lang.String;@6d782f7c

4 Answers 4

1

Take a look at the method

public static String bubbleSortDesc(String[] names)

The return of that method is supposed to be a String (only one), but you are returning the parameter "names", which is an array of strings. The "[]" after the String identifies it as an array.

I am not going to do your homework for you, so a hint: check if the return type of the method bubbleSortDesc should be one String or an array of Strings.

Good luck.

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

2 Comments

Thank you, I resolved everything and managed to get the program to compile and execute. However the lines that are supposed to print the array after being declared and sorted instead print out. Could you clue me in as to why this is happening?
When you do a print on an object, it prints the reference to that object. In this case you want to print the values of that object, which means creating a for-loop to go through the object and, for every value, print the value.
0

There are 2 points to fix. First you should return String array

 public static String[] bubbleSortDesc(String[] names)

and therefore you should define it like this:

 String descSort[]; 

Comments

0

public static String bubbleSortDesc(String[] names)

should be

public static String[] bubbleSortDesc(String[] names)

and also declare descSort as String array.

Also you are just printing the array objects. This will not print the list for you. You have iterate over the array.

Include this in you code:

for (String name:names)
{
System.out.println(name);
}

Do the same for descSort too....

Comments

0

You can fix your print command by changing it to the following:

System.out.println("Names listed sorted in descending order (via BubbleSort): "+ java.util.Arrays.deepToString(descSort));

If you want the nitty gritty, descSort is a String[]. In Java when you convert String[] into a String it gives you that crazy string representation. You have to instead converte each entry in the array to a String individually. Fortunately the deepToString method will do that for you.

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.