Results 1 to 4 of 4
  1. #1
    ChaosINC is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default Compare between 2 Strings

    Hey, I'm new to java.
    I need to write a simple program that gets 2 Strings and return a value based upon their lexical order.

    This is my code and it wouldn't return any value, plus I don't understand why it won't compile till I put a fourth return. any ideas?

    Thanks in advance.

    Java Code:
    import java.util.Scanner;
    
     public class sort1{
         private String str;
               
          public static void main(String[] args) { 
          Scanner read = new Scanner(System.in);  
          System.out.println("Type the first word");
          String s1 = read.next();
          System.out.println("Type the second word");
          String s2 = read.next();  
              
    
        }           
                
     public  static int Compare (String s1, String s2)
     {
             if (s1.compareTo(s2) > 0) {
         return -1;
    
            }
            if (s1.compareTo(s2) == 0) { 
           return 0;
            }
              else if (s1.compareTo(s2)>0) {
                  return 1;
          
            }
    return 0;
        }
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ChaosINC View Post
    Hey, I'm new to java.
    I need to write a simple program that gets 2 Strings and return a value based upon their lexical order.

    This is my code and it wouldn't return any value, plus I don't understand why it won't compile till I put a fourth return. any ideas?

    Thanks in advance.

    Java Code:
    import java.util.Scanner;
    
     public class sort1{
         private String str;
               
          public static void main(String[] args) { 
          Scanner read = new Scanner(System.in);  
          System.out.println("Type the first word");
          String s1 = read.next();
          System.out.println("Type the second word");
          String s2 = read.next();  
              
    
        }           
                
     public  static int Compare (String s1, String s2)
     {
             if (s1.compareTo(s2) > 0) {
         return -1;
    
            }
            if (s1.compareTo(s2) == 0) { 
           return 0;
            }
              else if (s1.compareTo(s2)>0) {
                  return 1;
          
            }
    return 0;
        }
    
    }
    Of course it doesn't return any value because your main method just reads two Strings and terminates, i.e. your Compare method isn't called at all. btw, method names start with a lower case letter.

    Have a close look at that method: if s1 > s2 is true you return -1 else if s1 == s2 is true you return 0 and then you compare s1 > s2 again; you did that already; if the test s1 > s2 fails and the test s1 == s2 fails you should unconditionally return 1 because s1 < s2 will always be true.

    kind regards,

    Jos
    Last edited by JosAH; 01-17-2010 at 08:36 AM.

  3. #3
    ChaosINC is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    Okay. thanks for pointing out my mistakes. I think I got this right this time..
    Second question, in this assignment I'm not allowed to use compareTo method unless I rewrite the method and include it in my code. since I'm new to java I need a little demonstration. thanks in advance.

    Java Code:
    
    import java.util.Scanner;
    
     public class sort {
         private String st;
         private String s1;
         private String s2;
     
      public int  compare (String s1, String s2)
     {    
         this.s1 = s1;
         this.s2 = s2;
         
         return 0;
        }
          public static void main(String[] args)
          { 
          Scanner read = new Scanner(System.in);  
          System.out.println("Type the first word");
          String s1 = read.next();
          System.out.println("Type the second word");
          String s2 = read.next();  
              
                
                
             if (s1.compareTo(s2) > 0) {
        System.out.println("1");
    
    
            }
            if (s1.compareTo(s2) == 0) { 
           System.out.println("0");
    
            }
              else if (s1.compareTo(s2)<0) {
             System.out.println("-1");
    
          
            }
    
        }
    
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ChaosINC View Post
    Okay. thanks for pointing out my mistakes. I think I got this right this time..
    Second question, in this assignment I'm not allowed to use compareTo method unless I rewrite the method and include it in my code. since I'm new to java I need a little demonstration. thanks in advance.
    Well, if your assignment says so you have to compare all the characters in the Strings yourself and decide if they're equal or one is (lexicographically) larger/smaller than the other. You have to use the String.charAt( ... ) method or convert both Strings to chararacter arrays.

    kind regards,

    Jos

    ps. your code still contains funny, useless parts.

Similar Threads

  1. Replies: 15
    Last Post: 11-06-2009, 10:31 AM
  2. [SOLVED] Cant compare strings sent by client
    By christuart in forum Networking
    Replies: 15
    Last Post: 09-01-2008, 03:24 AM
  3. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 07:04 PM
  4. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 03:57 AM
  5. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 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
  •