• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

problem with strings in Java...

String namesStr;
int position = 0;

namesStr = JOptionPane.showInputDialog("Enter names separated by commas");

position = namesStr.indexOf(",");
firstStr = namesStr.subString(0, position);

-------------------

The problem in my code is in the line:
firstStr = namesStr.subString(0, position);

What is wrong with the code? is it not assigning the string between position 0 and the position of the first comma to the variable firstStr?
 
Originally posted by: Stefan
String namesStr;
int position = 0;

namesStr = JOptionPane.showInputDialog("Enter names separated by commas");

position = namesStr.indexOf(",");
firstStr = namesStr.subString(0, position);

-------------------

The problem in my code is in the line:
firstStr = namesStr.subString(0, position);

What is wrong with the code? is it not assigning the string between position 0 and the position of the first comma to the variable firstStr?

I have no idea what you are talking about. What IS it doing? What are you trying to do?
 
I have no idea what you are talking about. What IS it doing? What are you trying to do?

I want the user to enter a string of names separated by commas (eg. name1, name2, name3) into a variable called namesStr, then assign the portion of that string that conatins "name1" to a variable called firstStr.

Originally posted by: gopunk
what's the error you get?

E:\My Documents\Java Assignments\A2Q3.java:15: cannot resolve symbol
symbol : method subString (int,int)
location: class java.lang.String
firstStr = namesStr.subString(0, position);
^
1 error

Tool completed with exit code 1
 
Originally posted by: FrogDog
Hmmm, I think I'd use a tokenizer if I was trying to do something like that.

Yeah I'd think a tokenizer would work better also.
Then you only need to use the tokenizer instead of finding the index of commas and making substrings, it would just do it all for you.
 
sounds like it can't find the method subString then in the String class... perhaps it is substring like the other guys aid
 
Originally posted by: cRazYdood
it's .substring(), not .subString()

caps matter

This is the correct answer. The other suggestions for using a StringTokenizer are also very valid. If all you're parsing on is the "," this would be a much better way of dealing with it.
 
Back
Top