DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6

Thread: Testing Strings

  1. #1
    Join Date
    Dec 2004
    Posts
    46

    Testing Strings

    Hi,

    Having a few problems with strings.
    I have this code and it wont compile.
    I am trying to write a program so that I can input two strings, and then tests the strings to see whether they are identical or, if not, which comes first in the alphabet.

    Code:
    public class Strings
    {
    
    public static void main (String[] args)
    {
       
       
    System.out.print("Please Enter First String: ");
    string1 = EasyIn.getInt();
    System.out.print("Please Enter Second String: ");
    string2 = EasyIn.getInt();
    
    if (string1.equals(string2))
       System.out.println("The Strings are Identical");
    else if (string1.compareTo(string2) >0 )
       System.out.println("The String1 comes first in alphabetical Order");
    else if (string1.compareTo(string2) < 0)
       System.out.println("The String2 comes first in alphabetical order");
      
    }
        }
    Can someone show me whats wrong with the code

    Thanks

  2. #2
    Join Date
    Dec 2004
    Posts
    46
    I have changed it and it now compiles:

    Code:
    public class Strings
    {
    
    public static void main (String[] args)
    {
       String string1, string2;
       
    System.out.print("Please Enter First String: ");
    string1 = EasyIn.getString();
    System.out.print("Please Enter Second String: ");
    string2 = EasyIn.getString();
    
    if (string1.equals(string2))
       System.out.println("The Strings are Identical");
    else if (string1.compareTo(string2) >0 )
       System.out.println("The First String comes first in alphabetical Order");
    else if (string1.compareTo(string2) >0 )
       System.out.println("The Second String comes first in alphabetical order");
      
    }
        }

    The only problem is when i input two strings such as hello and goodbye, its saying goodbye comes first in the alphabet

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Originally posted by T-Willis
    I have changed it and it now compiles:

    Code:
    public class Strings
    {
    
    public static void main (String[] args)
    {
       String string1, string2;
       
    System.out.print("Please Enter First String: ");
    string1 = EasyIn.getString();
    System.out.print("Please Enter Second String: ");
    string2 = EasyIn.getString();
    
    if (string1.equals(string2))
       System.out.println("The Strings are Identical");
    else if (string1.compareTo(string2) >0 )
       System.out.println("The First String comes first in alphabetical Order");
    else if (string1.compareTo(string2) >0 ) <--- uuups
       System.out.println("The Second String comes first in alphabetical order");
      
    }
        }
    Seems like the same test twice to me ...

    The only problem is when i input two strings such as hello and goodbye, its saying goodbye comes first in the alphabet
    eschew obfuscation

  4. #4
    Join Date
    Dec 2004
    Posts
    46
    Fixed my problem.
    I just had to change the greater and less signs.

  5. #5
    Join Date
    May 2004
    Posts
    219
    Originally posted by T-Willis
    The only problem is when i input two strings such as hello and goodbye, its saying goodbye comes first in the alphabet
    ...

    goodbye does come before hello

  6. #6
    Join Date
    Dec 2004
    Posts
    46
    Yes i realise that, sorry typing mistake

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links