Results 1 to 18 of 18
- 02-05-2012, 09:30 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
- 02-05-2012, 09:50 PM #2
Re: Taking specific values form Array
Use a loop and change the array index so it selects the elements that you want.
- 02-05-2012, 09:53 PM #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?
- 02-05-2012, 10:09 PM #4
Re: Taking specific values form Array
Start by defining the pattern, in mathematical terms.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-06-2012, 06:09 PM #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:
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?Java 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.
- 02-06-2012, 06:13 PM #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.
- 02-06-2012, 06:20 PM #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:
How can I get the value that caused exception and store in in separate array?Java Code:try { for (int i = 0; i < array.length; i++) { Integer.parseInt(array[i]); } } catch (Exception e) { // There are letters in a string }
- 02-06-2012, 06:26 PM #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.
- 02-06-2012, 06:52 PM #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 { resultArray.add(newArray[i]); } } finalArray = new String[resultArray.size()]; for (int i = 0; i < finalArray.length; i++) { finalArray[i] = resultArray.get(i); } return finalArray;
- 02-06-2012, 07:09 PM #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.
Why not add directly to resultArray instead of using newArray?
- 02-06-2012, 07:13 PM #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.
EDIT: Sorry, did not finish reading your post. Good idea :).Last edited by Eleeist; 02-06-2012 at 07:18 PM.
- 02-06-2012, 08:15 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 ArrayListLast edited by Eleeist; 02-06-2012 at 08:17 PM.
- 02-06-2012, 08:18 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 08:22 PM.
- 02-06-2012, 08:36 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.
- 02-06-2012, 08:37 PM #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) { resultArray.add(array[i]); } } finalArray = new String[resultArray.size()]; for (int i = 0; i < finalArray.length; i++) { finalArray[i] = resultArray.get(i); } return finalArray;
- 02-06-2012, 08:42 PM #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?
- 02-06-2012, 09:09 PM #17
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Taking specific values form Array
I hope it is clear now.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 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;
- 02-06-2012, 09:10 PM #18
Similar Threads
-
Taking values out of one array and putting them into another.
By Wnt2bsleepin in forum New To JavaReplies: 22Last Post: 02-05-2012, 10:46 PM -
Taking ScreenShots Of A Specific Window
By anthropamorphic in forum Advanced JavaReplies: 7Last Post: 08-15-2011, 02:37 AM -
Displaying (scrolling To) A Specific Record In A Continuous Form
By REVANSIDDHA in forum Advanced JavaReplies: 1Last Post: 04-03-2009, 07:17 AM -
how to insert data in table (stored proc) without taking all the values as parameters
By Faheem_Ahmed in forum New To JavaReplies: 0Last Post: 02-28-2009, 11:16 AM -
Open specific folder form JSP
By roseline43 in forum New To JavaReplies: 2Last Post: 09-02-2008, 07:53 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks