Array Question

A

Asad Kazmi

Hi,

I am extracting my String (mytext) but it only repitedly displays "My" 6
times. While I want to extract all character on new line one by one.Like,

My
Name
Is
Asad
Raza
Kazmi

So can some one tell me where is the problem ?

Regards
Asad Kazmi
******************************************


public static void main(String[] args) {
String mytext="My Name is Asad Raza Kazmi";
int count=0;
int index=0;
char seperator=' '; //Determines the no of substrings
do
{
++count;
++index;
index=mytext.indexOf(seperator,index);
}
while(index != -1);
// Extract the substring into an array

String[] substr = new String[count]; // Allocate for Substring
index=0; // Substring Start index
int endindex = 0; // Substring endindex
for(int i=0; i<count; i++)
{
endindex=mytext.indexOf(seperator,index); //Find next
seperator
if(endindex = =1)
substr=mytext.substring(index);
else
substr=mytext.substring(index,endindex);
}
index=endindex + 1;

// Display the Substring
for(int i=0; i < substr.length; i++)
System.out.println(substr);
}
}
 
S

Superdude

Asad Kazmi wrote:

Whew that is a long way to do it...

How come you couldn't do this?

String mytext="My Name is Asad Raza Kazmi";
StringTokenizer tokenizer = new StringTokenizer(mytext);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
System.out.println(token);
}
 
S

Superdude

But if you want to change it from

if(endindex = =1)
substr=mytext.substring(index);
else
substr=mytext.substring(index,endindex);
}
index=endindex + 1;


to

if(endindex = =1)
substr=mytext.substring(index);
else
substr=mytext.substring(index,endindex);
index=endindex + 1;
}

That'll work too.
 
S

Stefan Waldmann

Asad said:
Hi,

I am extracting my String (mytext) but it only repitedly displays "My" 6
times. While I want to extract all character on new line one by one.Like,

My
Name
Is
Asad
Raza
Kazmi

Hi,

the following code does just the thing you intend, and is a little bit
shorter ;-)
(works with Java 1.4 and higher)

String mytext="My Name is Asad Raza Kazmi";

// what you want to do, in 1 line:
String[] substr = mytext.split(" ");

// Display the Substring
for(int i=0; i < substr.length; i++)
System.out.println(substr);


Regards,
Stefan
 
A

Asad Kazmi

Hi, Roedy

When i complie with your codes it displays this error message.

java.lang.StringIndexOutOfBoundsException: String index out of range: -22
at java.lang.String.substring(String.java:1444)
at asad.SearchText.main(SearchText.java:45)
Exception in thread "main"

And which class in included for StringTokenizer ?

Superdude said:
But if you want to change it from

*************************
Mine Codes
*************************
if(endindex = =1)
substr=mytext.substring(index);
else
substr=mytext.substring(index,endindex);
}
index=endindex + 1;


to

************************
Yours Codes
************************
if(endindex = =1)
substr=mytext.substring(index);
else
substr=mytext.substring(index,endindex);
index=endindex + 1;
}

That'll work too.
 
A

Asad Kazmi

Hi,
you ammended this part and put index=endindex+1 inside closing braces while
i put this statement out of braces. so i was refering this as your code.

Regards
Asad

if(endindex = =1)
substr=mytext.substring(index);
else
substr=mytext.substring(index,endindex);
index=endindex + 1;
}
 
R

Roedy Green

you ammended this part and put index=endindex+1 inside closing braces while
i put this statement out of braces. so i was refering this as your code.

Regards
Asad

I see the problem. Superdude made that mod, not me.
 
S

Superdude

Yep,

public static void main(String[] args) {
String mytext="My Name is Asad Raza Kazmi";

int count=0;
int index=0;
char seperator=' '; //Determines the no of substrings
do {
++count;
++index;
index=mytext.indexOf(seperator,index);
}
while(index != -1);
// Extract the substring into an array

String[] substr = new String[count]; // Allocate for Substring
index=0; // Substring Start index
int endindex = 0; // Substring endindex
for(int i=0; i<count; i++) {
endindex=mytext.indexOf(seperator,index); //Find next
//seperator
if(endindex == -1)
substr=mytext.substring(index);
else
substr=mytext.substring(index,endindex);
index=endindex + 1;
}


// Display the Substring
for(int i=0; i < substr.length; i++)
System.out.println(substr);
}


Worked fine.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top