can someone please take a look at this(pretty basic)

V

vamp4l

basically, its a program that displays an input window and the user
enters numbers(i.e. 21 3 45 2) on the line as a string and im trying to
parse that string and pass each integer to a vector...heres the method
im trying to use...

String numbers = JOptionPane.showInputDialog(null,"Enter the
integers", "Integers", JOptionPane.PLAIN_MESSAGE);
String n[] = new String[numbers.length()];
n[0] = numbers.substring(0, numbers.indexOf(' ',0));
int len = n[0].length();

for(int j = 1; j < (numbers.length()-j); j++)
{
n[j] = numbers.substring(len+j, numbers.indexOf(' ',len+j));
vect.add(Integer.parseInt(n[j]));
len += n[j].length();
}

Ive spent hours trying to manipulate this some way to work correctly ,
it works for every integer but the very last one, where I always get a
string index out of bounds exception for the line
n[j] = numbers.substring(len+j, numbers.indexOf(' ',len+j));

any suggestions? thanks!
 
R

R.F. Pels

vamp4l said:
Ive spent hours trying to manipulate this some way to work correctly ,
it works for every integer but the very last one, where I always get a
string index out of bounds exception for the line
n[j] = numbers.substring(len+j, numbers.indexOf(' ',len+j));

Use StringTokenizer.
 
T

Thomas Hawtin

vamp4l said:
basically, its a program that displays an input window and the user
enters numbers(i.e. 21 3 45 2) on the line as a string and im trying to
parse that string and pass each integer to a vector...heres the method
im trying to use...

String numbers = JOptionPane.showInputDialog(null,"Enter the
integers", "Integers", JOptionPane.PLAIN_MESSAGE);
String n[] = new String[numbers.length()];
n[0] = numbers.substring(0, numbers.indexOf(' ',0));
int len = n[0].length();

for(int j = 1; j < (numbers.length()-j); j++)
{
n[j] = numbers.substring(len+j, numbers.indexOf(' ',len+j));
vect.add(Integer.parseInt(n[j]));
len += n[j].length();
}

Ive spent hours trying to manipulate this some way to work correctly ,
it works for every integer but the very last one, where I always get a
string index out of bounds exception for the line
n[j] = numbers.substring(len+j, numbers.indexOf(' ',len+j));

For the last number what do you expect the indexes passed to substring
to be? What are they? You would want it to look something like:

"21 3 45 2"
^ ^

You are currently using String.indexOf(' ', ...) to find the last index,
but there isn't a space there.

There's no need for n to be an array, as you only ever use one entry at
a time. You might be able to think of a better name for it too.

String.split is easier.

Tom Hawtin
 
J

jan V

Ive spent hours trying to manipulate this some way to work correctly ,

If you refuse to use StringTokenizer... then spend another few hours
struggling, and *then* look at what StringTokenizer can do for you...
painful lessons are good teachers. :)
 
R

Roedy Green

21 3 45 2)

Here are some ways to split that.:

By far the easiest is a regex split see
http://mindprod.com/jgloss/regex.

A StringTokenizer on the line would do it too.
see http://mindprod.com/jgloss/stringtokenizer.html

A loop where you scanned for space chars and extracted substrings
would be fastest, but overly complicated, especially dealing with
multiple space chars in a row.

One of you have your separate strings, see
http://mindprod.com/applets/converter.html
for how to convert them into ints
 
V

vamp4l

thanks for the suggestions...i used the tokenizer to get the number of
integers and did the loop like i was trying to do before without the
array and just did the loop for the number of integers minus the last
one and got the last one separately
 
R

Roedy Green

thanks for the suggestions...i used the tokenizer to get the number of
integers and did the loop like i was trying to do before without the
array and just did the loop for the number of integers minus the last
one and got the last one separately


When you end up doing extraordinary things to handle the first or
last element it is a sign your algorithm needs some adjustment so they
come out in the wash, or with just a tiny fillip.

A vanilla StringTokenizer out the box will do what you want without
even giving it custom delimiters.

The tokenizer uses the default delimiter set, which is " \t\n\r\f":
the space character, the tab character, the newline character, the
carriage-return character, and the form-feed character. Delimiter
characters themselves will not be treated as tokens.

Check out the sample code I gave you earlier at
http://mindprod.com/jgloss/stringtokenizer.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top