0

I have made a code to find subnet masks,broadcast id,etc from ip address input. However I am getting the following error and the code is running only partially with no syntax error:

linux@ubuntu:~/Desktop$ javac Subnet.java
linux@ubuntu:~/Desktop$ java Subnet
Enter the ip address:138.101.114.250
IP in Binary is10001010011001010111001011111010
Enter the number of addresses:26
Number of bits reqd for address=5
the subnet mask is=27
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Subnet.main(Subnet.java:31)
linux@ubuntu:~/Desktop$ ^C

This is the code

import java.util.Scanner;
class Subnet{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the ip address:");
String ip=sc.nextLine();
String split_ip[]=ip.split("\\.");
String split_bip[]=new String[4];
String bip="";
for(int i=0;i<4;i++)
{
split_bip[i]=appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i])));
bip+=split_bip[i];
}
System.out.println("IP in Binary is"+bip);
System.out.print("Enter the number of addresses:");
int n = sc.nextInt();

int bits=(int)Math.ceil(Math.log(n)/Math.log(2));
System.out.println("Number of bits reqd for address="+bits);

int mask=32-bits;
System.out.println("the subnet mask is="+mask);
int fbip[]=new int[32];
for (int i=0;i<32;i++)fbip[i]=(int)bip.charAt(i)-48;
for(int i=31;i>31-bits;i--)
fbip[i]&=0;
String fip[]={"","",""};
for(int i=0;i<32;i++)
fip[i/8]=new String(fip[i/8]+fbip[i]);
System.out.print("Subnet address is=");
for(int i=0;i<4;i++){
System.out.print(Integer.parseInt(fip[i],2));
if(i!=3)System.out.print(".");
}
System.out.println();
int lbip[]=new int[32];
for(int i=0;i<32;i++)lbip[i]=(int)bip.charAt(i)-48;
for(int i=31;i>31-bits;i--)
lbip[i]|=1;
String lip[]={"","",""};
for(int i=0;i<32;i++)
lip[i/8]=new String(lip[i/8]+lbip[i]);
System.out.print("Broadcast address is=");
for(int i=0;i<4;i++){
System.out.print(Integer.parseInt(lip[i],2));
if(i!=3)System.out.print(".");
}
System.out.println();
}
static String appendZeros(String s)
{
String temp=new String("00000000");
return temp.substring(s.length())+s;
}
}

And it is only a very minor error but I can't figure out where.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

ArrayIndexOutOfBoundsException means that you're trying to access an element that does not exist in an array, i.e. an element at an index that's greater than the last element's index.

String[] someArray = new String[]{"a - index 0", "b - index 1", "c - index 2"}; String inexistent = someArray[3]; //This will throw an ArrayIndexOutOfBoundsException since there are only three elements in the array, the last having index 2 (it always starts at 0 = zero-based)

Your error being Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Subnet.main(Subnet.java:31) you should take a look at line 31 of the Subnet.java file and see how you computed the array index

It would most certainly help to add a breakpoint at that line and inspect the array and the computed index

Cristi
  • 117
  • 1
  • 2
  • 9
0

I finally found the small error:

String fip[]={"","",""};

It is near the middle of the full code. The problem was that I was submitting 4 strings but in the above error code line I was letting it take only 3, the answer is that I should've added one more brackets, so a total of 4, like this:

String fip[]={"","","",""};
halfer
  • 19,824
  • 17
  • 99
  • 186