• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with my Java assignment

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the Assignment:

You will write a Java program AnalyseText.java that will do the following:

– initialise the variables that you will need to store all the information below;
– read the file theLovers.txt, line by line;
– count the number of lines in which Romeo says “Juliet”;
– count the number of lines in which Romeo says both “Juliet” and “love” (note that the word “love”
  may start with an upper-case or a lower-case letter);
– count the number of lines in which Juliet says “Romeo”;
– count the number of lines in which Juliet says both “Romeo” and “love”;
– count the number of scenes;
– in each scene, check if Romeo and Juliet are both present and, if that is the case, update the
variable that counts the number of those scenes;
– print all the relevant variables in a human-readable form.


My code seems to be quite weird and I'm not sure if the values it gives me are correct. Any suggestions are fine, even if I have to change my whole code.

This is what i have done so far:



Output:

Number of times Romeo says Juliet: 17
Number of times Romeo says Juliet and love: 56
Number of times Juliet says Romeo: 17
Number of time Juliet says Romeo and love: 56
Number of scenes: 140
 
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code indenting needs work.

We'd need to see a sample of the txt file in order to understand the format.

Per Javadocs
"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."

String#split( String regex )
The tokenizer you have doesn't specify how to deal with special characters. All well and good if there are none but I doubt that is the case in your situation. The trick with using split() is to come up with a regular expression that ignores everything other than letters. In regular expressions "[...]" denotes a set of characters. A leading "^" means not.  So, "[^a-zA-Z]" means any character that is not a letter. The trailing "+" means that there must be at least one occurrence to be interpreted as a delimiter, but you may have more than one. This regex should work 99% of the time, it depends on whether your text, for example, uses an apostrophe to indicate possessive nouns, e.g. "Romeo's love". Maybe this doesn't matter given your requirements. It seems that you only need to look for Romeo, Juliet,  Love, and Scene.
 
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful: the word urgent is regarded as an invitation to delay things.

Why are you using StringTokenizer? Have you read what its documentation says,

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.

You have made a good start; I presume you are correctly reading each line? Have you verified that? The code looks OK for that.
If things are urgent, you haven't got the time to sort that code out. But why are you using a tokenizer? There are better way to split Strings.

I had to stop writing for a bit, and had a chance to think. There are lots of ways you an split a String.
  • You can use the String#split() method, but beware: that takes a regular expression to split with. There is an example in the tokenizer documentation.
  • If you have the time, learn about regular expressions here. If you can create a regular expression for anything not a word character, one or more of them, that will give you the words.
  • Pass the entire line to a Scanner's constructor, and split it with next() and hasNext(). That is something else taking a regular expression.
  • Those multiple ifs don't look nice. You can use a switch on the first word to pass the text around, preferably to different methods.
  • I don't think I am allowed to say much more, but I will warn you to check whether you are likely to find such tokens as “Romeo's” or “Juliet,” (with comma). Also check how to change the case of your text in case “ROMEO” or similar appears anywhere.

    . . . and, welcome to the Ranch
     
    Campbell Ritchie
    Marshal
    Posts: 79278
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What about "\\W+" as a regex? Will there be any numbers or underscores in the text?
     
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I can see many problems with the way you parse the text; but it all depends on how the text is laid out.  If it looks like this then I don't think it will work at all.  But maybe thelovers.txt is formatted differently.

    Will the name of the one who speaks alway be the first word?  If not, how will you deal with a line like this: "Romeo and Juliet are doomed"?  By your logic, this would be Romeo saying Juliet's name.

    Knowing which scene you are in will probably need a flag.  Every time you see "Scene" you'll need to save the previous total (except the first time) and clear the total for this scene.
     
    Campbell Ritchie
    Marshal
    Posts: 79278
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Knute Snortum wrote:. . .  If it looks like this then I don't think it will work at all.  But maybe thelovers.txt is formatted differently. . . .

    Actually, if you have the name of the character as a line by itself, that makes it easier to parse:- if (line.equals("ROMEO")) ... It will be really awkward if the Bard has a character called Scene
     
    Anthony Giggs
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anthony Giggs wrote:This is the Assignment:

    You will write a Java program AnalyseText.java that will do the following:

    – initialise the variables that you will need to store all the information below;
    – read the file theLovers.txt, line by line;
    – count the number of lines in which Romeo says “Juliet”;
    – count the number of lines in which Romeo says both “Juliet” and “love” (note that the word “love”
      may start with an upper-case or a lower-case letter);
    – count the number of lines in which Juliet says “Romeo”;
    – count the number of lines in which Juliet says both “Romeo” and “love”;
    – count the number of scenes;
    – in each scene, check if Romeo and Juliet are both present and, if that is the case, update the
    variable that counts the number of those scenes;
    – print all the relevant variables in a human-readable form.


    My code seems to be quite weird and I'm not sure if the values it gives me are correct. Any suggestions are fine, even if I have to change my whole code.

     
    Campbell Ritchie
    Marshal
    Posts: 79278
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    . . . and?

    What have you done in the meantime?
     
    This parrot is no more. It has ceased to be. Now it's a tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic