# Thread: Taking specific values form Array

1. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Taking specific values form Array

Hi,

I need to take the values from following indexes in array:

2 and 3, 6 and 7, 9 and 10... and so on...

How would I go about doing that?

2. ## Re: Taking specific values form Array

Use a loop and change the array index so it selects the elements that you want.

3. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Re: Taking specific values form Array

Let me ask it another way: how do I change the array index in the loop so it will follow the pattern that I want?

4. ## Re: Taking specific values form Array

Start by defining the pattern, in mathematical terms.

db

5. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Re: Taking specific values form Array

I really cannot think of anything sensible. The sample output of the array:

Java Code:
```91652826
1000165
text one
text two
1056696394
1000066
text three
text four```
Maybe I could somehow determine wheter the value is an integer or string. If string, pull it out and put into separate array. But the original array is a String[]... Is there any way I could make such check?

Basically I want to get rid of all the indexes with integers, so only strings are left.

6. ## Re: Taking specific values form Array

Is your question whether a String contains all decimal digits?
A way to check would be to use the Long class's parseLong() method in a try{}catch block. If there isn't an exception, the String would be all decimal digits.
If there are too many digits for a long, you will have to write a loop that tests each character.

7. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Re: Taking specific values form Array

Yes, I meant whether there is a way to check if the String consists only of decimal digits.

Here is what I wrote:

Java Code:
```        try {

for (int i = 0; i < array.length; i++) {
Integer.parseInt(array[i]);
}

} catch (Exception e) {
// There are letters in a string
}```
How can I get the value that caused exception and store in in separate array?

8. ## Re: Taking specific values form Array

Put the try{} catch inside of the loop.
The String you are working with will be array[i] when you get the exception.

9. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Re: Taking specific values form Array

Thanks!

Pretty messy, but works:

Java Code:
```        String[] array = checkCharacters();
String[] newArray;
newArray = new String[array.length];
ArrayList<String> resultArray = new ArrayList<String>();
String[] finalArray;

for (int i = 0; i < array.length; i++) {
try {
Integer.parseInt(array[i]);
} catch (Exception e) {
newArray[i] = array[i];
}
}

for (int i = 0; i < newArray.length; i++) {
if (newArray[i] == null) {
continue;
} else {
}
}

finalArray = new String[resultArray.size()];

for (int i = 0; i < finalArray.length; i++) {
finalArray[i] = resultArray.get(i);
}

return finalArray;```

10. ## Re: Taking specific values form Array

Instead of using the same index: i
newArray[i] = array[i];
Have a separate index for newArray that you increment as you add items to it. When done adding the index would have the number of elements that were copied to newArray.

11. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Re: Taking specific values form Array

Because I don't know with how many items I will end up in resultArray.

Last edited by Eleeist; 02-06-2012 at 08:18 PM.

12. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Re: Taking specific values form Array

I don't understand. I have to have four loops and four arrays:

1. For the "raw" data tha goes in the method
2. For the "processed" data that has all digit instances changed to null
3. For "cleaned" data, that has all null removed (in ArrayList)
4. To move the final, processed data to normal Array, as I don't like to work with ArrayList
Last edited by Eleeist; 02-06-2012 at 09:17 PM.

13. ## Re: Taking specific values form Array

What if you get rid of newArray and use resultArray to hold the Strings in loop 1?
That would remove need for the second loop.

What are you trying to put into finalArray? the decimal digits or the non decimal digits? Your comments don't say which you want to do.

The first loop can detect if an element in array is this or that. Think of an if statement. When you test a condition you can test if this is true or not and do one of two things.
The first loop can save the desired item based on the results of the parse method. You will then know if the element from array is to be saved or not. If you save it then, you can later copy it to the finalArray as in the last loop.
Last edited by Norm; 02-06-2012 at 09:22 PM.

14. ## Re: Taking specific values form Array

If the catch block is executed do you want to save that value or not?
Save it and use continue.
If the want to save the value that does not throw the exception, save it after the catch block before the end of the loop.

15. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Re: Taking specific values form Array

Uff, finally figured out what you meant :).

Java Code:
```        String[] array = checkCharacters();
ArrayList<String> resultArray = new ArrayList<String>();
String[] finalArray;

for (int i = 0; i < array.length; i++) {
try {
Integer.parseInt(array[i]);
} catch (Exception e) {
}
}

finalArray = new String[resultArray.size()];

for (int i = 0; i < finalArray.length; i++) {
finalArray[i] = resultArray.get(i);
}

return finalArray;```

16. ## Re: Taking specific values form Array

Some comments in the code would make it easier for others to understand what you are trying to do.
What is the purpose of the method? How is it going to do its job?

17. Member
Join Date
May 2011
Posts
84
Rep Power
0

## Re: Taking specific values form Array

Java Code:
```// An array is created -  full of values read from an XML file and
// returned by checkCharacters() method.
// The array looks like this:
//number
//number
//text
//text
//number
//number
//text
//text
//... and so on.
// I don't need the numbers, only text is important to me.

String[] array = checkCharacters();

// More arrays declared. More on them later in comments

ArrayList<String> resultArray = new ArrayList<String>();
String[] finalArray;

// Iterate through the array[]

for (int i = 0; i < array.length; i++) {
try {

// Check whether value encountered is number or string

Integer.parseInt(array[i]);
} catch (Exception e) {

// If string, catch exception and assign this string value
// to ArrayList resultArray. I chose ArrayList because I don't
// know how many results it will need to hold

}
}

finalArray = new String[resultArray.size()];

// Change the ArrayList into normal array - I don't want to deal
// with ArrayList anymore.

for (int i = 0; i < finalArray.length; i++) {
finalArray[i] = resultArray.get(i);
}

// Return the final, processed data in an array.

return finalArray;```
I hope it is clear now.

18. ## Re: Taking specific values form Array

Programming by Exception is generally frowned on. Why not use String#matches("\\d*") to determine that the String consists of decimal digit characters?

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•