Results 1 to 6 of 6
  1. #1
    N3VRMND is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default While loop comparing strings from user

    write a short program which will run in a loop asking the user for input with a simple prompt (for example "-> "). This will run until the user hits the enter key with no input (entering a String of length 0). The program will then display the lowest and highest (in alphabetical order) of the entered Strings. For example running the program might show:

    > Four score and
    -> seven years ago
    -> our fathers brought
    -> forth on this continent
    -> a new nation.
    ->
    first: a new nation.
    last: seven years ago

    This is what I have to do.

    Java Code:
    import java.util.Scanner;
    
    public class StringReader
    {
        static String str1, str2;
        
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            Boolean strLength = true;
            
            String first = "first";
            String last = "last";
    
            while (strLength)
            {
                System.out.print("=> ");
                str1 = scan.nextLine();
    
                System.out.print("=> ");
                str2 = scan.nextLine();
    
                if (str1.isEmpty() || str2.isEmpty())
                    strLength = false;
            }
    
            System.out.println("First: " + first);
            System.out.println("Last: " + last);
        }
    }
    That is what I have. I can get it to terminate when str1 or str2 is empty. I dont know how I can somehow scan the user input into a string that stays constant. I can get it to compare strings but it sorta screws up my code. If it comes to it I'll end up turning in bass ackwards code over nothing. Any help is appreciated. Thanks

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You need a List<String> to add the strings to while you are still doing the looping because you don't know how many strings you are going to capture.

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    this works the same way we would find the min, max of an array of integers.

    e.g.
    Java Code:
    Integer min = null;
    Integer max = null;
    
    int[] values = { 4, 6, 3 ,7 };
    
    for (int i = 0; i < values.length. i++) {
      if (min == null || values[i] < min.intValue()) {
        min = new Integer(values[i]);
      }
      if (max == null || values[i] > max.intValue()) {
        max = new Integer(values[i]);
      }
    }
    except we are using the string.compareTo to evaluate greater than or less than,
    and instead of a static array the values are read in dynamically from the user entry., but that's not any different than the fixed size for() loop case, because the find and store the min/max value is 'greedy'.

    Java Code:
    import java.util.Scanner;
    
    public class StringReader {
    
      public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
    
        String first = null;
        String last = null;
    
        System.out.print("=> ");
        
        String input = null;
       // read the next line. as long as the line is not the empty string.
        while ( (input = scan.nextLine()).length() > 0 ) {
          
          if (first == null || input.compareToIgnoreCase(first) < 0) {
            first = input;
          }
          if (last == null || input.compareToIgnoreCase(last) > 0) {
            last = input;
          }
          System.out.print("=> ");
    
        } // while
    
        System.out.println("first; " + first);
        System.out.println("last: " + last);
      }
    }
    output:
    Java Code:
    $>java StringReader
    => Four score and
    => seven years ago
    => our fathers brought
    => forth on this continent
    => a new nation.
    => 
    first; a new nation.
    last: seven years ago

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    - note, no List<String> needed :)

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Misread the requirements and thought they wanted all of them sorted.
    But now that you have done the full program for the OP can they now happily turn it in as theirs?

  6. #6
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    it's not like _I_ need the marks.

    I was hoping [the community] would benefit from a solid example

Similar Threads

  1. homework help comparing strings
    By djester55 in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 10:15 PM
  2. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  3. Problem Comparing Strings (its not what you think)
    By hilather in forum New To Java
    Replies: 7
    Last Post: 11-19-2008, 06:43 PM
  4. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  5. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM

Posting Permissions

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