Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Which sort method to use for a String Array to output the index?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Which sort method to use for a String Array to output the index?

    Hey all,
    I've written a code for finding the index of an int array using a binary search, but I cant find out to do the same with a String Array. Basically the user enters an artist (ignoring case), which use a search method (binary, linear but not bubble sort) to output the index of the artist in the charts (obviously using a +1). Could you please advise me on the appropriate search method for String Arrays.

    I've only got the start so far.....

    Cheers

    Brian

     
    import java.util.*;
     
    public class ChartPosition {
     
        public static void main(String args[]) {
            Scanner myKeyboard = new Scanner(System.in);
     
            String[] Artist = {"Alexis Jordan", "Bruno Mars", "Cee Lo Green", "Cheryl Cole",
                "Duck Sauce", "Katy Perry", "Mike Posner", "Nelly", "Rihanna", "The Saturdays"};
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    using a binary search, but I cant find out to do the same with a String Array.
    Please explain what problem you are having with a binary search on String array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    I don't know the correct line to use for the String Array.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    Can you explain what a "line" is?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    this is the code I've written finding an integer index using binary, but I just get it to work with a String array.

    I've change the keyboard to String userType = myKeyboard.nextLine();
    I've tried changing the int into Strings, but I just keep getting errors messages

    import java.util.*;
     
    public class BinarySearch {
     
        public static void main(String[] args) {
     
            Scanner kybd = new Scanner(System.in);
     
            int[] nums = {2, 4, 7, 8, 12, 14, 15};
            //binary search - sorted
            output(nums);
     
            System.out.print("\nEnter item to be searched for: ");
            int searchValue = kybd.nextInt();
     
            int location = binarySearch(nums, searchValue);
            if (location == -1) {
                System.out.println(searchValue + " not found");
            } else {
                System.out.println(searchValue + " found at location " + location);
            }
        }
     
        public static void output(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + "  ");
            }
            System.out.println();
        }
     
        public static int binarySearch(int[] arr, int item) {
            int low = 0;
            int high = arr.length - 1;
            int location = (low + high) / 2;
     
            while (item != arr[location] && high >= low) {
                if (item < arr[location]) {
                    high = location - 1;
                } else if (item > arr[location]) {
                    low = location + 1;
                }
                location = (low + high) / 2;
            }
     
            if (item != arr[location]) {
                location = -1;
            }
            return location;
        }
    }


    --- Update ---

    If I could get the user to enter the artists name, it would output the index +1 to give their chart position.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    I just keep getting errors messages
    Please post the full test of the error messages if you want help with any of them.
    Also post the code that is causing the errors.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    OK, I've deleted it for now. I'll have another go first. Cheers

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    Also look at the search methods in the Arrays class.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    DANGEROUSSCION (December 8th, 2012)

  10. #9
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    I've tried again, by trying to alter codes given by University.

     
    import java.util.Scanner;
     
    public class BinarySearchString1 {
     
        public static void main(String[] args) {
            //create char array
            String ArtistArray[] = {"Alexis Jordan", "Bruno Mars", "Cee Lo Green", "Cheryl Cole", "Duck Sauce",
                                    "Katy Perry", "Mike Posner", "Nelly", "Rihanna", "The Saturdays"};
            output(ArtistArray[]);
     
            Scanner myKeyboard = new Scanner(System.in);
            System.out.println("\nWhich artist would you like?");
            String userType = myKeyboard.nextLine();
     
            String location = binarySearch(ArtistArray[], userType);
            if (location == null) {
                System.out.println("Artist not found");
            } else {
                System.out.println(userType + "is number" + location + "in the charts");
            }
        }
     
        public static int binarySearch(int[] arr, int item) {
            int low = 0;
            int high = arr.length - 1;
            int location = (low + high) / 2;
     
            while (item != arr[location] && high >= low) {
                if (item < arr[location]) {
                    high = location + 1;
                }
                location = (low + high) / 2;
            }
            if (item != arr[location]) {
                location = -1;
            }
            return location;
        }
    }

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    What happened when you compiled and executed the code?
    If there were errors, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    something to do with calling the ArtistArray....

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: output
    at ChartPosition.main(ChartPosition.java:10)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    --- Update ---

    line 10 and 16 errors, I got the red lines under ArtistArray

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Which sort method to use for a String Array to output the index?

    Can you compile the code and post the full text of the error messages? Don't try to execute it.
    Just compile it and copy the error messages.
    I don't know what the problem with the red lines is. The compiler's error messages are very good at describing the problem.
    Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: November 11th, 2012, 10:44 PM
  2. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  3. how to sort array by object string member value?
    By amgleason83 in forum Collections and Generics
    Replies: 9
    Last Post: March 16th, 2012, 05:29 PM
  4. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 03:27 PM
  5. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM