?
Solved

How do i count the number of elements in an array?

Posted on 2003-03-06
4
Medium Priority
?
236 Views
Last Modified: 2010-04-01
I am splitting the string using StringTokenizer. I also want to find the top and the bottom element of an array.

String mCust = request.getQueryString("cust");
StringTokenizer st = new StringTokenizer(mCust, ",");
  String[] tokens = new String[st.countTokens()];
  int i = 0;
  while(st.hasMoreElements())
     tokens[i++] = (String)st.nextElement();

---
Ramkumar
0
Comment
Question by:ramkumar_s1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
allahabad earned 80 total points
ID: 8086111
String mCust = Request.QueryString("cust");
StringTokenizer st = new StringTokenizer(mCust, ",");
    int totTokens = st.countTokens();
    String[] tokens = new String[totTokens];
    int i = 0;
    while(st.hasMoreElements()) {
      tokens[i++] = (String)st.nextElement();
    }
   System.out.println("Top token : " + tokens[0]);
   System.out.println("Bottom token : " + tokens[totTokens-1]);
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8088897
PLEASE DO NOT AWARD PTS TO ME SINCE A SOLUTION HAS ALREADY BEEN GIVEN.  I AM ONLY GIVING AN ALTERNATIVE.  Pts should goto allahabad.

that is a good way to get all and show the top and bottom.

but if you just want the top and bottom just store the first and last in two string vars rather than using an array

String mCust = Request.QueryString("cust");
StringTokenizer st = new StringTokenizer(mCust, ",");
   int totTokens = st.countTokens();
   String firstToken = null;
   String lastToken = null;
   int i = 0;
   while(st.hasMoreElements()) {
     if (i == 0) {
       firstToken = st.nextToken();
     }
     else if (i == (totTokens-1)) {
       lastToken = st.nextToken();
     }
     else {
       String dummyString = st.nextToken();
       dummyString = null;
     }
     i++;
   }
  System.out.println("Top token : " + firstToken);
  System.out.println("Bottom token : " + lastToken);

CJ
0
 
LVL 35

Expert Comment

by:girionis
ID: 10040977
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to allahabad

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question