• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing a while loop variable outside the loop  RSS feed

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am retrieving some values from the database and store it in a string variable like the following:


In the above code ,i can able to retrieve all the records whose flag is 0 in a string variable extnum.but if i try to access the variable outside the loop,the last record only gets printed.please assist me to access all the records stored in a variable after the while loop ends.

Thanks
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the variable "extnum"is getting overwritten you can add the variables in an ArrayList and can do whatever you want.
Sample is below.

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.It helps me.Is it possible to take the value before the last value from the array?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A List has methods which tell you how many elements it contains.
A List has a method which returns the n-th element.
A List has an element numbered 0 if it contains 1 element, 0 and 1 if it contains 2 elements, etc.

So the answer to your question is "yes, you can easily retrieve the last-but-one element." Remember you can only retrieve the last-but-one element from a List containing 2 or more elements.

It would be better to declare your List as

List<String> allValues = new ArrayList<String>();
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.my list contains more than one element like

The above code prints me the out put:

Channel
1
0

Here,how can i just get the value 1 from the above three.If you provide me any sample code,it will be helpful.

Thanks
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.Actually,the code which i have mentioned later does not gets the values from the database.I am pasting here the entire code:


In the above code,i am getting the output of the command "show channels"and saved it in the variable line.Then splitted only the channels and saved it in the variable channel.then i have added the channel values in allValues.now how can i get the last but one element?

Thanks
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the method to get the total number of elements in the List. pretend that there are 5.

if you want the last element-but one, you want the 4th element.

since the elements are 0-based indexed, that means you have elements 0, 1, 2, 3, and 4. So the last-but-one element would be at index 3.

Basically, find the number of elements, and assuming it's greater than 1, you want the element at index size - 2.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!