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: Why do I get 0 in the outputs?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Why do I get 0 in the outputs?

    I am trying to generate 100 random numbers between 1 and 1000. Then, I need to find their average and which of them is their maximum. I get a wrong output. How do I fix this?

    Thank you very much.

    import java.util.Random;
     
    public class DataSet 
    {
    	private Random randomGenerator;
    	private int randomInt;
    	private int total;
    	private double average;
    	private int highestValue = 1;
     
    	public void Dataset()
    	{
    		total = 0;
     
    		for (int i = 1; i <= 100; i++)
    		{
    			randomInt = randomGenerator.nextInt(1000);
     
    			total = total + randomInt;
     
     
    			if(highestValue < randomInt)
    			{
    				highestValue = randomInt;
    			}
    		}
     
    	}
     
     
    	public double getAverage()
    	{
    		average = total/100;
    		return average;
    	}
     
    	public int getMaximim()
    	{
    		return highestValue;
    	}
     
     
    	public int getRandomInt()
    	{
    		return randomInt;
    	}
    }

    public class DataSetTester 
    {
    	public static void main(String[] args)
    	{
    		DataSet jean = new DataSet();
     
    		System.out.println("The average of the generated numbers is: " + jean.getAverage());
    		System.out.println("The highest number of the generated random numbers is: " + jean.getMaximim());
     
     
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why do I get 0 in the outputs?

    Break the problem down. Try doing a smaller range of random numbers, or less of them, or better yet even using some fixed numbers to which you know the answer. Add some println's debug statements in there to make sure each portion of code is producing what you expect.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why do I get 0 in the outputs?

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/64464-why-do-i-get-0-outputs.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Why do I get 0 in the outputs?

    Quote Originally Posted by jean28 View Post
    ...I am trying to generate 100 random numbers...I get a wrong output....
    What method is responsible for generating the numbers?

    Try putting some print statements in that function that go something like:
    .
    .
    .
                    System.out.println("Generating 100 random numbers");
                    for (int i = 1; i <= 100; i++)
                    {
                            randomInt = randomGenerator.nextInt(1000);
                            System.out.println("randomInt = " + randomInt);
    .
    .
    .

    If you don't understand the output from these statements (or lack thereof), put a print statement in your main() method where you are calling that function.


    Cheers!

    Z
    Last edited by Zaphod_b; October 30th, 2012 at 12:25 PM.

Similar Threads

  1. Connecting Inputs to Outputs – Electronic Board
    By podge_y2k1 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 1st, 2012, 09:22 AM
  2. Code only outputs 1's
    By LoganC in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 27th, 2012, 09:26 PM
  3. Java program is running .. But I am not getting my outputs in the JSP
    By sankarravi6 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 30th, 2012, 07:01 PM
  4. Is there a way I could create/get a program that outputs a form in BBC?
    By Elite Cow in forum Java Theory & Questions
    Replies: 5
    Last Post: October 11th, 2011, 01:01 PM
  5. LinkedList outputs ONLY last element
    By hexwind in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 30th, 2011, 04:57 AM