Solved

scope of varibles & conversion

Posted on 2009-04-06
4
256 Views
Last Modified: 2012-05-06
Hi All,
I have a variable as shown below:
String curVal=request.getParameter("curVal");
// values are like -> only 4 (or)  only 4, 5 (or)  only  4,  5,  6
so now I want to store this values in array, so lets think values in "curVal -> 4,  5,  6"
 int items[] = new int[4];
 java.util.StringTokenizer st = null;
   if(curVal.length() > 0)
   {
      st = new java.util.StringTokenizer(curVal,",");
   }
        int j;
       for(j=0;j<4;j++)
       {
          while(st.hasMoreTokens())
             {
                  items[j]= Integer.parseInt(st.nextToken());
                  System.out.println("array :"+items[j]);
             }
       }
            // now I am expecting  4, 5,6,0 in array in the below statements, but I am getting 6,0,0,0 why?
            System.out.println("array 0:"+items[0]);
            System.out.println("array 1:"+items[1]);
            System.out.println("array 2:"+items[2]);
            System.out.println("array 3:"+items[3]);
%>

Thanks in advance.
0
Comment
Question by:prk_usa
[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
4 Comments
 
LVL 12

Expert Comment

by:basav_com
ID: 24079354
0
 
LVL 12

Expert Comment

by:basav_com
ID: 24079500
Here you messed up the code.

                   for(j=0;j<4;j++)
                   {
                      while(st.hasMoreTokens())
                         {
                              items[j]= Integer.parseInt(st.nextToken());
                              System.out.println("array :"+items[j]);
                         }
                   }
0
 
LVL 12

Accepted Solution

by:
basav_com earned 500 total points
ID: 24079548
Take a look at this example:

output:
array :4
array :5
array :6
array 0:4
array 1:5
array 2:6
array 3:0
public class SampleStringTest {
	public static void main(String args[]){
		//String curVal=request.getParameter("curVal");
		String curVal = "4,5,6";
        //		 values are like -> only 4 (or)  only 4, 5 (or)  only  4,  5,  6
		//so now I want to store this values in array, so lets think values in "curVal -> 4,  5,  6"
		 int items[] = new int[4];
		 java.util.StringTokenizer st = null;
		   if(curVal.length() > 0)
		   {
		      st = new java.util.StringTokenizer(curVal,",");
		   }
		        int j=0;
 
		          while(st.hasMoreTokens())
		             {
		                  items[j]= Integer.parseInt(st.nextToken());
		                  System.out.println("array :"+items[j]);
		                  j++;
		             }
		
		            // now I am expecting  4, 5,6,0 in array in the below statements, but I am getting 6,0,0,0 why?
		            System.out.println("array 0:"+items[0]);
		            System.out.println("array 1:"+items[1]);
		            System.out.println("array 2:"+items[2]);
		            System.out.println("array 3:"+items[3]); 
	}
}

Open in new window

0
 
LVL 12

Expert Comment

by:basav_com
ID: 24079601
Your code is messed up because you are looping again using the for loop.  You have alread used while(st.hasMoreTokens) to loop thru all the tokens and you actually stored everything in the
items[0] element only.
  while(st.hasMoreTokens())
                         {
                              items[j]= Integer.parseInt(st.nextToken());
                              System.out.println("array :"+items[j]);    ---------------> Print j also here. Then you will understand.
                                                     
                                              }

And then again looped the array again using for loop.


So look at my code, I simply removed the for loop and stored the elements in items[0] to items[2] using the while loop and by incrementing 'j' each time.

Ask me more if you don't understand the explanation :)
0

Featured Post

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Running Java Servlet inside Eclipse Luna 6 168
Clear browser cache on site login, is it possible? 3 34
'exit' is not valid keyword in java 14 163
servlet example 17 59
This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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