Jump to content

BlueJ Project help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
tayrob04

tayrob04

    Newbie

  • Members
  • Pip
  • 1 posts
I need some help writing a code in BlueJ that you can enter a list of words separated by commas and spaces and when done the code finds the shortest word and prints the word, the longest word and prints the word and the average length and prints the average length number. Can anyone help me?

#2
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
You can the store the words in a text file( or even in a String, String array or ArrayList<String>).
You make a minWord, maxWord, avgLength static variables.
Then you run the code through a for loop, and save the smallest word in minWord, the biggest in maxWord and you increment avgLength by each word's length, and at the end you divide avgLength by the number of processed words.
You split the words with the split() method which takes a delimiter, in your case, ',' , and you get the word length by running .length() method on a String (ex: String a = "java"; print a.length(); // output 4)

Hope you understood me.

Edited by ksemeks, 29 April 2010 - 07:34 AM.

// d-_-b+

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Since it can be separated by both commas and spaces i'd suggest you take a look into the stringtokenizer.

Quote

Why?
Because then you are able to split strings on multiple characters eg comma AND space.



example i found where they split on ';' and '=':

import java.util.StringTokenizer; 


class STDemo { 

  static String in = "title=Java-Samples;" + 

      "author=Emiley J" + 

      "publisher=java-samples.com" + 

      "copyright=2007"; 

  public static void main(String args[]) { 

    StringTokenizer st = new StringTokenizer(in, "=;"); 


    while(st.hasMoreTokens()) {

      String key = st.nextToken(); 

      String val = st.nextToken(); 

      System.out.println(key + "\\t" + val);

    } 

  } 

}


//output

title Java-samples 

author Emiley J 

publisher java-samples.com 

copyright 2007



#4
Dreamcatcher

Dreamcatcher

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
StringTokenizer is very usefull...It also had many many fuctions with which to check almost everything that has to do with substringing
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." -- Dennis Ritchie

#5
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
From Sun's homepage
"StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."

In most cases it would be better idea to use String.split instead
Split takes regex as an argument, so you can easily put several different symbols in there.

String[] s = "Hello,world hello world".split("[, ]");
System.out.println(Arrays.toString(s));

This splits the string from spaces and commas.