Here is a complete program taking your logic (after fixing the couple of
typos).  It works fine; I ran it from the command line with arguments Cat
Chat Chen and Cat Chat Fen, and in both cases it worked.  I believe your
error is somewhere else, not in the code you sent us.

import java.util.*;

public class Shirley
{

   public static void main(String[] args)
   {
      Vector myVector = new Vector();

      // load the vector with the command line args
      for (int x = 0; x < args.length; ++x)
      {
         myVector.add(args[x]);
      }

      String[] array1 = new String[myVector.size()];
      String tmpString = "";
      int j=0;

      for (int i=0; i<myVector.size(); i++)
      {
         tmpString = (String)myVector.get(i);
         if (tmpString.indexOf('C') != -1)
         {
           array1[j] = tmpString;
           j++;
         }
      }

      String[] array2 = new String[j];
      for (int i=0; i<j; i++)
      {
         array2[i] = array1[i];
      }

      // print out the contents of the second array
      for (int y = 0; y < array2.length; ++y)
      {
         System.out.println(array2[y]);
      }
   }
}

----- Original Message -----
From: Shirley Chen <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, December 20, 2000 2:39 PM
Subject: arrays


> Hi,
>
> I have a question, somebody please help me:
>
> I have a vector which will keep all the names the user typed in.  Then the
> user supposes can do a query like "give me all the name which starts with
> 'C'".  In order to do that, I do something like this:
>
> ...
> String[] array1 = new String[myVector.size()];
> String tempString = "";
> int j=0;
>
> for (int i=0; i<myVector.size(); i++)
> {
>    tmpString = (String)myVector.get(i);
>    if (tmpString.indexOf('C') != -1)
>       {
>         array1[j] = tmpString;
>         j++;
>       }
> }
>
> file://since array1 may not be filled up with data, I transfer it to
another
> String array so I know how many element in the array
>
> String array2 = new String[j];
> for (int i=0; i<j; i++)
> {
>    array2[i] = array1[i];
> }
>
> // then I will do display. Supposedly, say, array2.length is 20, if the
user
> wants to display first 5, I will display only the first 5 records, if the
> user wants to display 30, and I will display 20 instead.
> ...
>
> I can display all the situations ok except when myVector contains the
names
> that all start with "C" (which means array2.length should be
> myVector.size()). then the program will crash.  I think it might be array
> out of bound problem, but I don't know how to fix it. Please help me.
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to