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

    Default 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?
    Please
    Last edited by novak100; 11-18-2009 at 07:27 PM.

  2. #2
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    6

    Wink void.

    Make a new method.
    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);
    }
    }
    }
    and invoke it from your main.
    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.

  3. #3
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    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

  1. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-20-2009, 12:59 AM
  2. what is wrong in dis code?
    By jitun2004 in forum New To Java
    Replies: 8
    Last Post: 04-15-2009, 10:30 AM
  3. What is wrong with this code
    By rosh72851 in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 02:50 AM
  4. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 12:26 PM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-23-2008, 12:55 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
  •