Q:

How to solve this in java?

My program gets as input parameter a String containing a list of IP Addresses. Each IP address is separated by a line break. It can look like this:

10.1.1.1
2.2.2.2
11.1.1.1

it can look like this

10.1.1.1-20
1.1.1.1

but it can looki like this

172.16.12.1-20 /24
10.1.1.1

I want to check every IP address and return two Lists validAddresses, invalidAddresses.
I've already wrote a program that deals with the first the simplest type of input, i.e. no IP address ranges and no network masks. 
private String[] extractIPAddress(String address){
    String[] temp;
    temp = address.split("\\s+");
    return temp;
}

Then I do
addressList = extractIPAddress(String.valueOf(value));

for (int i=0; i < addressList.length; i++) {
    if (InetAddresses.isInetAddress(addressList[i]) == true) {
        validAddress = validAddress.concat(addressList[i] + '\n');
    } else {
        invalidAddress = invalidAddress.concat(addressList[i] + '\n');
    }
}

Now I'm pondering how to deal with the most complex type of input, esp.

when the line has a range attached to it 1.1.1.1-10, how to remove the -10 part in order check the main IP address; how to check whether range part -10 would make a valid IP address i.e. 1.1.1.10 and then how to put everything together, so I can return it as a line of the validAddress String, looking the same way as at the beginning, i.e. 1.1.1.1-10
same question applies to the network mask /24

What elements would this kind of program have? Could you outline it for me?
I thought I would do the following, but I'm not sure if that's the right way and how to implement some parts:

if I find a - then cut off the part starting at the position of - until end of line or "/" (how to do that?)
save that part into the ipRange variable
if I find / then cut off that part starting at the position of / until the end of the line
save that part into netMask variable 
copy the content into the tmp_ipRange = ipRange
remove the - in the tem_ipRange variable
replace the last octet of the main IP address with tmp_ipRange (how to do that?)
add the new IP address to the array created by the String.split() (impossible, because you can't just add something to an array in java? what alternative do I have? so I can't use split here?)
loop through the addressList (see above code) and check if the IP address is a valid IP address
after the validation add ipRange to the main ipAddress if ipRange is not null (how do I find the main ipAddress the ipRange belongs to?)
after the validation add netMaske to the main ipAddress (and range) if mainAddress is not null (how do I find the main ipAddress the netMask belongs to?)

A:

The parsing part could be done by a regular expression. Something like:

final Pattern p = Pattern.compile(
    "(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:-(\\d+))?(?:/(\\d+))?" );
for(String line : new String [] { "172.16.12.1-20/24",
                                  "172.16.12.1-20", 
                                  "172.16.12.1/24", 
                                  "172.16.12.1" })
{
    Matcher m = p.matcher(line);
    if (m.matches()) {
        String address = m.group(1);
        String rangePart = m.group(2); // is null if there is no range part
        String netmask = m.group(3); // is null if there is no netmask
        System.out.println(address + " - " + rangePart + " - " + netmask);
    }
}

Edit: If you need to deal with spaces, you can augment the regular expression by adding \\s*, for example:
"\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:\\s*-\\s*(\\d+))?(?:\\s*/\\s*(\\d+))?\\s*"

This way, you won't need to bother with spaces.
You could also create a similar regular expression for IPv6 addresses. It will be longer, of course, but the principle is the same.

A:

OK, I wrote the following code to tackle this task. The code is working as expected. Since this is one of my first java programs at all, I was wondering if you see any issues in this code?:
import java.util.*;
import java.lang.System;

import com.google.common.net.InetAddresses;

public class IPCheck{

  public static String[] extractLine(String line){
    String[] temp;
    temp = line.split("\\s+");

    return temp;
  }

  public static boolean hasNetMask(String line){
    boolean result = false;
    if(line.indexOf("/") != -1)
    result = true;

    return result;
  }

  public static boolean hasIPRange(String line){
    boolean result = false;
    if(line.indexOf("-") != -1)
    result = true;

    return result;
  }

  public static String extractNetMask(String line){
    String result = "";
    result = line.substring(line.indexOf("/"));

    return result;
  }

  public static String extractIPRange(String line){
    String result = "";
    result = line.substring(line.indexOf("-"));

    return result;
  }

  public static String chop(String line, String piece){
    String result = "";
    result = line.replace(piece, "");

    return result;
  }

  public static boolean validateIPRange(String ipRange){
    int tmpInt = 0;
    ipRange = chop(ipRange, "-");
    tmpInt = Integer.valueOf(ipRange);
    if(tmpInt > 255)
      return false;
    else
      return true;
  }

  public static void main(String args[]){
    String validIPAddress = "";
    String invalidIPAddress = "";
    String str = "10.1.1.1-300\n192.180.0.1-10/16\n192.168.0.1111";
    String[] addressList;
    String netMask = "";
    String ipRange = "";

    addressList = extractLine(str);
    for(int i=0; i<addressList.length; i++){

    if(hasNetMask(addressList[i]) == true){
      netMask = extractNetMask(addressList[i]);
      addressList[i] = chop(addressList[i], netMask);
    }

    if(hasIPRange(addressList[i]) == true){
      ipRange = extractIPRange(addressList[i]);
      addressList[i] = chop(addressList[i], ipRange);

       if(validateIPRange(ipRange) == false){
        /*if the IPRange is not valid, let's attach the ipRange
        to the current IP-Address to make it invalid*/
        addressList[i] = addressList[i].concat(ipRange);
        System.out.println( addressList[i]);
       }
    }

    if(InetAddresses.isInetAddress(addressList[i]) == true){
      validIPAddress = validIPAddress.concat(addressList[i] + ipRange + netMask);
    } else {
      invalidIPAddress = invalidIPAddress.concat(addressList[i] + ipRange + netMask);
    }
  }
 }
}

