aspose file tools*
The moose likes Beginning Java and the fly likes strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "strings" Watch "strings" New topic
Author

strings

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
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);
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Thanks for the reply, its greatly appreciated. I'll look into the API documentation...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Sep 23, 2005
Posts: 85
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....
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: strings
     
    Similar Threads
    Where are the dates
    comparison methods
    Writing to a file
    Need help extending a program.
    how to declare the length of the word entered?