Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

montreal176

How to split strings without arrays?

10 posts in this topic

Hi everyone! 

 

I have a quick question if someone could answer! I am trying to split various sentences into just words. For example one sentence is "The mathematics behind atmospheric predictions is complex.". We are not allowed to store them in an array and the teacher said something about "indexOf" however the day she went over the indexOf i was absent and am going to see her tomorrow, I would like to get a head start first though.

 

 

Thanks!!

Share this post


Link to post
Share on other sites

I am trying to split various sentences into just words. We are not allowed to store them in an array

How are you going to store the values then? Do you even need to store the values, or do you just need to print out each separate word? Tell us what exactly you want to do, you haven't given enough details for me to help you.

Share this post


Link to post
Share on other sites

How are you going to store the values then? Do you even need to store the values, or do you just need to print out each separate word? Tell us what exactly you want to do, you haven't given enough details for me to help you.

You're right, My bad. I am trying to get the words into a variable which i can count the length of each word in the sentence. Square it and get the average length of the words in the sentence, then to multiply it by how many words are in said sentence. Then use if statements depending on the value of that to do another task. Is that what your looking for?

Share this post


Link to post
Share on other sites

Take a look: http://stackoverflow.com/questions/11726023/split-string-into-individual-words-java

 

You've got a few options - most using arrays, which apparently aren't allowed. I believe that your teacher wants you to do this assignment in a specific way based on what you've learned so far.

Thanks man! Gonna look there, if i cant find anything, I'll go speak with her tomorrow. Thanks again.

Share this post


Link to post
Share on other sites

Not using arrays for this is stupid.

 

Anyways, you'd want to use recursion or a while loop. Just do something like this:

public static void doStuff(String sentence) {    int initialCount = sentence.size();    while ((sentence = sentence.instanceOf(" ")) != -1) {        //Stuff with the result    }    //Stuff with your previous calculations}

You should be able to figure out the rest from there.

 


 

Also, @Flamo353, change your logo please.

Edited by TacoManStan

Share this post


Link to post
Share on other sites

Not using arrays for this is stupid.

 

Anyways, you'd want to use recursion or a while loop. Just do something like this:

public static void doStuff(String sentence) {    int initialCount = sentence.size();    while ((sentence = sentence.instanceOf(" ")) != -1) {        //Stuff with the result    }    //Stuff with your previous calculations}

You should be able to figure out the rest from there.

 


 

Also, @Flamo353, change your logo please.

Right? the prof wouldnt accept it because it was "too easy" and thank you! Will give that a try!

Edited by montreal176

Share this post


Link to post
Share on other sites

@montreal176
 
Not exactly sure what the assignment is, but I wrote this according to what you said above (it doesn't take punctuation into account and only works for valid sentences which I'm sure your professor is providing):
 

int numWords;int lengthSum;	boolean calculate(String sentence){	int index = sentence.indexOf(" ");		String word = index == -1 ? sentence : sentence.substring(0, index);			numWords++;	lengthSum += word.length();			if(index == -1)		return true;			return calculate(sentence.substring(index + 1));}

If you need any extra help let me know.
 
Edit: Using this for your example sentence, it gave these answers:
 

Please enter in a sentence: The mathematics behind atmospheric predictions is complex.Average word length: 7.0Multiplied by each word in sentence: 49.0
Edited by Final Calibur

Share this post


Link to post
Share on other sites

Right? the prof wouldnt accept it because it was "too easy" and thank you! Will give that a try!

Tell your professor she's a fucking idiot. The entire goddamn point of programming is for it to be easy. Writing 10 lines when you can just do String#split is so stupid it makes my brain hurt. 

1 person likes this

Share this post


Link to post
Share on other sites

Tell your professor she's a fucking idiot. The entire goddamn point of programming is for it to be easy. Writing 10 lines when you can just do String#split is so stupid it makes my brain hurt.

She won't let us because she hasn't taught it, even though when she asked who could do it everyone raised their hand. That's why Im struggling with this assignment because I'm just not sure why we can't show out knowledge

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.