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?
BlueJ Project help
Started by tayrob04, Apr 28 2010 02:19 PM
4 replies to this topic
#1
Posted 28 April 2010 - 02:19 PM
|
|
|
#2
Posted 28 April 2010 - 08:11 PM
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.
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
Posted 29 April 2010 - 09:54 AM
Since it can be separated by both commas and spaces i'd suggest you take a look into the stringtokenizer.
Because then you are able to split strings on multiple characters eg comma AND space.
example i found where they split on ';' and '=':
Quote
Why?
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
Posted 29 April 2010 - 11:29 PM
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
Posted 30 April 2010 - 04:06 AM
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.
This splits the string from spaces and commas.
"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.


Sign In
Create Account

Back to top









