-1

So, please bear with me as I have a long question here, I have some code in java that is using an array list to implement a stack. I need to be able to enter the command "push" to add stuff to the stack. However my problem is that it has to be in the format pushSTUFF.

Where the "STUFF" is anything, upper case, lower case, string, int, etc.. The way I've been trying to implement this is with the string split method where PUSH is the delimiter. Then the command is passed to a switch case.

I quickly realized that the split gets discarded, at least as far as I can tell, and that the switch case is getting pushSTUFF not push as the case input.

In contemplating this problem I came up with a couple of ways I could do this. I just don't know if they are possible or how to do them.

So,

  1. Is there a way to split a string like pushSTUFF and keep both parts (the push and the STUFF)

  2. Is there a way to split, from a string, something of unknown length or contents (since I don't know what the user will input the STUFF is unknown)

  3. Is there a way to tell the switch case to look for the pushSTUFF as opposed to just push (again because STUFF is unknown).

Are any of these even possible to do? If so what would you recommend?

I'm sure there are better ways but as I'm still learning java these seemed like the best for right now. Also I didn't post any code because I didn't feel it was necessary to the question. I will post some if you need it though. Just ask and I will be happy to oblige.

(tl;dr) Is it possible to do any of 1, 2, or 3 above and if so how? Thanks in advance.

user2009481
  • 5
  • 1
  • 3
  • FYI you're getting downvotes because there are dozens, if not hundreds, of answered questions regarding splitting strings in Java on this site. Take a minute to Google your problem, or look at the list of Related questions in the right hand column. Almost certainly someone else has had your exact problem before. – dimo414 Mar 06 '13 at 23:28
  • I've been googleing this problem for the past hour and a half at least. I wouldn't come here for help if I could find something on my own. I like to teach myself when possible. – user2009481 Mar 06 '13 at 23:35

4 Answers4

1

Instead of splitting the strings, you can use regular expressions with groups and iterate over the matching parts of it (as you saw, the split character(s) get discarded).

For #1, you could do something like (pseudocode):

   regex = (push)(.*)
   stuff = groups[1]

That should also cover #2 since it will match all characters after the push.

I'm not entirely sure what you're asking in #3.

There is a regex tutorial here if you're not familiar with java regular expressions.

You can also take a look at the StringTokenizer, which has an option to keep delimiters.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
1

If the format will always be push*SOMETHING* why aren't you using String.substring()?

You can do:

String something = "pushSTUFF".substring(4);

This way, you will always get whatever is behind push.

I really don't understand what you are trying to achieve without seeing the actual code, but your problem seems simple enough to be solved this way.

0

You can search for any Uppercase letter and use String.substring(...)

Find if first character in a string is upper case, Java

Community
  • 1
  • 1
Wolfii
  • 355
  • 3
  • 9
0

Use .indexOf and find push:

public class splitstring {
 public static void main(String[] args){
  String tosplit, part1, part2 = new String();
  int ind = 0;
  tosplit = "push1234";
  ind = tosplit.indexOf("push");
  part1 = tosplit.substring(ind,ind + 4);
  part2 = tosplit.substring(ind + 4, tosplit.length());
 }
}