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?
Printable View
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?
Use a loop and change the array index so it selects the elements that you want.
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?
Start by defining the pattern, in mathematical terms.
db
I really cannot think of anything sensible. The sample output of the array:
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?Code:91652826
1000165
text one
text two
1056696394
1000066
text three
text four
Basically I want to get rid of all the indexes with integers, so only strings are left.
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.
Yes, I meant whether there is a way to check if the String consists only of decimal digits.
Here is what I wrote:
How can I get the value that caused exception and store in in separate array?Code:try {
for (int i = 0; i < array.length; i++) {
Integer.parseInt(array[i]);
}
} catch (Exception e) {
// There are letters in a string
}
Put the try{} catch inside of the loop.
The String you are working with will be array[i] when you get the exception.
Thanks!
Pretty messy, but works:
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 {
resultArray.add(newArray[i]);
}
}
finalArray = new String[resultArray.size()];
for (int i = 0; i < finalArray.length; i++) {
finalArray[i] = resultArray.get(i);
}
return finalArray;
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.
Why not add directly to resultArray instead of using newArray?
Because I don't know with how many items I will end up in resultArray.
EDIT: Sorry, did not finish reading your post. Good idea :).
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
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.
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.
Uff, finally figured out what you meant :).
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) {
resultArray.add(array[i]);
}
}
finalArray = new String[resultArray.size()];
for (int i = 0; i < finalArray.length; i++) {
finalArray[i] = resultArray.get(i);
}
return finalArray;
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?
I hope it is clear now.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
resultArray.add(array[i]);
}
}
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;
Programming by Exception is generally frowned on. Why not use String#matches("\\d*") to determine that the String consists of decimal digit characters?
db