Results 1 to 3 of 3
- 11-18-2009, 03:03 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
Need help! something wrong in my code
Hi!
I got this program. I need to count how many black pixels are there in the row 12.It should give me the answer 4.
I believe everything is ok i just can get the final results cos im getting this error:
File: E:\java2\Amediumtry.java [line: 15]
Error: E:\java2\Amediumtry.java:15: cannot return a value from method whose result type is void
THATS THE PROGRAM:
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Amediumtry{
public static void main(String[] argv)
throws Exception{
int acc =0;
BufferedImage bi = ImageIO.read(new File("E:\\java2\\image2.png"));
for (int i=0; i<32; i++){
if (bi.getRGB(i,12)==0xFF000000){
acc ++;
break;}
return(acc);
}
}
}
Can someone explain me whats going wrong and where should i look for a solution?
PleaseLast edited by novak100; 11-18-2009 at 06:27 PM.
- 11-18-2009, 08:29 PM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
void.
Make a new method.
and invoke it from your main.Java Code:private static int blackinrow(){ throws Exception{ int acc =0; BufferedImage bi = ImageIO.read(new File("E:\\java2\\image2.png")); for (int i=0; i<32; i++){ if (bi.getRGB(i,12)==0xFF000000){ acc ++; break;} return(acc); } } }
the explanation:
a void is a method but a method can only return what it is.
if you replace void with int you can return an integer from your code
if you replace void with string you can return a string.
- 11-18-2009, 11:59 PM #3
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
Hi Novak,
Your original code is good. You needed to remove the break statement because this stopped the loop. And you also needed to print the result when you had finished counting the number of black pixels.
Java Code:import java.io.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class Amediumtry { public static void main(String[] argv) throws Exception{ int acc = 0; BufferedImage bi = ImageIO.read(new File("E:\\java2\\image2.png")); for (int i=0; i<32; i++){ if (bi.getRGB(i,12)==0xFF000000){ acc++; //[B]break;[/B] } } // Print result [B]System.out.println("The number of black pixels is: " + acc);[/B] } }
If you wanted to make it a method as dinosoep suggested...
Java Code:import java.io.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class Amediumtry { public static void main(String[] argv) throws Exception { int blacks = blackinrow(); System.out.println("The number of black pixels in row 12 is: " + blacks); } static int blackinrow() throws Exception { int acc = 0; BufferedImage bi = ImageIO.read(new File("E:\\java2\\image2.png")); for (int i=0; i<32; i++){ if (bi.getRGB(i,12)==0xFF000000){ acc ++; } } return(acc); } }
A better solution might be:
Java Code:import java.io.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class Amediumtry { public static void main(String[] argv) { // Specify the file name String filename = "E:\\java2\\image2.png"; // Specify the row int row = 12; // Initialize black count int blackCount = 0; // Use a "try-catch-block" to handle a possible IOException made in blackinrow() try { // Pass the filename and row to blackinrow, and store result in blackCount blackCount = blackinrow(filename, row); } catch (IOException e) { // An IOException was thrown because blackinrow() could not read the filename System.out.println("The method blackinrow() threw an exception because it could not read the file."); System.out.println("The program will now stop running..."); // Stop the program System.exit(1); } // Print the results System.out.println("The number of black pixels in row " + row + " is: " + blackCount); } static int blackinrow(String filename, int row) throws IOException { int acc = 0; BufferedImage bi = ImageIO.read(new File(filename)); for (int i=0; i<32; i++){ if (bi.getRGB(i,row)==0xFF000000){ acc ++; } } return(acc); } }
Similar Threads
-
What's going wrong with this code?
By Suurisa in forum New To JavaReplies: 5Last Post: 10-19-2009, 11:59 PM -
what is wrong in dis code?
By jitun2004 in forum New To JavaReplies: 8Last Post: 04-15-2009, 09:30 AM -
What is wrong with this code
By rosh72851 in forum New To JavaReplies: 13Last Post: 10-31-2008, 01:50 AM -
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks