aspose file tools*
The moose likes Beginning Java and the fly likes Strings in Lexicographical order. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings in Lexicographical order." Watch "Strings in Lexicographical order." New topic
Author

Strings in Lexicographical order.

Sarah Tod
Greenhorn

Joined: Mar 03, 2013
Posts: 20
Hi All,

I was asked to create strings from keyboard & output has to display string that would be 2nd if arranged in lexicographic order.

//

//import java.util.Scanner;

public class lexicostrings
{
public static void main(String[] args)
{

String s1, s2, s3, ex; //Declared Variables

Scanner kb = new Scanner(System.in);

System.out.print("Enter 1st String: "); //Userinput 1st.

s1 = kb.next();

System.out.print("Enter 2nd String: "); //Userinput 2nd.

s2 = kb.next();

System.out.print("Enter 3rd String: "); //Userinput 3rd.

s3 = kb.next();


if (s1.compareTo(s2) < 0) //
System.out.println(s1 + "precedes" + s2 +
" in lexicographic ordering");

else if (s2.compareTo(s3) > 0)
System.out.println(s2 + "follows" + s3 +
" in lexicographic ordering");

}
// }
u.
There was no output. What am i missing? Please advise. Thank you.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi Sarah

Use code tags to paste code. See how much more legible it is:



Now back to your question. What re the 3 Strings that you are providing as input? Probably none of the pairs(s1/s2 or s2/s3) satisfy your checks. Hence, no output. Am I correct?


~ Mansukh
Sarah Tod
Greenhorn

Joined: Mar 03, 2013
Posts: 20
Yes, you are correct. I don't know i just followed accordingly to the textbook.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Which book are you following?
Sarah Tod
Greenhorn

Joined: Mar 03, 2013
Posts: 20
Walter Savitch - Introduction to problem solving
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
There must be a logic error in your code. You need to write down your algorithm and logic on a sheet of paper. Work out how you are going to identify the middle of three Strings arranged in order. You have not been told to print which comes first or last, only which comes in the middle. What happens if you write Sarah for all three?
Also your code style is poor. Look at these suggestions. Make sure to put {} after your ifs. Those formats make it much easier to see what the flow of control is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings in Lexicographical order.