Array Question

Discussion in 'Java' started by Asad Kazmi, Jun 27, 2004.

  1. Asad Kazmi

    Asad Kazmi Guest

    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);
    }
    }
     
    Asad Kazmi, Jun 27, 2004
    #1
    1. Advertising

  2. Asad Kazmi

    Superdude Guest

    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);
    }



    > 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);
    > }
    > }
    >
    >
    >
     
    Superdude, Jun 27, 2004
    #2
    1. Advertising

  3. Asad Kazmi

    Superdude Guest

    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.
     
    Superdude, Jun 27, 2004
    #3
  4. Asad Kazmi

    Roedy Green Guest

    On Sat, 26 Jun 2004 19:59:05 -0600, Superdude
    <> wrote or quoted :

    >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);
    > }

    or in one line with Regex.split.

    See http://mindprod.com/jgloss/regex.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 27, 2004
    #4
  5. Asad Kazmi

    Asad Kazmi Guest

    Thankyou superdude :)
    Regards
    Asad kazmi

    "Superdude" <> wrote in message
    news:...
    > Roedy Green wrote:
    >
    > > On Sat, 26 Jun 2004 19:59:05 -0600, Superdude
    > > <> wrote or quoted :
    > >
    > >
    > >>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);
    > >> }

    > >
    > > or in one line with Regex.split.
    > >
    > > See http://mindprod.com/jgloss/regex.html
    > >

    >
    > True.
     
    Asad Kazmi, Jun 27, 2004
    #5
  6. Asad Kazmi

    Superdude Guest

    Roedy Green wrote:

    > On Sat, 26 Jun 2004 19:59:05 -0600, Superdude
    > <> wrote or quoted :
    >
    >
    >>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);
    >> }

    >
    > or in one line with Regex.split.
    >
    > See http://mindprod.com/jgloss/regex.html
    >


    True.
     
    Superdude, Jun 27, 2004
    #6
  7. Asad Kazmi wrote:
    > 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
     
    Stefan Waldmann, Jun 28, 2004
    #7
  8. Asad Kazmi

    Asad Kazmi Guest

    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" <> wrote in message
    news:...
    > 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.
     
    Asad Kazmi, Jun 28, 2004
    #8
  9. Asad Kazmi

    Roedy Green Guest

    On Mon, 28 Jun 2004 18:46:22 +0500, "Asad Kazmi" <>
    wrote or quoted :

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


    What code are you referring to?
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 28, 2004
    #9
  10. Asad Kazmi

    Asad Kazmi Guest

    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;
    }


    "Roedy Green" <> wrote in message
    news:...
    > On Mon, 28 Jun 2004 18:46:22 +0500, "Asad Kazmi" <>
    > wrote or quoted :
    >
    > >When i complie with your codes it displays this error message.

    >
    > What code are you referring to?
    > --
    > Canadian Mind Products, Roedy Green.
    > Coaching, problem solving, economical contract programming.
    > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Asad Kazmi, Jun 28, 2004
    #10
  11. Asad Kazmi

    Roedy Green Guest

    On Mon, 28 Jun 2004 22:42:27 +0500, "Asad Kazmi" <>
    wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 29, 2004
    #11
  12. Asad Kazmi

    Superdude Guest

    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.
     
    Superdude, Jun 30, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joseph Turian
    Replies:
    11
    Views:
    527
    Joseph Turian
    Jan 19, 2005
  2. Fabio
    Replies:
    0
    Views:
    317
    Fabio
    Nov 5, 2008
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    505
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    219
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    494
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page