title: How would I get only IPv4 addresses
body: <p>I have the following code which is supposed to get only the IPv4 addresses of all active interfaces, but it still returns an IPv6 address on some computers.</p>
  
  <pre><code>public static List&lt;List&gt; getIpAddress() {
      List&lt;String&gt; ip = new ArrayList&lt;&gt;();
      List&lt;List&gt; ipRefined = new ArrayList&lt;&gt;();
      try {
          Enumeration&lt;NetworkInterface&gt; interfaces = NetworkInterface.getNetworkInterfaces();
          while (interfaces.hasMoreElements()) {
              NetworkInterface iface = interfaces.nextElement();
              if (iface.isLoopback() || !iface.isUp())
                  continue;
              Enumeration&lt;InetAddress&gt; addresses = iface.getInetAddresses();
              while(addresses.hasMoreElements()) {
                  ip.add(addresses.nextElement().getHostAddress());
              }
          }
      } catch (SocketException e) {
          throw new RuntimeException(e);
      }
      for(int x = 0; x &lt; ip.size(); x++){
          if(ip.get(x).contains("%")){
              try {
                  if (ip.get(x + 1).contains(".")) {
                      List&lt;String&gt; tempList = new ArrayList&lt;&gt;();
                      tempList.add(ip.get(x).substring(ip.get(x).indexOf("%") + 1));
                      tempList.add(ip.get(x + 1));
                      ipRefined.add(tempList);
                  }
              } catch (IndexOutOfBoundsException ae) {
              }
          }
      }
      return ipRefined;
  }
  </code></pre>
  
  <p>I've tried to specify using only IPv4 by using <code>System.setProperty("java.net.preferIPv4Stack" , "true");</code>, but this only causes <code>getIpAddress()</code> to return an empty list. How should I be getting the IPv4 of active interfaces without the use of string manipulation?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Using <code>System.setProperty("java.net.preferIPv4Stack" , "true");</code> always causes <code>getIpAddress()</code> to return an empty list.</p>