Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Taking specific values form Array

  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: Taking specific values form Array

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

  3. #3
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Taking specific values form Array

    Start by defining the pattern, in mathematical terms.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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. #7
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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. #9
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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;

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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?

  11. #11
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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.

  12. #12
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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 08:17 PM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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. #15
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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;

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default 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. #17
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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
                    
                    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;
    I hope it is clear now.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    Norm likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 22
    Last Post: 02-05-2012, 10:46 PM
  2. Taking ScreenShots Of A Specific Window
    By anthropamorphic in forum Advanced Java
    Replies: 7
    Last Post: 08-15-2011, 02:37 AM
  3. Replies: 1
    Last Post: 04-03-2009, 07:17 AM
  4. Replies: 0
    Last Post: 02-28-2009, 11:16 AM
  5. Open specific folder form JSP
    By roseline43 in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 07:53 PM

Posting Permissions

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