Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:48:03+00:00 2026-06-09T17:48:03+00:00

My program gets as input parameter a String containing a list of IP Addresses.

  • 0

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?)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:48:05+00:00Added an answer on June 9, 2026 at 5:48 pm

    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);
        }
      }
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

as input my program gets a String containing IP Addresses are separated by a
I write a program that gets as an input any string that contain the
I have a program which gets commands as a string. Each character in the
I am trying to write a program which gets input files and prints included
I'm developing a program which allows users to input some information which then gets
I've wrote a program that gets a string, and then calculate a total value
I've written a small assember program that gets and input file, an output file
I have a setup where a program gets its input like so: 1) user
I am using libcurl for a small program that gets data from an input
I'm trying to make a program that gets two input numbers, multiplies them (storing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.