Results 1 to 6 of 6
  1. #1
    eohalloran is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Filtering returned results

    Hi,

    I have added a small piece of code that goes out and discovers what ethernet ports are available on a PC and returns them to the calling function in a GUI that populates a Combo Box. The code is as follows:

    Java Code:
    import java.net.*;
    import java.net.SocketException;
    import java.util.*;
    
    
    public class ListNets
    {
           String tmp2[];
           public static String[] ListEthPorts ()
           {
              String tmp[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
              int i = 0;
              try
              {
                 Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
                 for (NetworkInterface netint : Collections.list(nets))
                 {
                    tmp[i] = netint.getName();
                    i++;
                 }
    
              }
              catch(SocketException exp)
              {
    
              }
              return tmp;
        }
    }
    This works great and does actually populate the combo box with the correct interface names. What I want to do now is apply a filter based on a regular expression that only returns interface names of a specific type such as eth1.34, eth1.35 etc.... Currently it returns all interfaces such as l0, vif 1.1.

    The regular expression I want to apply is:

    Java Code:
    "(eth[0-9]\\.[0-9]{2})|(eth[0-9])|(eth[0-9]{2})|(eth[0-9]{2}\\.[0-9])|(eth[0-9]{2}\\.[0-9]{2})"
    Is there a neat way of doing this just before tmp is returned? I have tried some examples that are out there but they have not worked for me. Also is there a neater way to initialize the 12 ports instead of the way I have done it with the String tmp[]?

    Thanks

    Eric

    BTW, I am a complete newb at Java, so forgive my ignorance in advance.
    Last edited by eohalloran; 01-24-2012 at 12:14 AM. Reason: Clarification that Iam new to Java

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,610
    Rep Power
    23

    Default Re: Filtering returned results

    What contents do you need in the tmp array? The code replaces the first n values.

  3. #3
    eohalloran is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Filtering returned results

    Hi Norm,

    Each PC I am running the GUI on has a combination of real, virtual and sub-interfaces. I have VLAN trunking setup and the GUI I want to run only on the sub-interfaces that connect to the VLan'd switch. Basically there are 12 interfaces and that code returns the following for example on one PC:

    "vif0, peth0, eth1, eth1.35, eth1.34, eth1.33, eth1.32, eth1.31, eth1.30, eth0, virbr0, l0"

    I only want to return "eth1.35, eth1.34, eth1.33, eth1.32, eth1.31, eth1.30"

    The regular expression I constructed above will work but I can't figure how to apply it. I can get the required results by indexing the results but that means if I add or remove sub-interfaces, I have to alter the code. The best way is to filter using the regular expression.

    Thanks

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,610
    Rep Power
    23

    Default Re: Filtering returned results

    Is there a forum for regular expressions?

  5. #5
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Filtering returned results

    Take a look here for regular expressions in java. Basically, i think you'd just want to throw an if statement around where you assign the name to the string array and check the string with the .matches() function.

    Java Code:
    if (netint.getName().matches(".*")) {
      ...add to array...
    }
    Also, instead of using a string array whose length has to be defined, i would suggest looking at an ArrayList. ArrayLists are dynamic so you can add as many values as you want whenever you need to.

  6. #6
    eohalloran is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Filtering returned results

    Shoss,

    Thanks for the pointer to ArrayLists. I applied the following code and it works as expected.

    Java Code:
    import java.lang.String.*;
    import java.net.*;
    import java.net.SocketException;
    import java.util.*;
    import java.util.ArrayList.*;
    import java.util.regex.Pattern;
    
    
    public class ListNets
    {
           public static String[] ListEthPorts ()
           {
              String [] tmp = null;
              String ifName = null;
              ArrayList<String> ifList = new ArrayList<String>();
              try
              {
                 Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
                 for (NetworkInterface netint : Collections.list(nets))
                 {
                    ifName = netint.getName();
                    if(Pattern.matches("(eth[0-9]\\.[0-9]{2})|(eth[0-9]{2}\\.[0-9])|(eth[0-9]{2}\\.[0-9]{2})", ifName) == true)
                    {
                       ifList.add(ifName);
                    }
                 }
    
    	     int numValidIf = ifList.size();
                 tmp = new String[numValidIf];
    
                 for(int i = 0; i < numValidIf; i++)
                 {
                    tmp[i] = ifList.get(i);
                 }
              }
    
              catch(SocketException exp)
              {
    
              }
              return tmp;
        }
    }
    Eric

Similar Threads

  1. Filtering an array
    By counterfox in forum New To Java
    Replies: 7
    Last Post: 01-19-2012, 03:47 AM
  2. Filtering strings?
    By Gradden in forum New To Java
    Replies: 1
    Last Post: 09-26-2011, 04:18 AM
  3. Replies: 5
    Last Post: 09-17-2011, 01:35 AM
  4. JLists - Filtering
    By Psyclone in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2010, 06:09 AM
  5. looping and filtering
    By javafanatic in forum New To Java
    Replies: 14
    Last Post: 02-09-2010, 09:48 AM

Posting Permissions

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