1

I'm trying to use the same format of this code which I followed through a tutorial. It will print the longest word as well as the length of the longest word. The tutor told me that to find the shortest word I have to flip the if statement to be less than ('<') instead of greater than... however, after I enter any string, when I run the program it returns:

"Shortest Word: "", length: 0"

I'm not sure how to fix this so it looks for an actual word and not an empty character.. I'd like to follow the same logic here without using arrays as well.

Scanner in = new Scanner(System.in);

System.out.println("Please enter a phrase: ");
String phrase = in.nextLine();


String w = "";
String lw = "";       
int l;
char ch;


phrase = phrase + " ";
l = phrase.length();
int i;
for (i=0; i < l; i++) {
    ch = phrase.charAt(i);
    if(ch != ' ') {
        w = w + ch;
    } else {
        if(w.length() > lw.length()) {
            lw = w;
        }
        w = "";
    }
}

System.out.println("Longest Word: \"" + lw + "\", length: "+ lw.length());
9
  • 1
    the code you have shown can't possibly return the output you show. Commented Feb 12, 2020 at 7:44
  • I forgot to add, the user inputs any phrase they choose to Commented Feb 12, 2020 at 7:46
  • 1
    be that as it may, the code you've shown can not produce "Shortest Word: "", length: 0" as output Commented Feb 12, 2020 at 7:47
  • First, store lw.length() in a variable instead of always calculating it. Then you have a check w.length() > lwLength instead. If you filp it, that is a good first step — but in order for it to work, whereas longest word needs to start short and get longer as you find better candidates, shortest word needs to start long and get cut shorter. Start with lwLength = Integer.MAX_VALUE. Commented Feb 12, 2020 at 7:47
  • I agree with @Stultuske. How can System.out.println("Longest Word: \"" + lw + "\", length: "+ lw.length()); suddenly create the output of "Shortest Word: "", length: 0"? Commented Feb 12, 2020 at 7:47

1 Answer 1

1

One thing you have to do is change

if(w.length() > lw.length())

to

if(w.length() < lw.length())

However, that's not enough, since lw in initialized to an empty String, so the condition will always be false (w.length() will never be < 0).

Therefore you also have to check whether lw is still empty:

if(w.length() < lw.length() || lw.isEmpty ()) {
    lw = w;
}

The full code:

Scanner in = new Scanner(System.in);

System.out.println("Please enter a phrase: ");
String phrase = in.nextLine();

String w = "";
String lw = "";       
int l;
char ch;

phrase = phrase + " ";
l = phrase.length();
int i;
for (i=0; i < l; i++) {
    ch = phrase.charAt(i);
    if(ch != ' ') {
        w = w + ch;
    } else {
        if(w.length() < lw.length() || lw.isEmpty ()) {
            lw = w;
        }
        w = "";
    }
}

System.out.println("Shortest Word: \"" + lw + "\", length: "+ lw.length());
Sign up to request clarification or add additional context in comments.

4 Comments

This is excellent, I really appreciate this tip.. In addition if two or more words are the same length, how would I make the latter (last) shortest word print instead of the first
@gingerhaus38 you can do that by changing < to <=
Got it, I made a mistake earlier, now for some reason the code still produces "" as the shortest character although I specifically changed this how I should
@gingerhaus38 I tried the code I posted and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.