Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Odd Integer Output

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Odd Integer Output

    I'm fairly well versed in Java syntax and logic, but I'm not incredibly experienced with any sort of useful programming or app development (Meaning I've taken AP CS 1 and did well). I'm trying to get into program development, so as a basic practice problem I am trying to make a program that measures the radius of a white circle on a black background and prints the result to console. Not only is the program not working, the output seems more like a string than an int. Any help is appreciated. The program is composed of two classes, the main, in which the static main method is held, and the RadiusFinder, which holds the methods which do the actual computation. These, as well as the picture I am attempting to analyze, are posted below. The console simply prints 091-91.

    [B][ATTACH=CONFIG]3461[/ATTACH][/B]
     
    [B]Main Class:[/B]
    import java.awt.*;
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    public class Main {
    	public static void main(String[] args) {
    		BufferedImage img = null;
    		RadiusFinder test = new RadiusFinder(img);
    		test.pictureSet();
    		System.out.println(test.radius());
    	}
    }
     
    [B]RadiusFinder:[/B]
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
     
     
     
    import javax.imageio.ImageIO;
     
    public class RadiusFinder {
     
    	File picture;
     
    	BufferedImage img = null;
    	int width;
    	int height;
    	int greatestY;
    	int leastY;
    	int greatestX;
    	int leastX;
     
     
    	public void pictureSet() 
    	{
    		try {
    			picture = new File("C:\\Users\\Matthew\\Pictures\\Camera Roll\\Untitled.jpg");
    			img = ImageIO.read(picture);
    		}catch(Exception e)
    		{
    			System.out.println(e);
    		}
     
    	}
    	public RadiusFinder(BufferedImage pic) 
    	{
    		pic = img;
     
    	}
    	public void verticalEdgeFinder() 
    	{
    		boolean isLeast = false;
    		boolean isGreatest = false;
    		width = img.getWidth();
    		height = img.getHeight();
    		//--------------Used for finding the greatest y value that a white pixel occurs at------
    		for(int i = height - 1; i < 0 && !isGreatest; i--) 
    		{
    			for(int z = 0; z < width && !isGreatest; z++) 
    			{
    				Color pixel = new Color(img.getRGB(z, i));
    				int red = pixel.getRed();
    				int blue = pixel.getBlue();
    				int green = pixel.getGreen();
    				if(red == 255 && blue == 255 && green == 255) 
    				{
    					isGreatest = true;
    					greatestY = i;
    					System.out.print(greatestY);
    				}
    			}
    		}
    		//--------------Used for finding the lowest y value that a white pixel occurs at------
    		for(int i = 0; i < height - 1 && !isLeast; i++) 
    		{
    			for(int z = 0; z < width && !isLeast; z++) 
    			{
    				Color pixel = new Color(img.getRGB(z, i));
    				int red = pixel.getRed();
    				int blue = pixel.getBlue();
    				int green = pixel.getGreen();
    				if(red == 255 && blue == 255 && green == 255) 
    				{
    					isLeast = true;
    					leastY = i;
    					System.out.print(leastY);
    				}
    			}
    		}
    	}
     
    	public void horizontalEdgeFinder() 
    	{
    		boolean isLeast = false;
    		boolean isGreatest = false;
    		width = img.getWidth();
    		height = img.getHeight();
    		for(int i = width - 1; i < 0 && !isGreatest; i--) 
    		{
    			for(int z = 0; z < height && !isGreatest; z++) 
    			{
    				Color pixel = new Color(img.getRGB(i, z));
    				int red = pixel.getRed();
    				int blue = pixel.getBlue();
    				int green = pixel.getGreen();
    				if(red == 255 && blue == 255 && green == 255) 
    				{
    					isGreatest = true;
    					greatestX = i;
    					System.out.print(greatestX);
    				}
    			}
    		}
    		for(int i = 0; i < width - 1 && !isLeast; i++) 
    		{
    			for(int z = 0; z < height && !isLeast; z++) 
    			{
    				Color pixel = new Color(img.getRGB(i, z));
    				int red = pixel.getRed();
    				int blue = pixel.getBlue();
    				int green = pixel.getGreen();
    				if(red == 255 && blue == 255 && green == 255) 
    				{
    					isLeast = true;
    					leastX = i;
    					System.out.print(leastY);
    				}
    			}
    		}
    	}
     
    	public int radius() 
    	{
    		pictureSet();
    		horizontalEdgeFinder();
    		verticalEdgeFinder();
    		int rad;
    		if(greatestX-leastX > greatestY - leastY) 
    		{
    			rad = greatestX- leastX;
    		}else {
    			rad = greatestY-leastY;
    		}
    		return rad;
    	}
     
    }
    Last edited by mxb1999; December 30th, 2018 at 04:50 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Odd Integer Output

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.


    The output I appear to be getting is 091-91
    Please copy the program's output and paste here. Not an image.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Odd Integer Output

    Since you are using print and not println you can't tell where one prints stops and the other begins. For example, 091-91 could be
    0
    91
    -91
    as well as other possibilities.

    Regards,
    Jim

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Odd Integer Output

    Also the print outs should include a String that identifies what is being printed:
       System.out.println("theValue="+theValue);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  2. Counting the amount of zeroes, odd and even numbers in an integer
    By 54stickers in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 07:17 PM
  3. Odd Even - For loop
    By clevel211 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2010, 08:09 PM
  4. Odd/Even
    By SOK in forum What's Wrong With My Code?
    Replies: 16
    Last Post: September 27th, 2010, 07:47 AM
  5. Odd Even Zero Counter?
    By velop in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:13 PM