2

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 ?

6
  • 1
    Note that your code wouldn't even compile (ACNLab1() is not a constructor since that class doesn't exist in your code). Commented Jan 13, 2017 at 12:48
  • 1
    Btw, why don't you use lists instead of arrays, at least for the 1st dimension? That way you wouldn't have to get the number of lines first but could keep adding lines as you read them. Commented Jan 13, 2017 at 12:51
  • 1
    @Thomas +1 Also, you seem to violate a few naming conventions, for instance variable names should follow camelCapitalization as with method names. Therefore the first letter must be lower-case and every seperate word in the name must start with a capital letter. This does not affect code functionality at all, it simply makes the code easier to read. Commented Jan 13, 2017 at 12:51
  • My apologies. Constructor name is actually IPAddressLookup. Sorry for that! Edit the post again. Commented Jan 13, 2017 at 13:01
  • 1
    @fillpant, thanks for advise about naming conventions. Commented Jan 13, 2017 at 13:22

2 Answers 2

4

It would seem that you are reading the file twice, but you are not resetting the reader so that on the second loop, you start again from the top.

You would need to add something like so: bra.getChannel().position(0) between your loops (after this: String [][] ClassATable = new String[Height][Width];). This will reset your reader so that it can start once again from the top.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this code:

public class IPAddressLookup {
ArrayList<String[]> ip = new ArrayList<>();
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(", ");
            ip.add(str);
            Width = str.length;
            Height++;
        }

        String [][] ClassATable = new String[Height][Width];

        for(int i=0 ; i<ip.size();i++){
            String[] temp = ip.get(i);
            for(int j=0;j<temp.length;j++){
                ClassATable[i][j] = temp[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();
}

}

Result:

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 The text file contains: Row : 5 Column : 3

4 Comments

Thanks! I will take a look. Also can you explain lines like ip.add(str), ip.get(i)??
I have created a Arraylist of string arrays. ip.add(str) is to add an array of string ex. {102.168.212.226, 104.170.214.228, 0} in arraylist. And ip.get(i) to get the string array from arraylist.
Thank you very much! It is working. Sorry for asking dumb question. The method name itself explains.
Welcome. @Spectra

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.