The Artima Developer Community
Sponsored Link

Java Answers Forum
simple help needed

2 replies on 1 page. Most recent reply: Jan 29, 2003 8:33 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Jarkko Orjala

Posts: 1
Nickname: renney7
Registered: Jan, 2003

simple help needed Posted: Jan 29, 2003 10:50 AM
Reply to this message Reply
Advertisement
import java.io.*;
public class Shortest {
public static void main(String[] args) throws IOException { BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
String[] s = new String[3];
int j;
for (j = 0;j < s.length;j++) { System.out.println("Word "+(j+1)+":"); s[j] = b.readLine();
}

System.out.println("Shortest word(s) was: ");
int[] len = {s[0].length(),s[1].length(),s[2].length()};
int l = Math.min(len[0],Math.min(len[1],len[2]));
for (j = 0;j < len.length;j++) {
if (l == len[j]) { System.out.println(s[j]);
}
}
}
}

First, sorry for the chaotic presentation.

Im slowly getting desperate with this.. the program should print out all the three words starting from the shortest, not just the shortest one. I have tried to fine tune this code for some time now in order to make it work, but nope. It should be simple, but at the moment it is too challenging for me. The one who can teach how its done gets an award of salami pizza..

PS. Due to being busy at work and the deadline getting closer I dont have time to learn all the tutorials that deal with this..

And of course, thanks beforehand.


Joan Friedman

Posts: 6
Nickname: joan
Registered: Jan, 2003

Re: simple help needed Posted: Jan 29, 2003 11:57 AM
Reply to this message Reply
You're telling it to only print the shortest word with this conditional line:
if (l == len[j]) { System.out.println(s[j]);

If you want to print all 3 in order, sort the array of words and then print the sorted array. There's sorting you can use in the Collections package.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: simple help needed Posted: Jan 29, 2003 8:33 PM
Reply to this message Reply
/**
 * Shortest.java
 *
 * Simple demo of sorting Strings by length.
 *
 * Copyright 2003, Matt Gerrans.
 *
 * If you find this code useful, please buy a pizza for the next skinny
 * homeless person you see.
 *
 * Turning in this code as homework is a violation of ethical principals,
 * and punishable by severe penalties, even if you change the variable
 * names.
 */
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Arrays;
import java.util.StringTokenizer;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
 
public class Shortest 
{ 
   public static void main(String[] args) throws IOException 
   { 
      System.out.println( "Enter some gibberish, followed by Ctrl-Z." );
      BufferedReader input = new BufferedReader( new InputStreamReader(System.in) ); 
 
      // Load the strings into a list, since we don't know ahead of time how
      // many there might be:
      List stringList = new ArrayList();
 
      // Read each line, then extract words from each line with the 
      // StringTokenizer and add them to the list:
      String line = input.readLine();
      while( line != null )
      {
         StringTokenizer st = new StringTokenizer( line );
         while(st.hasMoreTokens())
            stringList.add( st.nextToken() );
 
         line = input.readLine();
      }
 
      if( stringList.size() > 0 )
      {
         Object [] stringArray = stringList.toArray();
   
         // Create a custom Comparator to sort by the length of the strings.
         Arrays.sort( stringArray, new Comparator()
                                   {
                                      public int compare( Object a, Object b )
                                      {
                                         return ((String)a).length() - ((String)b).length();
                                      }
                                   } );
 
         System.out.println( "Words, sorted by length: " ); 
   
         for( int i = 0; i < stringArray.length; i++ ) 
            System.out.println( stringArray[i] );
      }
      else
         System.out.println( "Try typing in some words next time." ); 
   }
}

Flat View: This topic has 2 replies on 1 page
Topic: print applet in htm Previous Topic   Next Topic Topic: Networking prob

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use