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

Thread: Sorting/Lexicographic =)

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sorting/Lexicographic =)

    Need help =)..

    Question: Write a program that reads in four strings and prints the lexicographically smallest and largest one:
    enter four strings:
    Charles
    Alex
    Delta
    Baker

    Output: The Lexicographic minimum is Alex.
    The Lexicographic maximum is Delta.

    //project
     
    import java.util.Arrays;
    import java.io.*;
     
     
     
    public class testing {
        public static void main(String[] args)throws IOException {
        	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    		String name1,name2,name3,name4;
     
    		System.out.println("Enter 1st: ");
    		name1 = br.readLine();
    		System.out.println("Enter 2st: ");
    		name2 = br.readLine();
    		System.out.println("Enter 3st: ");
    		name3 = br.readLine();
    		System.out.println("Enter 4st: ");
    		name4 = br.readLine();
     
            String[] names = {name1,name2,name3,name4};
            Arrays.sort(names);
            System.out.println(Arrays.toString(names));
     
        }
    }

    my program only sort the names.. can someone help me or give me a hint on how can i give the output(" The Lexicographic minimum is Alex. The Lexicographic maximum is Delta.")

    because i dont have any idea on how can i pick the minimum and maximum.. i dont know what will i do to my first program.. help me T_T

    thanks in advance =)
    Last edited by jcs990; March 11th, 2010 at 07:53 AM. Reason: [code][/code]

  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting/Lexicographic =)

    *bump* ..

    need someone to help me ..thxx

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Sorting/Lexicographic =)

    Please don't bumnp, but they will be the first and last elements in the array once its sorted lol

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting/Lexicographic =)

    Quote Originally Posted by Freaky Chris View Post
    Please don't bumnp, but they will be the first and last elements in the array once its sorted lol
    hi Sir Chris... sorry for the bump... i know it will be the first and last elements.. but i want to know how can i print the output of "The Lexicographic minimum is Alex.The Lexicographic maximum is Delta." .. i need the specific output..

    can u help me, how can i get the 1st and last item in the array.. after it was sorted..
    because i dont have any idea...

    please help me ..

    thank u Sir in advance

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Sorting/Lexicographic =)

    You can reference elements in the array using [] and an index.

    String[] someStrings = {"hello" , "world", "today", "is", "a", "sunny", "day"};
    System.out.println("The first string in the array is " + someStrings[0]);
    System.out.println("Teh last string in the array is " + someStrings[someStrings.length - 1]);

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    jcs990 (March 12th, 2010)

  7. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting/Lexicographic =)

    Quote Originally Posted by helloworld922 View Post
    You can reference elements in the array using [] and an index.

    String[] someStrings = {"hello" , "world", "today", "is", "a", "sunny", "day"};
    System.out.println("The first string in the array is " + someStrings[0]);
    System.out.println("Teh last string in the array is " + someStrings[someStrings.length - 1]);

    @Sir helloworld922.. thank you for helping me .... i greatly appreciate it

    More power to you ^_^


    @Sir Freaky Chris... thank you Sir GodBless..

Similar Threads

  1. thread sorting
    By thanos_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 06:23 PM
  2. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  3. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM
  4. Sorting Algorithms
    By Dalisra in forum Java Programming Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM
  5. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM