Results 1 to 2 of 2
  1. #1
    busdude is offline Member
    Join Date
    Oct 2008
    Rep Power

    Default How to ask for several integers from the program user?

    I need to write a program in which you ask the user to input a bunch of integers (no set amount, as many as they want) in one line and then the line adds up all the integers that are positive and sets the value of that line equal to the sum.

    So, I was wondering if I need to use a command that specifically sets a variable value to a list, or stack? because we haven't really learned about arrays yet?.

    pasted the assignment:

    (10 marks) A set of data is to be read and manipulated. You can read the data from standard input (i.e., from a user typing in a console window, or from a file using input redirection) or from JOptionPane dialog boxes. The number of lines is unknown. Each line of data contains
    a line number, like 1., 2., 3., etc.
    an integer n,
    followed by n data values which are integers.
    Write a complete Java program, consisting of only a class and a main method to read this data and print for each line
    the line number followed by the product of the positive data values if there are any,
    otherwise the line number followed by zero.
    For example, if the input data is

    1. 4 5 -3 2 7
    2. 5 1 -3 5 6 -80
    3. 0
    4. 1 -7
    5. 2 6 -6

    then your program would print
    1. 70
    2. 30
    3. 0
    4. 0
    5. 6

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    The arguments are returned in an array of String values.
    Just parse the values using the usual Integer.parseString()

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 05:45 PM
  2. Finding Median of X Integers
    By Hasan in forum New To Java
    Replies: 3
    Last Post: 08-12-2008, 02:06 PM
  3. Replies: 4
    Last Post: 04-16-2008, 01:26 PM
  4. Java program that stores user inputs
    By staticy2003 in forum Advanced Java
    Replies: 6
    Last Post: 01-24-2008, 07:46 PM
  5. Random Integers
    By in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts