473,787 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Just a few quick questions about Strings. Thank you!

27 New Member
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 5447
r035198x
13,262 MVP
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 Recognized Expert MVP
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
SuperDuper
27 New Member
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(ind ex++);
boolean currWhiteSpace = Character.isWhi tespace(c);
if(prevWhiteSpa ce && !currWhiteSpace ){
numWords++;
}
prevWhiteSpace = currWhiteSpace;
}
System.out.prin tln(numWords);
}
}[/PHP]

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

SomeStrings.s1 is part of SomeStrings.cla ss 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
SuperDuper
27 New Member
Okay well now I have this.
[PHP]
import java.util.Scann er;
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(ind ex++);

boolean currWhitespace = Character.isWhi tespace(c);
if (prevWhitespace && !currWhitespace ) {
numWords++;
}
prevWhitespace = currWhitespace;
}
System.out.prin tln("String: s"+count);
System.out.prin tln(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
SuperDuper
27 New Member
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.cla ss 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(cou nt);
boolean prevWhitespace = true;
boolean currWhitespace = Character.isWhi tespace(a);

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

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

WordLength = NewWordLength + PrevWordLength;
PrevWordLength = WordLength;

AvWordLength = WordLength/NumberofWords;

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

}

}[/PHP]
Oct 17 '07 #6
SuperDuper
27 New Member
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 TotalWordLength s =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(ind ex++);
boolean currWhitespace2 = Character.isWhi tespace(c);
if (currWhitespace 2 && !prevWhitespace )
{
NumberofWords++ ;
}
prevWhitespace = currWhitespace2 ;

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

if(!currWhitesp ace)
{
NewWordLength++ ;

System.out.prin tln(NewWordLeng th);
}
else if (currWhitespace )
{
NewWordLength = 0;
System.out.prin tln("Whitespace ");
}

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

System.out.prin tln("Last Word Length: " +WordLength);
System.out.prin tln("Number of words: " +NumberofWords) ;
System.out.prin tln("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
1434
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 understand what the heck I'm doing...
3
5822
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: MSJETOLEDB40.DLL failed to register. HRESULT - 2147024865 I found the problem file in the c:\windows\system directory. Now I know there is a button in access 2000 somewhere to care of little problems like
8
6534
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 there are many examples. BUT, they fail sometimes. The techique is to pass the form's Me.filter as the "where condition" in a Docmd.openreport statement in code behind a "print button" on the form.
28
17696
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
1027
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 postgresql features which make a real database (like subselecting, triggers, ...) And i build a dynamic web application where ALL (99,99%) pages are dynamic pages build with PHP and postgresql. Databse is more than 2 GB and performance is...
7
2202
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
1283
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 the following? ListBox1.DataSource = null; ListBox1.DataSource = myList; 2 - BindingList<...is the same as List<...except when it is set as
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.