#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Lexicographic order


    this is what i have to do: but i have not idea how to do it!!! help please!!!

    Write a program that reads three words, one on each line, and prints them in ascending lexicographic order. For example, if the input words are John, James and Adam, the output should be
    Adam
    James
    John
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    What are you having trouble with?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0
    Originally Posted by vaypet
    this is what i have to do: but i have not idea how to do it!!! help please!!!

    Write a program that reads three words, one on each line, and prints them in ascending lexicographic order. For example, if the input words are John, James and Adam, the output should be
    Adam
    James
    John
    Think about the question and take it one step at a time. The first line tells you to take in three words. So start with that. Once that it is complete, you need to "compare" *hint* *hint*. You can do that with loops. Then just print out the values. I don't expect anyone here will do the work for you. Try, see what you get, and post where you get stuck.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0
    ok. this is what i have up until now... when i try to compile it it saya " reached end of file while parsing"

    import java.util.Scanner;
    public class asi1
    {
    public static void main(String[] args)
    {
    String name1, name2, name3 ;
    boolean b;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("please enter three names: ");
    name1=keyboard.nextLine();
    name2=keyboard.nextLine();
    name3=keyboard.nextLine();


    boolean b = name1.equals(name2);
    // Check if identical ignoring case

    int b = name1.equalsIgnoreCase(name2);
    int i = name1.compareTo(name2);

    if (i < 0)
    {
    System.out.println(name1);
    }
    else if (i > 0)
    {
    System.out.println(name2);
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    You're missing two closing brackets at the end of the file. Also, you declare the variable "b" three times. Also, equalsIgnoreCase returns a boolean, not an int.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0
    This is the latest modifications i did to the file

    import java.util.Scanner;
    public class asi3
    {
    public static void main(String[] args)
    {
    String name1, name2, name3 ;
    boolean b, b1;
    int i, i1, i2,i3;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("please enter three names: ");
    name1=keyboard.nextLine();
    name2=keyboard.nextLine();
    name3=keyboard.nextLine();


    b = name1.equals(name2);
    // Check if identical ignoring case

    b1 = name1.equalsIgnoreCase(name2);

    i = name1.compareTo(name2);

    if (i < 0)
    {
    i1 = name1.compareToIgnoreCase(name3);

    if (i1 < 0)
    {
    System.out.println(name1);
    if (i1 > 0)
    {
    System.out.println(name3);
    i2 = name2.compareToIgnoreCase(name3);
    if (i2<0)
    {
    System.out.println(name2);
    System.out.println(name3);
    }
    if (i2>0)
    {
    System.out.println(name3);
    System.out.println(name2);
    }
    }
    }
    }
    else if (i > 0)
    {

    i1 = name2.compareToIgnoreCase(name3);

    if (i1 < 0)
    {
    System.out.println(name2);
    if (i1 > 0)
    {
    i2 = name1.compareToIgnoreCase(name3);
    if (i2<0)
    {
    System.out.println(name1);
    System.out.println(name3);
    }
    if (i2>0)
    {
    System.out.println(name3);
    System.out.println(name1);
    }
    }
    }
    }

    }
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    And? What problems (if any) are you having with this code?

    In the future, please use code or highlight tags to preserve the formatting of your code.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0
    the main issue i am having is that it lets me enter only 2 names instead of 3, and it skips over the third name, afterwards saying "process completed" i am using Jcreator...
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0
    Originally Posted by vaypet
    the main issue i am having is that it lets me enter only 2 names instead of 3, and it skips over the third name, afterwards saying "process completed" i am using Jcreator...
    Try taking in each value seperately

    System.out.print("Enter first string: ");
    name1 = scan.nextLine();

    System.out.print("Enter second string: ");
    name2 = scan.nextLine();

    System.out.print("Enter third string: ");
    name3 = scan.nextLine();

IMN logo majestic logo threadwatch logo seochat tools logo