473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Just a few quick questions about Strings. Thank you!

Just a few quick questions.

1) How would I count the length of words in 5 certain strings.
SomeString.s1, SomeString.s2, etc..

2) How to compute the average word length?

3) How to compute the standard deviation (without duplicating code)

4) How to do all of this without duplicating the code for each String.

If anyone knows the answer to any of these questions, please reply with a solution; code or not.

PS - Arrays, and String.split, String.trim, or String tokenizers cannot be implemented.
Thanks a lot.
Oct 15 '07 #1
6 5423
r035198x
13,262 8TB
Just a few quick questions.

1) How would I count the length of words in 5 certain strings.
SomeString.s1, SomeString.s2, etc..

2) How to compute the average word length?

3) How to compute the standard deviation (without duplicating code)

4) How to do all of this without duplicating the code for each String.

....
And you don't have a clue on how to do any of them yet? Please post your ideas.

...
PS - Arrays, and String.split, String.trim, or String tokenizers cannot be implemented.
Thanks a lot.
That's unfortunate.
Oct 15 '07 #2
JosAH
11,448 Expert 8TB
PS - Arrays, and String.split, String.trim, or String tokenizers cannot be implemented.
Thanks a lot.
That's not true: all these classes and methods are already implemented; all you
have to do is use (invoke or call) them to make use of them. Are you the poster
who wanted to implement the 'Nim' game without wanting to use arrays and
methods?

kind regards,

Jos
Oct 15 '07 #3
That's not true: all these classes and methods are already implemented; all you
have to do is use (invoke or call) them to make use of them. Are you the poster
who wanted to implement the 'Nim' game without wanting to use arrays and
methods?

kind regards,

Jos
Hah, yes that's me.
I've passed in the last assignment, and I had no troubles.
I'm not allowed to implement arrays/string tokens, etc.. (theoretically) because my uncle has yet to teach us, so I can't jump ahead.

I have an idea of how to count the words.. my current code is as follows:

[PHP]import java.io.*;
public class WordCount
{
public static void main(String SomeStrings.s1, BufferedReader in)
{
String line;
long numWords = 0;
int index = 0;
boolean prevWhiteSpace = true;
while(index < line.length()){
char c = line.charAt(index++);
boolean currWhiteSpace = Character.isWhitespace(c);
if(prevWhiteSpace && !currWhiteSpace){
numWords++;
}
prevWhiteSpace = currWhiteSpace;
}
System.out.println(numWords);
}
}[/PHP]

As you can see I state in the method (String SomeStrings.s1)

SomeStrings.s1 is part of SomeStrings.class which I've placed in my Java Files folder.

Yet when I compile this I get a nasty red error:
Current document is out of sync with the Interactions Pane and should be recompiled!

Thanks a lot.
Oct 15 '07 #4
Okay well now I have this.
[PHP]
import java.util.Scanner;
public class Word
{
public static void main(String []args)
{
int count = 1;
String line = SomeStrings.s1;
long numWords = 0;
int index = 0;
boolean prevWhitespace = true;
while (index < line.length()) {
char c = line.charAt(index++);

boolean currWhitespace = Character.isWhitespace(c);
if (prevWhitespace && !currWhitespace) {
numWords++;
}
prevWhitespace = currWhitespace;
}
System.out.println("String: s"+count);
System.out.println(numWords);
}


}
[/PHP]

Except it prints the number of words..

How do I get the LENGTH of each word.. and store it into a temp variable for SomeStrings.s1... then determine the average word length?

I can't figure that out.
Oct 16 '07 #5
Okay well I think I've figured out how to count words.
My code is posted below.

Although at the top, where I have Line = SomeStrings.s1;
it won't refer to the .class file in my Java Folder.

I know the SomeStrings.class is properly placed because if I set up a println it *will* print out the string.

Although I can't when set as above.

Any thoughts?

Code:
[PHP]public class WordTest
{
public static void main(String[]args)
{

String Line = SomeStrings.s1;
long stringnumber = 1;
double WordLength = 0;
double NewWordLength = 0;
double PrevWordLength = 0;
double AvWordLength = 0;
int count = 0;
double NumberofWords = 1;
int index = 0;
char a = Line.charAt(count);
boolean prevWhitespace = true;
boolean currWhitespace = Character.isWhitespace(a);

//While loop number 1 (counting number of words)
while (index < Line.length())
{
char c = Line.charAt(index++);

boolean currWhitespace2 = Character.isWhitespace(c);
if (prevWhitespace && !currWhitespace2)
{
NumberofWords++;
}
prevWhitespace = currWhitespace2;
}
//While loop number 2 (counting lengths)
while(count <= Line.length())
{
if(!currWhitespace)
{
NewWordLength++;
}
count++;
}

WordLength = NewWordLength + PrevWordLength;
PrevWordLength = WordLength;

AvWordLength = WordLength/NumberofWords;

System.out.println("Average Word Length: "+AvWordLength);

}

}[/PHP]
Oct 17 '07 #6
Can somebody please help me?
I'm super stuck, yet I'm sure there's an easy way out.

I want to add up the length of each word.. in the code i'll provide, it has 2 words.

The output is:
1.0
2.0
Whitespace
1.0
2.0
3.0
4.0
Last Word Length: 4.0
Number of words: 2.0
Average Word Length: 2.0


As you can see, the "2.0" before the Whitespace, is the length of the first word.
My goal is to Add this (+) to the "4.0" which is the length of the second word.
Though it's getting the best of me. Please help.

[PHP]public class WordTest
{
public static void main(String[]args)
{

String Line = "hi john";
long stringnumber = 1;
double WordLength = 0;
double NewWordLength = 0;
double PrevWordLength = 0;
double AvWordLength = 0;
double NumberofWords = 1;
double TotalWordLengths =0;
int count = 0;
int index = 0;
boolean prevWhitespace = true;

//While loop number 1 (counting number of words)
while (index < Line.length())
{
char c = Line.charAt(index++);
boolean currWhitespace2 = Character.isWhitespace(c);
if (currWhitespace2 && !prevWhitespace)
{
NumberofWords++;
}
prevWhitespace = currWhitespace2;

}
//While loop number 2 (counting lengths of words)
while(count < Line.length())
{
char a = Line.charAt(count);
boolean currWhitespace = Character.isWhitespace(a);

if(!currWhitespace)
{
NewWordLength++;

System.out.println(NewWordLength);
}
else if (currWhitespace)
{
NewWordLength = 0;
System.out.println("Whitespace");
}

WordLength = NewWordLength;
AvWordLength = WordLength/NumberofWords;
count++;
}

System.out.println("Last Word Length: " +WordLength);
System.out.println("Number of words: " +NumberofWords);
System.out.println("Average Word Length: "+AvWordLength);

}

}
[/PHP]
Oct 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Troll | last post by:
John and Greg , Just wanted to say thank you for the help provided last week. Could not have done it without you :c) It was a rough two weeks and a very steep learning curve in trying to...
3
by: rlgoer1 | last post by:
Please bear with me. I'm still learning my netiquette with this group. I am using Access 2000 and after 3 reinstalls and 5 repairs, I am still getting the following error message: ...
8
by: dick | last post by:
I am just trying to print/report the results of a "filter by selection" which is done by right-clicking a form, filling in values, and "applying the filter." I have searched the newsgroups, and...
28
by: Nina | last post by:
I need to generate unique id for my application. Use system time to create id, which is the best fit, but I don't know how to do it. Please help. Thanks in advance for any input.
0
by: Janning Vygen | last post by:
Hi all, To all those people using mysql and asking if they should change to postgres, i just want them to tell that i came from mysql too but very (!) soon really wanted to use all these nice...
7
by: Tony K | last post by:
How can I customize, at runtime, the path for the OLEDB connection uses. I used the Add Data Source Wizard and found the path set by the wizard is ReadOnly.
4
by: NvrBst | last post by:
A few quick questions if possible. 1 - If I have a "List<..myList" and set it as a datasouce for say ListBox1, then change items in "myList" is the correct/only way to update the ListBox with...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.