简体   繁体   中英

Bubble Sorting string in an array

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

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.

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....

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[]; 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM