2
\$\begingroup\$

I have started to learn Java for the past 20 days with no prior coding/comp science experience.

My brother gave me an idea to make a subnet calc/ping tool to enhance my knowledge and mentored through this.

Just wanted to know how can I upgrade this code and if I have used proper language idioms in this code. Any ideas to simplify/features for more learning would be appreciated.

import java.util.*;
import java.util.regex.Pattern;
import java.math.*;
import java.text.NumberFormat;
import java.text.ParseException;
import java.net.*;


public class example {

    public static void main(String[] args) {
        Scanner Scan = new Scanner(System.in);
        System.out.print("Enter IP: ");
        String IP;
        IP = Scan.nextLine();
        String Z = IP.split("\\/")[1];
        String Y = IP.split("\\/")[0];
        String[] parts = Y.split("\\.");
        String F1 = parts[0];
        String F2 = parts[1];
        String F3 = parts[2];
        String F4 = parts[3];
        int A = Integer.parseInt(F1);
        int B = Integer.parseInt(F2);
        int C = Integer.parseInt(F3);
        int E = Integer.parseInt(F4);

        {
            System.out.println(F1 + "." + F2 + "." + F3 + "." + F4);
        }
        String X = (IP.substring(IP.lastIndexOf("/") + 1)); {
            System.out.println(X);
        }
        int S = Integer.parseInt(X);
        double Bits = 32;
        double TotalBites = Bits - S;
        double I = Math.pow(2, TotalBites);
        int D = (int)(I - 2); {
            System.out.println(D);
        }

        List < String > Hosts = new ArrayList < String > ();

        if (S >= 24) {
            int val = (int) TotalBites - 0;
            String bitsda = stringMultiply("0", val);
            String bitstoconvert = "1" + bitsda;
            int decimal = Integer.parseInt(bitstoconvert, 2) - 1;
            //String bits = Strings.repeat("0", val);

            for (int IPF4 = E; IPF4 <= decimal; IPF4++) {

                Hosts.add(F1 + "." + F2 + "." + F3 + "." + IPF4);
            }

        } else

        if (S >= 16) {

            int val = (int) TotalBites - 8;
            String bitsda = stringMultiply("0", val);
            String bitstoconvert = "1" + bitsda;
            int decimal = Integer.parseInt(bitstoconvert, 2) - 1;
            //String bits = Strings.repeat("0", val);
            for (int IPF3 = C; IPF3 <= decimal; IPF3++) {
                for (int IPF4 = E; IPF4 <= 255; IPF4++)
                    Hosts.add(F1 + "." + F2 + "." + IPF3 + "." + IPF4);

            }



        } else

        if (S >= 8) {

            int val = (int) TotalBites - 16;
            String bitsda = stringMultiply("0", val);
            String bitstoconvert = "1" + bitsda;
            int decimal = Integer.parseInt(bitstoconvert, 2) - 1;
            //String bits = Strings.repeat("0", val);

            for (int IPF2 = B; IPF2 <= decimal; IPF2++) {
                for (int IPF3 = C; IPF3 <= 255; IPF3++)
                    for (int IPF4 = E; IPF4 <= 255; IPF4++)
                        Hosts.add(F1 + "." + IPF2 + "." + IPF3 + "." + IPF4);


            }
        }

        Hosts.forEach(host -> pingIt(host));

    }

    public static void pingIt(String host) {
        try {
            String ipAddress = host;
            InetAddress inet = InetAddress.getByName(ipAddress);
            System.out.println("Sending Ping Request to " + ipAddress);
            if (inet.isReachable(5000)) {
                System.out.println(ipAddress + " is reachable.");
            } else {
                System.out.println(ipAddress + " NOT reachable.");
            }
        } catch (Exception e) {
            System.out.println("Exception:" + e.getMessage());
        }
    }


    public static String stringMultiply(String s, int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(s);
        }
        return sb.toString();
    }
}
\$\endgroup\$
4
\$\begingroup\$

Requirements?

Please state a short bit about what the requirements are, so we can more easily check how your code might be improved accordingly.

Java Naming Conventions

Please stick to the Java Naming Conventions! (Classes with capital, variables in lowerCamelCase, etc)

Don't use String to store information

Prefer using a custom class to store information, for example, you store ipv4 addresses in a String. Better to create a class for that.

public class IPv4
{
     String s1,s2,s3,s4;

     public IPv4(String s1, String s2, String s3, String s4)
     {
         this.s1 = s1;
         ....
     }

     public String toString() 
     {
        ...
     }
}

Reuse

I noticed this repeating pattern a few times:

 F1 + "." + F2 + "." + F3 + "." + IPF4

It might be worth extracting a method that does exacly this (for example like so):

public static String toIpV4(String s1, String s2, String s3, String s4)
{
    return String.format("%s.%s.%s.%s", s1,s2,s3,s4);
}

Explore Java standard classes and methods

Java comes with a lot of handy standard methods and classes. Since Java 11 we have String.repeat(int count) , which exactly is what you want in stringMultiply. Just look up the functionality you want, it might already be implemented.

\$\endgroup\$
3
\$\begingroup\$

Splitting the input, parsing the parts, figuring the bits and filling the hosts list should all be seperate methods that return the results to main.
Every method should perform a simple task. If you list the steps necessary to perform an action, each step should be a separate method.
Commenting a methods purpose above it will help you and others understand it's inner workings.
Bits and bites are not the same thing. double totalbites = Bits - S;
Also variable names should not start with a capital unless they are all capitals.
Variable names like A, B, C, E, X, S are not very helpful.
input1, input2,... and num1, num2,... would convey a clearer picture of these variables purpose.
Use comments to explain the actions taking place.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.