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

basic stringing need helps  RSS feed

 
Paul Phoenix
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I just started a java class and just had a question about stringing with spaces. My program seems to not register the strings after the spaces and it ends up ruining the whole program. I've been looking all over and i don't know if there is a keybord command for this or what. My program works with single strings with no space but not with ones with spaces. (sorry its a mess the indents arent registering on the forums for some reason)

here is what my program looks like.



This is what I need to Output.

Enter a string of characters: The Lord of the Rings
The length of string "The Lord of the Rings" is 21
Enter an integer between 0 and 20: 10
The character at index 10 of string "The Lord of the Rings" is 'f'
Enter another string of characters: Lord
The first occurrence of string "Lord" in string "The Lord of the Rings" is at position 4

any help would be apprciated thanks!

Edit by mw: Added CodeTags.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Phoenix wrote:... String stringOne=keyboard.next(); // Here i think is the first problem. keyboard.next seems to only get the first string and not the spaces and words after that...

Welcome to the Ranch!

It depends on what type of object "keyboard" references. But let's assume that keyboard is an instance of Scanner using System.in. The default delimiter is whitespace, so it would consider the "next" token to end with any whitespace. You can set this to whatever you like using Scanner's useDelimiter method. For example, a new line...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Phoenix wrote:...sorry its a mess the indents arent registering on the forums for some reason...

The trick is to UseCodeTags. I added them to your original post for you.
 
Paul Phoenix
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:
Paul Phoenix wrote:... String stringOne=keyboard.next(); // Here i think is the first problem. keyboard.next seems to only get the first string and not the spaces and words after that...

Welcome to the Ranch!

It depends on what type of object "keyboard" references. But let's assume that keyboard is an instance of Scanner using System.in. The default delimiter is whitespace, so it would consider the "next" token to end with any whitespace. You can set this to whatever you like using Scanner's useDelimiter method. For example, a new line...


the keyboard is using System.in . I tried using keyboard.useDelimiter("\n"); however i didn't notice any changes. It might be that I am using it wrong. I just don't get why the program isn't counting the spaces and other words in it's length
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Phoenix wrote:... I tried using keyboard.useDelimiter("\n"); however i didn't notice any changes...

On a Windows machine, you probably need to use a combination of carriage return and newline: "\r\n"

Better yet, to be platform independent, use...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Phoenix wrote:... the keyboard is using System.in...

And "keyboard" is an instance of...? Scanner, right?
 
Paul Phoenix
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:
Paul Phoenix wrote:... I tried using keyboard.useDelimiter("\n"); however i didn't notice any changes...

On a Windows machine, you probably need to use a combination of carriage return and newline: "\r\n"

Better yet, to be platform independent, use...


ahh that did it. Thanks for all your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!