简体   繁体   中英

reading data from text file and save it into 2D array

I'm new to Java programming... I'm trying to read data from text file and save it into 2D array. So basically, program will receive parameter (IP) and look for the file with same IP number. And the program will read each line and store into an 2D array.

My attempt:

String ipNum = request.getParameter("ipNum");
String root = getServletContext().getRealPath("/");
String dailyPath = root + "\\" + ipNum +".txt";
int[][] myarray = new int[3][6];
    BufferedReader br = new BufferedReader(new FileReader(dailyPath));   
    String line = " ";
    String [] temp;
    while ((line = br.readLine())!= null){ 
        temp = line.split(" "); 
        for(int i = 0; i<myarray.length; i++) {
            for (int j = 0; j<myarray.length; j++) {    
                myarray[i][j] = Integer.parseInt(temp[i]);
            }
        }
    }

Data:

CPU 30 30 30 30 30 30
RAM 70 70 70 70 70 70
HAR 80 80 80 80 80 80
NET 100 100 100 100 100 100

the problem that I'm having is that when I call array, I always get 100 or 0 (assuming empty)

so for example myarray[1][2] should output 30 but i get 100 myarray [2][4] = 70 but i get 100...

I tried to play around with the code for past few hours, I can't figure it out...is my whole code wrong or something?

Thanks for help!

Yeah, you are iterating twice and therefore filling your array with the last value... try this code:

int i = 0;
while ((line = br.readLine())!= null){ 
    temp = line.split(" "); 
    for (int j = 0; j<myarray[i].length; j++) {    
        myarray[i][j] = Integer.parseInt(temp[j]);
    }
    i++;
}

Hope this helps...

Besides Joshs remark you also set the termination conditions the wrong way.

for(int i = 0; i< myarray.length; i++) {
    for (int j = 0; j< myarray[i].length; j++) {    
         myarray[i][j] = Integer.parseInt(temp[i]);
    }
}

This way you can iterate through the array but you can't use this to solve your problem. Instead you need to use 1 while-loop and 1 for-loop.

Your 'while' loop and your first 'for' loop are performing a similar task--for each line you read, you are iterating over every row in your array and filling it's columns with the line you are reading. Every value is 100 because the last line you read is full of 100s.

I suggest removing the first 'for' loop, declare 'int i = 0;' before the 'while' loop, and an 'i++' at the bottom (but inside) the for loop.

@Vivek makes a good point that you need to measure 'myArray[ i ].length' for your j counter.

It looks to me like you're reading each line of text and then parsing that line into a 2D array. This isn't entirely correct as each line can be considered a row. Maybe you could use a counter for each line you read in your while ((line = br.readLine())!= null) loop and then only read into the row of your 2D array at the counter index... like so:

int rowCounter = 0;
while ((line = br.readLine())!= null) {
    for(int i = 0; i<myarray.length; i++)
        myarray[rowCounter][i] = Integer.parseInt(temp[i]);
    rowCounter++;
}

As a side note, if you did want to index into a multidimensional array you also have a side problem. In both of your loops you are iterating until you reach the max number of rows. For your j loop going through the columns this might cause a problem. Instead when using a 2D loop try:

for(int i = 0; i < array.Length; i++)
    for(int j = 0; j < array[0].Length; j++)
        //Do some stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM