New To Java - Need some help

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

[code][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);

}

}

}[code]

[1384 byte] By [tmlucky13a] at [2007-11-26 23:08:19]
# 1
Gah! Could we get some line breaks maybe? That's horrible to read.
hunter9000a at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 2
Try posting again, but with code tags* and line breaks.* http://forum.java.sun.com/help.jspa?sec=formatting
prometheuzza at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 3
when comparing strings use String.equals(String); Explain exactly what you are trying to do.
Vagabona at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 4

Sorry About my Code

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);

}

}

}

tmlucky13a at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 5
Third times a charm. I'm sure you can get [code] tags to work: [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url]
DrLaszloJamfa at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 6

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.

Yes because you compare the first character of each string and in this case both strings start with 't' and 't' is not less than 't' so you always hit the else statement.

>for ( int count = 0; count < 1; count++) {

<div class="jive-quote">

<b>if(ch1 < ch2)</b>

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

else

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

}

}

}

firoza at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 7

You want to loop over every character in the Strings, until you reach the end of one of them. If the current chars are the same, then continue to the next pair of chars. If one is less than the other, then that String comes before the other. If you get the end of one String and all the chars have been the same, then the shorter String comes first (unless your assignment says otherwise).

hunter9000a at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 8

What do you expect this code to do?

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);

}

In your example, s1 is tanned and s2 is tanning, so ch1 is 't' and ch2 is 't'.

You have a loop that executes once (count < 1), then you print something

based on the fact that 't' is not less than 't'.

Hmmm... Your code needs work. Start with paper and pencil.

Step by step describe how to compare two string to determine their

ordering. Do this before you start typing any more Java code.

DrLaszloJamfa at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 9
Ok. How do I get it to count every string to determine the out come.Like in tanned and tanning the first 4 letters are the same but when it count the next letter (e) comes before (i), but when I run it it come out tanned follows tanning. I need it to count every character. Thanks
tmlucky13a at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 10

Ok. How do I get it to count every string to

determine the out come.

Like in tanned and tanning the first 4 letters are

the same but when it count the next letter (e) comes

before (i), but when I run it it come out tanned

follows tanning. I need it to count every character.

Thanks

In that case, since you know that E comes before I in the alphabet, you know that the String that E came from comes before the String that I came from, so you're done! No need to look at the rest of the characters, since no matter what they are, the String's order won't change.

Message was edited by:

hunter9000

hunter9000a at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 11

Ok something like this should do it

boolean flag = false;

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

if(s1.charAt(i) < s2.charAt(i)){

flag = true;

break;

} else {

flag = false;

}

}

if(flag)

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

else

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

at this point you should be done.

Code testing left for user.

firoza at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 12
Code testing left for user.For example, try s1="a" and s2="ab"
DrLaszloJamfa at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 13

Ok something like this should do it

boolean flag = false;

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

if(s1.charAt(i) < s2.charAt(i)){

flag = true;

break;

} else {

flag = false;

if(flag)

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

System.out.println(s1 + " follows " +

s2);

at this point you should be done.

Code testing left for user.

No, that code doesn't work with s1 = "ba" and s2 = "ab", it return ba < ab. You also don't handle the case where the Strings are equal.

hunter9000a at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 14
Let's get married Dr. Laszlo Jamf, It's obviously meant to be :P
hunter9000a at 2007-7-10 14:02:54 > top of Java-index,Java Essentials,New To Java...
# 15
> Let's get married Dr. Laszlo Jamf, It's obviously meant to be :PI would definitely have to ask my wife for permission.
DrLaszloJamfa at 2007-7-21 19:21:58 > top of Java-index,Java Essentials,New To Java...
# 16
> I would definitely have to ask my wife for permission.I'll take that as a yes. NEW TOPIC: How do I change my username to TheFutureMrDrLaszloJamf?
hunter9000a at 2007-7-21 19:21:58 > top of Java-index,Java Essentials,New To Java...
# 17
> > Let's get married Dr. Laszlo Jamf, It's obviously> meant to be :P> > I would definitely have to ask my wife for permission.You moved to the wrong town then. May I suggest Bountiful?
DrClapa at 2007-7-21 19:21:58 > top of Java-index,Java Essentials,New To Java...
# 18
Bountiful?Sweeeet.
DrLaszloJamfa at 2007-7-21 19:21:58 > top of Java-index,Java Essentials,New To Java...

Java Essentials New Topic

Java Essentials Hot Topic

Java Essentials

All Classified