Results 1 to 6 of 6
  1. #1
    theodorekon is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default 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
    // This is a comment
    234
    44
    // This is also a comment
    werg
    bf

    34r2
    dfv
    // End of file
    My code 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++;
            	}
    Now the matrix created is:
    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??

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by theodorekon View Post
    Any ideas??

    hi, with a simple if-statement you could exclude the unwanted lines in the while-loop. or use the class arraylist to store your lines and then use the method remove() the unwanted elements.

  4. #4
    theodorekon is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    hi, with a simple if-statement you could exclude the unwanted lines in the while-loop. or use the class arraylist to store your lines and then use the method remove() the unwanted elements.
    The problem is that when I use in the while loop the statement

    if(scanLines.next!="") {...}

    it jumps a line every time it runs the while loop. As a result the lines[] matrix only gets half of the lines of the txt file. Do you have any other if statement I could use??

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by theodorekon View Post
    The problem is that when I use in the while loop the statement

    if(scanLines.next!="") {...}

    it jumps a line every time it runs the while loop. As a result the lines[] matrix only gets half of the lines of the txt file. Do you have any other if statement I could use??

    Declare the String s and then use

    while ((s=scanLines.hasNext())) {
    if (!s.contains("\\"))

    for saying: if the string s does not contain "\\" then continue.

  6. #6
    theodorekon is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    Declare the String s and then use

    while ((s=scanLines.hasNext())) {
    if (!s.contains("\\"))

    for saying: if the string s does not contain "\\" then continue.
    It doesn't work. It says:

    ScanXan5.java:43: incompatible types
    found : boolean
    required: java.lang.String
    while ((s=scanLines.hasNext())) {
    ^
    ScanXan5.java:43: incompatible types
    found : java.lang.String
    required: boolean
    while ((s=scanLines.hasNext())) {
    ^
    2 errors

Similar Threads

  1. Reducing fractions
    By SwEeTAcTioN in forum New To Java
    Replies: 10
    Last Post: 01-13-2010, 04:51 PM
  2. Arrays -changing from 1 dimension to 2d
    By dbashby in forum New To Java
    Replies: 36
    Last Post: 10-16-2009, 10:58 PM
  3. applet and dimension
    By mensaFool in forum New To Java
    Replies: 5
    Last Post: 09-15-2009, 04:37 AM
  4. Replies: 5
    Last Post: 05-18-2009, 12:42 PM
  5. noob: two-dimension array
    By bobmasta5 in forum New To Java
    Replies: 5
    Last Post: 03-15-2009, 11:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •