Results 1 to 6 of 6
- 04-15-2010, 01:07 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
Taking out elements from a matrix thus reducing its dimension
Hi,
I have written a program in Java2 which reads a txt file and stores each line of the file in a matrix called lines[]. Now what I want to do is to read through this matrix and take out some of its elements thus reducing its dimension. For example some of the lines in my file start with // which means that they are comments and I want to exclude those from my matrix. Some other lines are blank and I also want to exclude them. To make it more clear I give the example below:
I have the input.txt file which is as follows
My code is:// This is a comment
234
44
// This is also a comment
werg
bf
34r2
dfv
// End of file
Now the matrix created is:Java Code:int numLines=0; scanLines = new Scanner(new BufferedReader(new FileReader("input.txt"))); scanLines.useDelimiter("\n"); while (scanLines.hasNext()) { lines[numLines]=scanLines.next().trim(); numLines++; }
lines[// This is a comment, 234, 44, // This is also a comment, werg, bf, , 34r2, dfv, // End of file]
which has a dimension of 10. I want to exlude the lines which start with // and the ones that are blank and end up with the matrix:
lines[234, 44, werg, bf, 34r2, dfv,]
which has a dimension of 6.
Any ideas??
- 04-15-2010, 02:48 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Why are you using an array? Is this a necessity?
If not I would suggest switching over to a List interface, ArrayList should be fine. This type of an interface makes dealing with the removal or addition of data much simplier.
If you must stick with arrays, you will probably need to iterate through the array twice. Once to get the total number of lines to put in the new array. And the second time to actually put the lines in the new array.
- 04-15-2010, 03:12 PM #3
- 04-15-2010, 05:02 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
- 04-15-2010, 05:21 PM #5
- 04-15-2010, 05:55 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Reducing fractions
By SwEeTAcTioN in forum New To JavaReplies: 10Last Post: 01-13-2010, 04:51 PM -
Arrays -changing from 1 dimension to 2d
By dbashby in forum New To JavaReplies: 36Last Post: 10-16-2009, 10:58 PM -
applet and dimension
By mensaFool in forum New To JavaReplies: 5Last Post: 09-15-2009, 04:37 AM -
[SOLVED] how to align JTextFiels[dimension] and JComboBox[dimension]
By Wolverine in forum AWT / SwingReplies: 5Last Post: 05-18-2009, 12:42 PM -
noob: two-dimension array
By bobmasta5 in forum New To JavaReplies: 5Last Post: 03-15-2009, 11:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks