• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

strings

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, is there a way to take input from the user. Say 3 words separated by spaces then display each new word on a new line. For example, I want to type into the console; Today is Friday. followed by enter key. The output should look like this
Today
is
Friday
I want to used only the following string operators.
.length(); , .indexOf(); , .subString();

I have the following code so far:

public class Split {

public static void main(String[] args) {

System.out.println("Please enter 3 words, followed by a space between each word. ");


String input;
input = Stdin.readln();

int blank = input.indexOf(" ");
String firstName = input.subString(0, blank);




System.out.println(firstName + blank+ "\n" + firstName + "\n"+ firstName);
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there is a way. However, I doubt that is the only answer you are looking for. It looks like you have figured out how to get the first "word" from the input. It also looks like you are having trouble trying to get the second "word" from the input.

In order to figure out how to do this, you should take a look at the Java API documentation. You can scroll the top left frame down until you find the java.lang package and click on it. This will bring up a list in the bottom left frame that shows all the interfaces and classes in this package. Then scroll down until you find the String class. When you click on the link, it will bring up the docs for String in the main frame on the right. Then scroll down until you find the indexOf() method. You should see that there are four versions. At the moment, you are using the version that takes a single String parameter. There is a similar one that takes a String and an int. If you click on the link for this method it will describe what the int represents. You will need this second version of getIndex() in order to locate the second "word".

I hope this helps.

Layne
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, its greatly appreciated. I'll look into the API documentation...
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Mike"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've changed my name to meet the web site standards. I am just testing it to see if my name displays. Sorry about that; Well anyways all is good now.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike:
Thanks for the reply, its greatly appreciated. I'll look into the API documentation...



Let us know what you figure out and how it works for you. Feel free to ask more questions, especially if you encounter any problems after reading the API docs.

Layne
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Well I have installed the java API on my machine. I have looked at it and tried to figure out my code; but still no luck!I did manage to get the program to work using the string tokenizer class, but my assignment is to do it by manipulating strings only. This is kind of where I am at the moment.

String input;
input=Stdin.readln(); read a sentence from the user.
but next I am unsure if I do need to convert the string into an integer before I can manipulate the string.For example, do I need this command,
word=Integer.parseInt(input); // to convert string into integer and assign it contents to the variable word.
As far as I understand input.indexOf(); will only display a numerica value in the console window. I don't see how I need that.I thought I should find where the blank is located but then use that to separate the words entered.
For example,
mac= input.indexOf(" "); // to find blank space inbetween words. SO i guess I do need the .indexOf(); method, but how do i convert that so the compiler knows to end the line with that. Then to start a new line with the following word. I am puzzled by these things. IF anyone could help that would be great and much appreciated....
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Smith:
input=Stdin.readln(); read a sentence from the user.
but next I am unsure if I do need to convert the string into an integer before I can manipulate the string.For example, do I need this command,
word=Integer.parseInt(input); // to convert string into integer and assign it contents to the variable word...



Mike, it sounds like you are fairly new to this game. This is a good beginning programming exercise because it will force you to think about the algorithm. That is why you need to write the program using simple methods like length(), indexOf() and substring() [NOT subString()]. There are more powerful tools available, but you would not learn as much using them. You will learn some Java along the way as well, but that is secondary.

So before thinking about the Java programming, just think about the problem. Your program will be given a string containing a number of words, and it needs to identify each word (so it can print them out). Your tools are:
  • length() will tell you how long the input String is
  • indexOf(int ch) will find the FIRST space in the String
  • indexOf(int ch, int fromIndex) will find the NEXT space (and the NEXT ...)
  • substring() can be used to get the word in between any two spaces

  • Step 1 for you is to forget about writing a Java program, and just work out in clear detail how you can use those tools to solve your problem. Imagine that you are playing a game, where another player has written down a string, but will not show it to you. You have to work out what the words are, but the only questions you can ask are length(), indexOf(int), indexOf(int, int) and substring(). Once you have worked out how to do that, write down the instructions you would give to someone else so that they could play the game for you.

    Once you have done that, you have your algorithm, and are ready to start programming.

    Oh, by the way, for now you should start with a simplified version of the problem:
  • The string has no leading of trailing spaces
  • The string contains no double spaces
  • The string contains at least two words
  • Once you have that program working, you should then remove these constraints one at a time, and make sure that the program still works when the string contains leading spaces, sequences of spaces, trailing spaces, only one word, and even no words at all! BUT, don't bother about these more complex cases until you have the basic case working.

    Finally, you are ready to start programming. BUT before you jump in, you need to make sure that you understand how these String methods work. They are a bit tricky for beginner programmers. I suggest you start by carefully reading the Java Doc entries for the String class, and these particular methods. Then you should experiment with them one at a time, until you are confident that you understand them. Most importantly, remember that String indexes begin at 0, not 1, and so the index of the last char in the string is length()-1.

    For example, write simple test programs to make sure that you understand what startements like the following will print:

    Only once you can correctly predict the output of such statements every time are you ready to start coding.

    I realize that this seems like a long and slow way to approach your problem. I can assure you that it is not. Your classmates who just start banging away at the keyboard with the standard trial-and-error approach will still be going once you are finished, they won't learn anywhere near as much, their programs won't be as good, and it is highly unlikely that their programs will deal correctly with non-standard inputs.

    Good luck with the challenge.
     
    Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic