Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings in Lexicographical order.

 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are correct. I don't know i just followed accordingly to the textbook.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book are you following?
 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Walter Savitch - Introduction to problem solving
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic