Click to See Complete Forum and Search --> : A program that asks for two strings and reports their lexicographical ordering
Tmcclain
Mar 13th, 2007, 02:09 PM
I need some help with this program. Can somebody tell what I need to did in order to get my program started. Thanks
A program that asks for two strings and reports their lexicographical ordering, i.e. as they would appear in a lexicon that sorts them according to ASCII values. Do not use any predefined string comparison methods! The only predefined methods you may use are nextLine() of the Scanner class and length() and charAt(int i) of the String class.
EXTRA CREDIT: allow the user to decide whether they want to ignore case in the comparison (again without using predefined methods). Make sure you get the program working first without this feature.
Examples not ignoring case:
"abc" == "abc", "ABC" < "abc", "fishery" > "fish", "run?" > "run!"...
Examples ignoring case:
"abc$" == "ABC$", "Hat" == "haT", "cat" < "Hat", "Hatter" > "hat", ...
CornedBee
Mar 13th, 2007, 04:05 PM
ASCII values? Java's got no stinkin' ASCII values. Only UTF-16 unit values, and on a higher level Unicode code points.
But whatever.
Which part do you have problems with?
Tmcclain
Mar 13th, 2007, 07:26 PM
I don't how I should start this program. If you could help me get started maybe I can figure the rest out. Thanks
CornedBee
Mar 14th, 2007, 05:39 AM
You would start by implementing a lexicographical compare. If you don't know how that works, you look it up on the web.
Tmcclain
Mar 15th, 2007, 03:32 PM
I use compare to but my teacher only want us to use only predefined methods you may use are nextLine() of the Scanner class and length() and charAt(int i) of the String class. So can you tell my how I would set this up. Thanks
public class StringCompare
{
public static void main(String[] args)
{
String s1 = "abc",
s2 = "def";
// Compare "abc" and "def"
if (s1.compareTo(s2) < 0)
{
System.out.println(s1 + " comes before " + s2);
}
else if (s1.compareTo(s2) == 0)
{
System.out.println(s1 + " is equal to " + s2);
}
else if (s1.compareTo(s2) > 0)
{
System.out.println(s1 + " follows " + s2);
}
CornedBee
Mar 15th, 2007, 07:35 PM
You need to re-implement compareTo on your own. As I said, look up lexicographical comparing on the web. Don't expect me to give you any code.
Tmcclain
Mar 27th, 2007, 03:13 PM
I change my code to the way my teacher wanted it, but still have a little trouble. I am having trouble with my program counting all the character in the string. Like if I typed (tanned) in string1 and (tanning) in string2 it would print tanned follows tanning when it suspose to be tanned comes before tanning. Also having trouble getting it to equal to each other (print tanning is equal to tanning or print the two strings are equal). Can somebody please help me finish this program. Thanks
Here is my code below
import java.util.Scanner;
public class Strings
{
public static void main(String[] args)
{
String s1;
String s2;
Scanner scan = new Scanner (System.in);
System.out.println("Enter the first string:");
s1 = scan.nextLine();
System.out.println("Enter the second string:");
s2 = scan.nextLine();
//Sting length of the characters
System.out.println("Sting Length of s1: " + s1.length());
System.out.println("Sting Length of s2: " + s2.length());
//Compare the characters
char ch1 = s1.charAt(1);
char ch2 = s2.charAt(1);
for ( int count = 0; count < 1; count++) {
if(ch1 < ch2)
System.out.print(s1 + " comes before " + s2);
else
System.out.print(s1 + " follows " + s2);
}
}
}
System_Error
Mar 27th, 2007, 06:11 PM
You're not comparing each character in the strings. I would find the smaller string and use that as indexing. Then compare each character:
for (int index=0; index<smallerString.length(); ++index)
{
//-larger string comes first
if (largetString.charAt(index) < smallerString.charAt(index))
{
//-print out
//-break;
}
//-other comparisions
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.