Results 1 to 7 of 7
  1. #1
    Wumbo105 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Ordering strings lexicographically from a file?

    The file I'm reading in is just a .txt file that actually has numbers, but I read them in as strings, because int wasn't exactly working.
    Here's what I got so far.

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.math.*;
    public class FunTime {
    public static void main(String[] args) throws FileNotFoundException {
    File myFile = new File ("funtime.txt");
     
    Scanner sc = new Scanner(myFile);
    String a = sc.next();
    String b = sc.next();
    String c = sc.next();
     
    int d = a.compareTo(b);
    if (d > 0) {
    int e = b.compareTo(c);
    if (e > 0) {
    System.out.print(c);
    System.out.print(" "+b);
    System.out.println(" "+a);
    }
    if (e < 0) {
    System.out.print(b);
    System.out.print(" "+c);
    System.out.println(" "+a);
    }
    }
    if (d < 0) {
    int f = a.compareTo(c);
    if (f > 0) {
    System.out.print(c);
    System.out.print(" "+a);
    System.out.println(" "+b);
    }
    if (f < 0)  {
    System.out.print(a);
    System.out.print(" "+c);
    System.out.println(" "+b);
     
    }
    }
     
     
    
    }
    }
    So basically, the .txt file has 3 numbers on it's first line, and what I have to do is read that line in, and print them out in order, smallest to largest. What I have is incomplete, I figured making an if loop for every possible combination would work eventually, but it's extremely inefficient. I'm fairly certain there's an easier way. I'm about to punch my screen.
    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Ordering strings lexicographically from a file?

    print them out in order, smallest to largest
    You wouldn't need a loop for 3 values. You should be able to do it with a few if statements.

    The variable names makes it hard to understand the code. Better names would make it easier.
    What does d contain? Or e or f???
    Instead of d, how about a_vs_b
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Wumbo105 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Ordering strings lexicographically from a file?

    The variable name can change, I understand that part. But like you said, it should be done in a few if statements, and that's what I'm trying to figure it out.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Ordering strings lexicographically from a file?

    When two values are compared one will be lower.
    Compare that lower one to the third one. The low one of those two is the lowest of the three, the higher one needs to be compared to the higher one at the first compare.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Wumbo105 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Ordering strings lexicographically from a file?

    Yes. Once again, thanks for telling me something I already know. HOW to do that is what I'm looking for.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Ordering strings lexicographically from a file?

    If you already have the algorithm for solving the problem, then you should be able to code it.
    I just gave you most of the algorithm for solving the problem. You need to finish it.
    HOW to do that
    Use nested if statements. Here is some pseudo code:
    Java Code:
    if( a vs b) {
      //  here a is lower
      now test a vs c
      if c lower its the lowest
      else a is lowest
    } else {
      // here  b is lower
      now test b vs c
      etc
    }
    Finish up the pseudo code and then write the code.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Wumbo105 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Ordering strings lexicographically from a file?

    Ah okay. Yeah, I think I can take it from there. I just figured there would be somewhat of a single-line solution that would find the lowest of whatever is given. Thanks.

Similar Threads

  1. Replies: 5
    Last Post: 05-03-2012, 12:21 AM
  2. re-ordering problem please help me...
    By gomdohri in forum New To Java
    Replies: 1
    Last Post: 09-08-2011, 07:15 PM
  3. what is natural ordering???
    By blueduiker in forum New To Java
    Replies: 1
    Last Post: 02-24-2010, 10:51 AM
  4. getting all possible ordering of vector
    By sara12345 in forum New To Java
    Replies: 3
    Last Post: 01-08-2010, 10:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •