title: Shortest and Longest word in a sentence in Java
body: <p>I am new in Java Programming! So tried to solve a problem to find the shortest and longest word in a sentence. My program is shown below . I hope you people can show me the right direction about my program -</p>
  
  <pre><code>public class StringProblems {
      void shortAndLongWord(String str) {
          String sw = "", lw = "";
          int s = str.length(), l = 0;
          String words[] = str.split(" ");
  
          for(String word:words) {
              if(word.length()&lt;s)
                  sw = word;
              else if(word.length()&gt;l)
                  lw = word;
          }
  
          System.out.println("LONGEST WORD : "+lw);
          System.out.println("SHORTEST WORD : "+sw);
      }
  
      public static void main(String[] args) {
          Scanner scr = new Scanner(System.in);
  
          StringProblems obj = new StringProblems();
          System.out.printf("Enter a line to get shortest and longest word:");
          String str = scr.nextLine();
          str += " ";
          obj.shortAndLongWord(str);
      }
  }
  </code></pre>
  
  <p><strong>Output of this program is :</strong>
  *Enter a line to get shortest and logest word:This is Sentence</p>
  
  <p>LONGEST WORD : 
  SHORTEST WORD : Sentence**</p>
  
  <p>I dont know where my logic went wrong! Please help me to solve!</p>