patternjavaMinor
IP sorting program
Viewed 0 times
sortingprogramstackoverflow
Problem
The purpose of this little program is to:
I would like to know if you think the program what it is meant to do properly. I would particularly be interested in your thoughts on the efficiency of the program and if any improvements can be made to it.
```
import java.util.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
public class IPSort
{
String[] tests = {":8:","::::5:6::8","::::5:6::7","::::5:6::8","123..245.23","1...","..1.","123...23",".1..","123..245.23", "123..245.23", "104.244.253.29", "1.198.3.93", "32.183.93.40", "32.183.93.40", "104.30.244.2", "104.244.4.1","0.0.0.1",":a:","::5:3:4:5:6:78","1::2:3","1::2:3:4","1::5:256.2.3.4","1:1:3000.30.30.30","ae80::217:f2ff:254:7:237:98"};
ArrayList bit32 = new ArrayList();
ArrayList bit128 = new ArrayList();
ArrayList cleanBit32 = new ArrayList();
ArrayList cleanBit128 = new ArrayList();
boolean myMatcher32Bit(String s)
{
Pattern patter32Bit = Pattern.compile("^(?=(?:[^.]\\.){3}[^.]$)(?=(?:[^:]:){0}[^:]$)(?=(?:[^a-zA-Z][^a-zA-Z])$)");
Matcher matcher32Bit = patter32Bit.matcher(s);
return matcher32Bit.find();
}
boolean myMatcher128Bit(String s)
{
Pattern patter128Bit = Pattern.compile("^(?=(?:[^.]\\.){0}[^.]$)(?=(?:[^:]:){1,7}[^:]$)");
Matcher matcher128Bit = patter128Bit.matcher(s);
return matcher128Bit.find();
}
public void sortIntoRespectiveIPTypes()
{
for(String s: tests)
{
if(myMatcher32Bit(s))
{
bit32.add(s);
}
else if(myMatcher128Bit(s))
{
bit128.add(s);
}
}
System.out.println("32 bit IPs");
for(String ip: bit32)
- Separate
IPv4 addressesandIPv6 addresses
- Will be able to sort them even if the formatting of the input is not great
- Sort them into an order
- Print them into the system terminal in ascending and descending order
I would like to know if you think the program what it is meant to do properly. I would particularly be interested in your thoughts on the efficiency of the program and if any improvements can be made to it.
```
import java.util.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
public class IPSort
{
String[] tests = {":8:","::::5:6::8","::::5:6::7","::::5:6::8","123..245.23","1...","..1.","123...23",".1..","123..245.23", "123..245.23", "104.244.253.29", "1.198.3.93", "32.183.93.40", "32.183.93.40", "104.30.244.2", "104.244.4.1","0.0.0.1",":a:","::5:3:4:5:6:78","1::2:3","1::2:3:4","1::5:256.2.3.4","1:1:3000.30.30.30","ae80::217:f2ff:254:7:237:98"};
ArrayList bit32 = new ArrayList();
ArrayList bit128 = new ArrayList();
ArrayList cleanBit32 = new ArrayList();
ArrayList cleanBit128 = new ArrayList();
boolean myMatcher32Bit(String s)
{
Pattern patter32Bit = Pattern.compile("^(?=(?:[^.]\\.){3}[^.]$)(?=(?:[^:]:){0}[^:]$)(?=(?:[^a-zA-Z][^a-zA-Z])$)");
Matcher matcher32Bit = patter32Bit.matcher(s);
return matcher32Bit.find();
}
boolean myMatcher128Bit(String s)
{
Pattern patter128Bit = Pattern.compile("^(?=(?:[^.]\\.){0}[^.]$)(?=(?:[^:]:){1,7}[^:]$)");
Matcher matcher128Bit = patter128Bit.matcher(s);
return matcher128Bit.find();
}
public void sortIntoRespectiveIPTypes()
{
for(String s: tests)
{
if(myMatcher32Bit(s))
{
bit32.add(s);
}
else if(myMatcher128Bit(s))
{
bit128.add(s);
}
}
System.out.println("32 bit IPs");
for(String ip: bit32)
Solution
Import Statements
Before I start complaining about your import statements, I want you to know that the package declaration
Never import the whole package. Always import only what you need. So in the case of your import statements:
It would be:
Looks a lot more organized, and you see exactly what you need.
Formatting
I see some formatting issues in your code. In method declarations:
Parameters should be separated with a comma followed by a space, not just a comma. You do this once, but just once.
Here:
There should be always spaces before and after an operator, not just
About your bracing style, you mostly have the braces on a new line, but here:
You don't.
Choose a style, and stick to it. I suggest the brace on the same line, as that is what the standard Java conventions recommend, but it's your choice.
List declarations
Instead of declaring an
Should be:
And this:
Turns into:
And everywhere else you declare them as
The code so far:
```
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IPSort {
String[] tests = { ":8:", "::::5:6::8", "::::5:6::7", "::::5:6::8",
"123..245.23", "1...", "..1.", "123...23", ".1..", "123..245.23",
"123..245.23", "104.244.253.29", "1.198.3.93", "32.183.93.40",
"32.183.93.40", "104.30.244.2", "104.244.4.1", "0.0.0.1", ":a:",
"::5:3:4:5:6:78", "1::2:3", "1::2:3:4", "1::5:256.2.3.4",
"1:1:3000.30.30.30", "ae80::217:f2ff:254:7:237:98" };
List bit32 = new ArrayList();
List bit128 = new ArrayList();
List cleanBit32 = new ArrayList();
List cleanBit128 = new ArrayList();
boolean myMatcher32Bit(String s) {
Pattern patter32Bit = Pattern.compile(
"^(?=(?:[^.]\\.){3}[^.]$)(?=(?:[^:]:){0}[^:]$)(?=(?:[^a-zA-Z][^a-zA-Z])$)");
Matcher matcher32Bit = patter32Bit.matcher(s);
return matcher32Bit.find();
}
boolean myMatcher128Bit(String s) {
Pattern patter128Bit = Pattern.compile(
"^(?=(?:[^.]\\.){0}[^.]$)(?=(?:[^:]:){1,7}[^:]$)");
Matcher matcher128Bit = patter128Bit.matcher(s);
return matcher128Bit.find();
}
public void sortIntoRespectiveIPTypes() {
for (String s : tests) {
if (myMatcher32Bit(s)) {
bit32.add(s);
} else if (myMatcher128Bit(s)) {
bit128.add(s);
}
}
System.out.println("32 bit IPs");
for (String ip : bit32) {
System.out.println(" " + ip);
}
System.out.println("\n128 bit IPs");
for (String ip : bit128) {
System.out.println(" " + ip);
}
int count = 0;
for (String ip : tests) {
if (myMatcher32Bit(ip) == false && myMatcher128Bit(ip) == false) {
count++;
}
}
if (count != 0) {
System.out.println("\nDidn't match an IP format");
for (String ip : tests) {
if (myMatcher32Bit(ip) == false && myMatcher128Bit(
ip) == false) {
System.out.println(" " + ip);
}
Before I start complaining about your import statements, I want you to know that the package declaration
import java.lang.* is implied in all Java programs and is not necessary.Never import the whole package. Always import only what you need. So in the case of your import statements:
import java.util.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;It would be:
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;Looks a lot more organized, and you see exactly what you need.
Formatting
I see some formatting issues in your code. In method declarations:
public void sort128BitIPs(ArrayList bit128,ArrayList newBit128)Parameters should be separated with a comma followed by a space, not just a comma. You do this once, but just once.
Here:
tempArray[i]= Integer.parseInt(s);There should be always spaces before and after an operator, not just
=. In your code, you also have the same problem with +.About your bracing style, you mostly have the braces on a new line, but here:
while(lastIndex != -1){
lastIndex = str.indexOf(findStr,lastIndex);
if(lastIndex != -1){
count++;
lastIndex += findStr.length();
}
}You don't.
Choose a style, and stick to it. I suggest the brace on the same line, as that is what the standard Java conventions recommend, but it's your choice.
List declarations
Instead of declaring an
ArrayList as an ArrayList, it is better practice to declare it to just List. Here:ArrayList bit32 = new ArrayList();
ArrayList bit128 = new ArrayList();
ArrayList cleanBit32 = new ArrayList();
ArrayList cleanBit128 = new ArrayList();Should be:
List bit32 = new ArrayList();
List bit128 = new ArrayList();
List cleanBit32 = new ArrayList();
List cleanBit128 = new ArrayList();And this:
public void run(ArrayList bit32,ArrayList bit128,ArrayList newBit32,ArrayList newBit128)
{
sortIntoRespectiveIPTypes();
sort32BitIPs(bit32,newBit32);
sort128BitIPs(bit128,newBit128);
printInOrder(newBit32,newBit128);
}Turns into:
public void run(List bit32, List bit128, List newBit32, List newBit128)
{
sortIntoRespectiveIPTypes();
sort32BitIPs(bit32,newBit32);
sort128BitIPs(bit128,newBit128);
printInOrder(newBit32,newBit128);
}And everywhere else you declare them as
ArrayLists, change it into List.The code so far:
```
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IPSort {
String[] tests = { ":8:", "::::5:6::8", "::::5:6::7", "::::5:6::8",
"123..245.23", "1...", "..1.", "123...23", ".1..", "123..245.23",
"123..245.23", "104.244.253.29", "1.198.3.93", "32.183.93.40",
"32.183.93.40", "104.30.244.2", "104.244.4.1", "0.0.0.1", ":a:",
"::5:3:4:5:6:78", "1::2:3", "1::2:3:4", "1::5:256.2.3.4",
"1:1:3000.30.30.30", "ae80::217:f2ff:254:7:237:98" };
List bit32 = new ArrayList();
List bit128 = new ArrayList();
List cleanBit32 = new ArrayList();
List cleanBit128 = new ArrayList();
boolean myMatcher32Bit(String s) {
Pattern patter32Bit = Pattern.compile(
"^(?=(?:[^.]\\.){3}[^.]$)(?=(?:[^:]:){0}[^:]$)(?=(?:[^a-zA-Z][^a-zA-Z])$)");
Matcher matcher32Bit = patter32Bit.matcher(s);
return matcher32Bit.find();
}
boolean myMatcher128Bit(String s) {
Pattern patter128Bit = Pattern.compile(
"^(?=(?:[^.]\\.){0}[^.]$)(?=(?:[^:]:){1,7}[^:]$)");
Matcher matcher128Bit = patter128Bit.matcher(s);
return matcher128Bit.find();
}
public void sortIntoRespectiveIPTypes() {
for (String s : tests) {
if (myMatcher32Bit(s)) {
bit32.add(s);
} else if (myMatcher128Bit(s)) {
bit128.add(s);
}
}
System.out.println("32 bit IPs");
for (String ip : bit32) {
System.out.println(" " + ip);
}
System.out.println("\n128 bit IPs");
for (String ip : bit128) {
System.out.println(" " + ip);
}
int count = 0;
for (String ip : tests) {
if (myMatcher32Bit(ip) == false && myMatcher128Bit(ip) == false) {
count++;
}
}
if (count != 0) {
System.out.println("\nDidn't match an IP format");
for (String ip : tests) {
if (myMatcher32Bit(ip) == false && myMatcher128Bit(
ip) == false) {
System.out.println(" " + ip);
}
Code Snippets
import java.util.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public void sort128BitIPs(ArrayList<String> bit128,ArrayList<String> newBit128)tempArray[i]= Integer.parseInt(s);while(lastIndex != -1){
lastIndex = str.indexOf(findStr,lastIndex);
if(lastIndex != -1){
count++;
lastIndex += findStr.length();
}
}Context
StackExchange Code Review Q#114906, answer score: 5
Revisions (0)
No revisions yet.