This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing words from a sentence in Java  RSS feed

 
Megan Long
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a program (name it Sentence) that asks the user to enter a sentence with more than 5 words.
a) Print the first five words from that sentence, each on its own line. NOTE: Be sure to manage gracefully the
sentence that has fewer than five words.
b) Print the middle 10 characters from the sentence. NOTE: Some sentences won’t have a perfect-middle 10
characters. In these cases, split the sentence so that there is one more character before the middle 10 than
after the middle 10.

I am not allowed to use any iterations or loops.

This is an example of how it's supposed to end up looking:
Enter a sentence with 5+ words: Hello my name is Computer Science 1001
FIRST WORD: Hello
SECOND WORD: my
THIRD WORD: name
FOURTH WORD: is
FIFTH WORD: Computer
MIDDLE 10 CHARACTERS: is Compute
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
So what have you tried so far and where are you stuck.
 
Megan Long
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty much I only know how to make it write a sentence. I'm not sure how to make it rewrite the first 5 words or pick the characters from the middle.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the methods in the String class for some useful tools for this assignment.  See the API doc for the String class's methods:
http://docs.oracle.com/javase/8/docs/api/index.html
Find the String class in the lower left frame, click on it and the class's API doc will show in the main frame.
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This exercise is probably meant to teach you about the following:
Java String.split()
Java String.substr()
how to access a Java array by index
Java array.length
if-statement in Java

That's pretty much everything you need to know to write a solution for this exercise. I'm assuming you already know how to use basic math operations in Java.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternative suggestion:
You can pass the entire sentence (a String) to the constructor of a Scanner and then use the Scanner to separate out the five tokens. Not sure what you would do about the rest of the sentence; I have never tried. Maybe the nextLine method would help.
A Scanner would not help you find the middle of the String, but you would have the original String still available to find the middle of.
 
Liutauras Vilda
Marshal
Posts: 4638
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Megan Long wrote:FIRST WORD: Hello
SECOND WORD: my
THIRD WORD: name
FOURTH WORD: is
FIFTH WORD: Computer
MIDDLE 10 CHARACTERS: is Compute

You took an easiest scenario. I'd argue it is a sentence.

Sentence in my opinion supposed to end with either: period, ellipses, question mark, exclamation mark, semi-collon maybe.
Also, there could be in the middle of sentence: colon, comma, hyphen, quotes, compound word - think if you need to keep that in mind.
Sentence even could start with a hyphen if there is a dialog between two or more people, so in that case you most likely would fail your (a) part right away which asking you to print first 5 words.

Is that an interview coding question? Nevertheless, it might be at some stage in your life, so always try to think outside the obvious things. What if sentence is an empty? What if you got a null reference? What if? How about...?

If I overcomplicated a bit, sorry, keep in mind that in other assignment such questions supposed to be asked by yourself, the more - better.

Gogl-mogl - Wikipedia, the free encyclopedia.
- Exactly five words, he answered.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!