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

Thread: Number Programming NumberFor

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Number Programming NumberFor

    Hi,
    I've only just started java and I am trying to to make a program so that when a any line of digits are added it prints the count N for the numbers (how many numbers are in the line), the minumum and maximum of the line of numbers, their sum, and the average of all the numbers. Each of these specifications has to be printed on a new line.
    The program has a to be called NumberFor
    eg.
    >NumberFor 22.0 42.5 15.2 -2 6
    4
    -2
    42.5
    77.1
    19.275

    Currently I have:

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

    double value = Double.parseDouble(args[0]);
    double sum = 0.0;

    //count N

    // first value read initialized min and max
    double max = StdIn.readDouble();
    double min = max;

    // read in the data, keep track of min and max
    {
    if (value > max) max = value;
    if (value < min) min = value;
    }

    System.out.println(+ min);
    System.out.println(+ max);

    // sum of values
    double N = Double.parseDouble(args[0]);
    for (double i = 0.0; i < N; i++) {
    sum += value;
    }

    System.out.println(+ sum);


    //average
    double cnt = 0;
    while (!StdIn.isEmpty()){
    sum += value;
    cnt ++;
    }
    double average = sum / cnt;
    System.out.println(+ average);
    }

    }

    and the only output i can get to work is the max
    Read more at numbers programming - CodingForums.com


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

    Default Re: Number Programming NumberFor

    Quote Originally Posted by programming-beginner View Post
    Hi
    Hi.

    Quote Originally Posted by programming-beginner View Post
    I've only just started java and I am trying to to make a program...
    Well, no one was born knowing this stuff. Have you gone through any introductory material in a textbook or class notes or on-line tutorials or anything?

    Quote Originally Posted by programming-beginner View Post
    trying to to make a program so that when a any line of digits are added ...
    Trying to decipher the rest of your post, I infer that by "line of digits are added" you mean that your program is supposed to process command-line arguments.

    So, for example if you execute

    java NumberFor 22.0 42.5 15.2 -2 6


    You want the program to treat 22.0, 42.5, 15.2, -2, and 6 as numbers. (Find min, max, sum, average of these five values, right?)

    Well, when your main method is defined like this:
        public static void main(String [] args)
    the command-line arguments are stored in an array of Strings named args

    Here's how you "get at" the arguments in a program:
    //
    // PrintArgs.java
    //
    public class PrintArgs {
        public static void main(String [] args) {
            System.out.println("Number of command-line arguments = " + args.length);
            for (int i = 0; i < args.length; i++) {
                System.out.println("args[" + i + "] = " + args[i]);
            }
        } // End main
    } // End class definition

    Here's a run. Program output is blue, user input is black:

    java PrintArgs 22.0 42.5 15.2 -2 6
    Number of command-line arguments = 5
    args[0] = 22.0
    args[1] = 42.5
    args[2] = 15.2
    args[3] = -2
    args[4] = 6


    Now that you know how to get to them, how would you complete your program?

    Maybe wiith a loop that goes through all of the arguments, parsing each into a numeric value so that the values can be added together (one at a time inside the loop). Or some such thing.



    Can you take it from there?



    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Germany
    Posts
    20
    My Mood
    Bored
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Number Programming NumberFor

    Quote Originally Posted by programming-beginner View Post
    // read in the data, keep track of min and max
    {
    if (value > max) max = value;
    if (value < min) min = value;
    }
    You have braces{} but nothing before it, like if, while, for.

Similar Threads

  1. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  2. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  3. programming the Fibonacci number
    By johnmerlino in forum Algorithms & Recursion
    Replies: 3
    Last Post: July 25th, 2012, 11:22 PM
  4. How To Ask the User to Enter Another Number Using the Number 1
    By Pettsa in forum Object Oriented Programming
    Replies: 6
    Last Post: May 3rd, 2012, 03:44 AM
  5. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM

Tags for this Thread