Results 1 to 3 of 3
- 11-17-2009, 03:08 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
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);
}
}
}
- 11-17-2009, 03:08 PM #2
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
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
- 11-17-2009, 04:12 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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
-
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Reading a Field in a Binary File
By janakiram.attuluri in forum Advanced JavaReplies: 2Last Post: 01-09-2008, 10:47 AM -
Reading Binary File and Changing data
By janakiram.attuluri in forum Advanced JavaReplies: 1Last Post: 12-21-2007, 10:10 AM -
reading a binary file with a RAF
By jkurth in forum Advanced JavaReplies: 2Last Post: 12-20-2007, 07:30 AM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks