Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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 18 of 18

Threaded View

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java dictionary search algorithm

    You should not use any other String methods apart from length() and charAt(), or the Java binarySearch method.

    boolean lessThan(String s, String t, int n)
    This method should return true if the first n elements of the array s come before the first n elements of the array t in dictionary order. For example, if n is 4:

    • lessThan("binary", "bind", 4) is true, because the first four characters differ, and 'a' is less than 'd'.
    • lessThan("binder", "binding", 4) is false, because the first four characters match.
    • lessThan("binding", "binder", 4) is false, because the first four characters match.
    • lessThan("bin", "binary", 4) is true.
    • lessThan("bit", "binary", 4) is false.



    What I've done so far:
    public boolean lessThan(String s, String t, int n) {
            boolean lessThan = false;
            for (int i =0; i<n; i++){
                if (s.charAt(i) == t.charAt(i)){
                    lessThan = false;
                }else{
                    lessThan = true;
                }
            }return lessThan;
        }
    which only fulfils when first 4 character match outputting false, but ive got no clue on how to implement the 1,4,5th bullet points.
    Last edited by drdre; March 16th, 2019 at 12:11 PM.

Similar Threads

  1. Beginner program w/ file, arrays and binary search algorithm - HELP!
    By lisagurra in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2018, 09:23 AM
  2. Incorporating a search dictionary
    By seventhCircuit777 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 24th, 2018, 05:56 PM
  3. Binary Search Algorithm
    By omgitztmarie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2014, 06:22 AM
  4. Algorithm for building an dynamic dictionary and auto spell corrector?
    By Dinesh Raja in forum Algorithms & Recursion
    Replies: 7
    Last Post: December 5th, 2013, 08:20 AM
  5. Replies: 0
    Last Post: March 28th, 2010, 01:27 PM

Tags for this Thread