I have a text file with set of IP addresses which I read it with BufferedReader want to store it in a 2D string array.
This is my text file:
102.168.212.226, 104.170.214.228, 0
57.68.58.212, 59.70.60.214, 1
10.42.12.22, 12.44.14.24, 2
78.16.22.234, 80.18.24.236, 3
123.168.2.2, 125.170.4.4, 4
Here is my code:
import java.io.;
import java.util.;
public class IPAddressLookup
{
IPAddressLookup() //Constructor
{
int Width = 0, Height = 0;
try
{
File fileA = new File("ClassA.txt");
BufferedReader bra = new BufferedReader(new FileReader(fileA));
String line = "";
String[] str;
while((line = bra.readLine()) != null )
{
str = line.trim().split(", ");
Width = str.length;
Height++;
}
String [][] ClassATable = new String[Height][Width];
for(int i = 0; i < Height; i++)
{
if((line = bra.readLine()) != null )
{
str = line.trim().split(", ");
for(int j = 0; j < Width; j++)
ClassATable[i][j] = str[j];
}
}
for(int i = 0; i < Height; i++)
for(int j = 0; j < Width; j++)
System.out.println(ClassATable[i][j]);
System.out.println("The text file contains:");
System.out.println("Row : " +Height);
System.out.println("Column : " +Width);
}
catch(IOException e)
{
System.out.println("Error: File not found.");
}
}
public static void main(String args[])
{
IPAddressLookup acnl1 = new IPAddressLookup();
}
}
The problem is when I try to print the String array, it shows "null" in output. Also is there any way to read the string IP addresses from file and store them in a integer 2D array??
I am bit new in Java. Can anyone help me with this ?
ACNLab1()is not a constructor since that class doesn't exist in your code).