This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes 2D array input of text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "2D array input of text file" Watch "2D array input of text file" New topic
Author

2D array input of text file

Ken Perrine
Greenhorn

Joined: Oct 25, 2012
Posts: 4
I'm having problems reading in a 2-D matrix. It reads in OK, but 2 problems arise. See below.
THANKS
Nooooobie Ken



Output is OK except:
1. An exception is thrown part way through: “Exception in thread "main" java.lang.NullPointerException at read2d.Read2d.main(Read2d.java:13)”
It is thrown at different places each time I run it, and sometimes the exception is split between 2 disparate locations on the output. However, it doesn’t affect the matrix itself.
2. The output does not print a line break or “fin”.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
You should have used the code button; I have gone back to your code and used it, and you can see how much better it looks.
What’s a 2D array? It doesn’t exist in Java. What you have is an array of arrays, which is actually better than a 2D array.
How do you know your array will have 154 elements? If you only have 153 lines, you are going to run out of lines. The 154th will be null if you use a BufferedReader.
How do you know each line will have 13 elements? Why, if you are splitting the line to get an array, don’t you simply reassign the line to the corresponding element of the outer array?
Why have you got your little text file in the root directory of C?
Why does your matrix contain only Strings? I though a matrix contained numbers?
Have you come across the syntax to test whether a line is null? It is peculiar and you would never guess it in 100 years:-
Ken Perrine
Greenhorn

Joined: Oct 25, 2012
Posts: 4
Campbell Ritchie wrote:You should have used the code button; I have gone back to your code and used it, and you can see how much better it looks.
What’s a 2D array? It doesn’t exist in Java. What you have is an array of arrays, which is actually better than a 2D array.
How do you know your array will have 154 elements? If you only have 153 lines, you are going to run out of lines. The 154th will be null if you use a BufferedReader.
How do you know each line will have 13 elements? Why, if you are splitting the line to get an array, don’t you simply reassign the line to the corresponding element of the outer array?
Why have you got your little text file in the root directory of C?
Why does your matrix contain only Strings? I though a matrix contained numbers?
Have you come across the syntax to test whether a line is null? It is peculiar and you would never guess it in 100 years:-


I'm not sure if this is how I reply, but here it goes:
I'm a NOOOOBIE-- didn't know about the code button, will use if from now on.
My 2D array has a fixed number of 153 rows and 13 columns, each consisting of FP numbers (I'll convert them later-- for simplicity I just wanted to see if I could read in the array). I know the 154 element is null, that's why I'm trying Buffered Reader with an Exception.
I'm not sure what you mean by an "outer" array. In VB and before, you had arrays of the type (xx,yy), so the "array of arrays" is confusing. I will have a lot of data (.txt) files in an (xx,yy) arrangement to read in.
The text file with the data of the array being read is on the root directory for simplicity, as I am trying different permutations and the directory seems to change with each change in code-- it's so I know it will read and not get kicked back for "not found". I'll work on keeping it in the correct directory later.
Yup, I know about the null, but I thought the BR with Exception did the same thing.
So, I'm still not sure why the last 2 lines of code aren't run--???
Thanks,
Ken
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Don't rely on an exception being thrown to control the flow of logic in your program. Exceptions are for when things going bad. The readline method can return null therefore you should check the thisLine variable for null before trying to call split, otherwise you would get a NullPointerException. Your last two lines don't get executed because the exception causes the program to exit before those lines can be executed.


Junilu - [How to Ask Questions] [How to Answer Questions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
You might have 2D arrays in VB but you don’t in Java. You have one array with 154 elements, which you are trying to fill with 153 elements, and when you get to the 154th of 153 … Exactly what I thought earlier.
You can set each element of the outer array like thisNo need to mess about counting to 13 or copying, or anything like that.

There is a serious problem with hard‑coding numbers like 154. They tie you to a file of a particular size and format. What happens if the file has 153 lines, or 155. Is it more serious to have a null or to miss out a line from processing? I can’t answer that for you.
You probably havent’t come across Lists<String> yet, because a List can make things easier by counting the lines for you. Then you know exactly what size array you would want (if you still want an array at all).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
As I said 154 elements to that array, and each element is itself an array.
 
wood burning stoves
 
subject: 2D array input of text file