Help

I am comparing strings and having trouble with my code. Here are my errors and my code is below.

Strings.java:42: ')' expected

System.out.println(s1 " comes before " s2);

^

Strings.java:42: not a statement

System.out.println(s1 " comes before " s2);

^

Strings.java:42: ';' expected

System.out.println(s1 " comes before " s2);

^

Strings.java:44: ')' expected

System.out.println(s1 " follows " s2);

^

Strings.java:44: not a statement

System.out.println(s1 " follows " s2);

^

Strings.java:44: ';' expected

System.out.println(s1 " follows " s2);

^

Strings.java:49: reached end of file while parsing

}

[publicclass Strings

{

publicstaticvoid main(String[] args)

{

String s1, 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("String Length of s1: " + s1.length());

System.out.println("String Length of s2: " + s2.length());

//Compare the characters

char ch1, ch2;

for (int i = 0; (i < s1.length()) || (i < s2.length()); i++){

ch1 = s1.charAt(i);

ch2 = s2.charAt(i);

if(ch1 < ch2){

System.out.print(s1 +" comes before " + s2);

return;

}

if (ch1 > ch2){

System.out.print(s1 +" follow " + s2);

return;

}

if (s1.length()< s2.length()){

System.out.println(s1" comes before " s2);

}elseif (s1.length() > s2.length()){

System.out.println(s1" follows " s2);

}else{

System.out.println("The names are the same");

}

}

}

[3107 byte] By [tmlucky13a] at [2007-11-27 0:45:02]
# 1
Compare these two lines. Hmmm...System.out.print(s1 + " comes before " + s2);System.out.println(s1 " comes before " s2);
DrLaszloJamfa at 2007-7-11 23:09:48 > top of Java-index,Java Essentials,New To Java...
# 2

the correct code is as follows:

import java.util.Scanner;

public class Strings

{

public static void main(String[] args)

{

String s1, 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("String Length of s1: " + s1.length());

System.out.println("String Length of s2: " + s2.length());

//Compare the characters

char ch1, ch2;

for (int i = 0; (i < s1.length()) || (i < s2.length()); i++) {

ch1 = s1.charAt(i);

ch2 = s2.charAt(i);

if(ch1 < ch2){

System.out.print(s1 + " comes before " + s2);

return;

}

if (ch1 > ch2){

System.out.print(s1 + " follow " + s2);

return;

}

if (s1.length()< s2.length()){

System.out.println(s1 +" comes before " +s2);

}else if (s1.length() > s2.length()) {

System.out.println(s1+ " follows "+ s2);

}else {

System.out.println("The names are the same");

}

}

}

}

alwar.sumita at 2007-7-11 23:09:48 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks for the help. One more thing. When I run my code everything works but, it keep looping. For example

Enter the first string:

tanning

Enter the second string:

tanned

String Length of s1: 7

String Length of s2: 6

tanning follows tanned

tanning follows tanned

tanning follows tanned

tanning follows tanned

tanning follow tanned

Another example is it tell me the names are the same and there not.

Enter the first string:

fish

Enter the second string:

feed

String Length of s1: 4

String Length of s2: 4

The names are the same

fish follow feed

tmlucky13a at 2007-7-11 23:09:48 > top of Java-index,Java Essentials,New To Java...
# 4
The String class has methods (equals and compareTo) that will do all this work for you and get the logic right!
floundera at 2007-7-11 23:09:48 > top of Java-index,Java Essentials,New To Java...