Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Taking a part of a string that's from a user's input  RSS feed

 
Greenhorn
Posts: 19
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm at a loss here. What I have is a string that picks up if a user types a certain command. If they don't add an argument at the end, the code block doesn't run. What I need to do is remove the basic command from the string, and storing the user's argument in a separate string. I have attempted using split's but every result I get doesn't have two separate indexes. If I use [1] instead of [0] on the split string, i get an ArrayOutOfBoundsException: 1


Example:
The result of grabbing the user's input
What I want:
 
Rancher
Posts: 3747
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you splitting the String?
 
Drew Hedgecoke
Greenhorn
Posts: 19
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe I was typing the main command itself into it.
The entered command:

What I tried to do:
 
Marshal
Posts: 61713
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to escape the dollar sign, which, I think, is a meta‑character denoting one end of the line or the other. What you are using is a “regular exression”, which you can read about in the Java™ Tutorials. If you get the expression right, you will splt the String into before that and after that, probably a two‑element array with the first being the empty String "".
If you always have the same length introduction, try the substring() method. But you will have problems if there are things like double spaces in the input.
Try a Scanner, calling next() twice followed by nextLine()
 
Drew Hedgecoke
Greenhorn
Posts: 19
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little off topic, but as I'm a beginner at java with basic knowledge, people continuously point me to these documentation websites. However, every time I attempt to learn from there, they use language that is hard to understand except for user's who are more advanced into the subject. I find myself reading the same sentences four or five times over and can't wrap my brain around them. The only reason I've gotten this far was simply by playing around with the program and codecademy.

Anywho, I'll try out your advice and see if it gets anywhere.
 
Drew Hedgecoke
Greenhorn
Posts: 19
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your advice worked. I checked up on the documentation and used the magic of ctrl+f. Exactly like you said. I just used "\\" before the dollar sign, and it split correctly. I appreciate the help.
 
Campbell Ritchie
Marshal
Posts: 61713
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Drew Hedgecoke wrote:. . . . I just used "\\" before the dollar sign, and it split correctly.

Well done

I appreciate the help.

That's a pleasure I sometimes get it right.
I suggest you replace the middle space in the regular expression by \\s+. You will doubtless know from your reading that \\s+ represents any number > 0 of whitespace characters, which will allow your users to use double spaces. Also consider adding \\s+ at the end of your regular expression, which will strip any leading spaces from the remainder of the text. By the way: lots of people abbreviate “regular expression” to “regex”.

There are three kinds of standard documentation for Java®. One is the API documentation, which is definitive for the specification of the methods, though it is possible that somebody made a mistake writing something. It is written on the assumption shown here that the readers are experienced programmers. The second, which is also definitive, is a specification, the best‑known being the Java® Language Specification, usually called the JLS. Both those contain parts very hard to read, unfortunately. The third is tutorials, this one being provided by Oracle (formerly by Sun), and most tutorials are easy to read. I suggest you should always read the documentation, and then decide which parts you can't understand and need to know about. Quote them here and ask; we shall be only too happy to answer. You will gradually find the bits you understand getting bigger. For example, you now know the difference between the String parameter for this method and the String parameter for this one, and also why the two parameters have different names.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!