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 2 of 2

Thread: Arrays in java:

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Arrays in java:

    A program that analyzes a set of numbers can be very useful. Create an Analysis application that prompts the user for numbers in the range 1 through 50, terminated by a sentinel, and then performs the following analysis on the numbers
    Determine the average number
    Determine the maximum number
    Determine the range (maximum - minimum)
    Displays a histogram that shows the numbers in each five-unit range.

    --- Update ---

    how do i go about solving this problem?
    this is what i have so far:
    import java.util.Scanner;

    public class Arrays {
    public static void main (String [] args){

    Scanner scan = new Scanner (System.in);
    int array [] = new int[0];
    int num;
    System.out.println("Enter a number between 1 - 50 inclusive OR -1 if u wish to end");
    array = num.nextInt();

    //loop with sentinel value
    while( num != -1)
    array[0]++;

    // maximum value
    int maximum;
    for( num=0; num<=50; num++){
    if (maximum< array[num]){
    maximum= array[num];
    }
    }

    // minimum value
    int minimum;
    for (num=0; num >=50; num++){
    if(minimum>array[num]){
    minimum=array [num];
    }
    }

    // average
    int average;
    int sum;
    for(num=0; num<=50;num++){
    sum=sum+array[num];
    }
    average = sum/50;

    int range;
    range = maximum-minimum;

    }

    }


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Arrays in java:

    I am not sure, but I think your array holds no values in it. And you try to compare the number with the values within the array, which do not exist.
    And besides that, array = num.nextInt(); gives error.

    To get input from user, you should do this (using your variable names);

    num = scan.nextInt();

    Once you define an array (int array [] = new int[0]; ) you can't change its length. It seems you are trying to do that as well.

  3. The Following User Says Thank You to beer-in-box For This Useful Post:

    puny prince (February 14th, 2013)

Similar Threads

  1. Using arrays more efficiently in Java?
    By mjballa in forum Java Theory & Questions
    Replies: 1
    Last Post: February 4th, 2012, 08:53 PM
  2. Please help! Java ARRAYS
    By Score11 in forum Object Oriented Programming
    Replies: 11
    Last Post: January 15th, 2012, 05:45 AM
  3. issue with arrays in Java
    By Sei783 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 9th, 2011, 01:19 AM
  4. New To Java (Help with arrays)
    By SilentPirate in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 16th, 2010, 05:48 AM
  5. Details about 'CopyTo' of Arrays in Java
    By Fendaril in forum Collections and Generics
    Replies: 18
    Last Post: November 13th, 2008, 08:31 AM