Results 1 to 3 of 3
  1. #1
    str23 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Unhappy reading one line of a TXT file and counting 0 binary digets

    8 8
    1 1 1 1 1 1 1 1
    1 0 0 0 0 0 0 1
    1 0 1 1 1 1 0 1 Copy and pasted into image1.txt
    1 0 1 1 1 1 0 1
    1 0 1 1 1 1 0 1
    1 0 1 1 1 1 0 1
    1 0 0 0 0 0 0 1
    1 1 1 1 1 1 1 1

    Hello. i am a begginer to java and have no clue of how to do this task.

    2) Output the number of black pixels on row number 4. The first row has number 0. In this example the output would be:
    2


    so i know the answer of the output needed =2.

    here is my code

    I will be very gratefull for any help

    thanks

    import java.io.FileReader;
    import java.io.*;
    public class Q2{
    public static void main(String[] argv)
    throws Exception{
    try{
    StreamTokenizer tok =
    new StreamTokenizer(new FileReader("C:\\image1.txt"));
    int acc = 0;
    tok.nextToken();
    int w = (int) tok.nval;
    tok.nextToken();
    int h = (int) tok.nval;
    int [][] img = new int[w][h];
    for (int j=0;j<h;j++)
    for (int i=0;i<w;i++){
    tok.nextToken();
    img[i][j] = (int) tok.nval;
    if (img[i][j] == 0)
    acc ++;
    }
    System.out.println(acc);
    }
    catch (IOException e) {
    String error = e.toString();
    System.out.println(error);
    }
    }
    }

  2. #2
    str23 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    these are the other questions i have to do

    if i fail these i will fail university lol

    3) Output the row number of every row that includes a black pixel:

    1 2 3 4 5 6

    4) Output the row number of the last line that contains a black pixel.

    6

    5) Output number of rows that are completely white.

    2

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Firstly format your code and use code tags.
    Unformatted code is almost unreadable (at least to me).

    Secondly, what does the code you have given us actually do? Then maybe we can start to fix it.

    As a hint I wouldn't use the StreamTokeniser. Why don't you simply read in each line of the file and work from that?

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  2. Reading a Field in a Binary File
    By janakiram.attuluri in forum Advanced Java
    Replies: 2
    Last Post: 01-09-2008, 10:47 AM
  3. Reading Binary File and Changing data
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 10:10 AM
  4. reading a binary file with a RAF
    By jkurth in forum Advanced Java
    Replies: 2
    Last Post: 12-20-2007, 07:30 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

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