Results 1 to 2 of 2
  1. #1
    doowstados is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default How to store multiple user inputs - should be a simple answer but I am lost.

    How do I store a set of continuous values in Java? For example, if I needed a user to input various values in a non-sensical order, then have the user enter a sentinel value, how could I store the values entered without repeating:

    double Num1 = readDouble("Gimme a number:");
    double Num2 = readDouble("Gimme another number:");
    double Num3 = readDouble("Gimme another number:");
    ... etc

    In a sense I want to create a method that automatically assigns values to each new variable (NumX).

    Here is my code so far:

    Java Code:
    // The purpose of this program is to find the range of user inputted values. It will continue to read values until user inputs "0", at which //time the program will find the minimum and maximum values associated with that data set.
    
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    
    	public void run() {
    		
    		
    		
    		println("This program finds the range of the numbers entered. Enter 0 when finished entering numbers. Do not enter 0 until finished.");
    		double userValue = 0;
    		getInput(); //Takes user input, saves input to a variable
    		findMin(); // Finds the minimum value from that set of variables
    		findMax(); // Finds the maximum value from that set of variables
    		dispMinMax(); // Displays min/max values for user.
    		
    	}
    		private void getInput() {
    			 while (true) {
    				 double userValue = readDouble("Please insert values:");
    				 if (userValue == sentinel) break;
    				 // This is where I need help
    			 }	
    		}
    		                                // I'll get around to this part after I finish up getInput().
    		private void findMin() {
    			
    		}
    		
    		private void findMax() {
    			
    		}
    		
    		private void dispMinMax() {
    			
    		}
    		
    		private static final int sentinel = 0;
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: How to store multiple user inputs - should be a simple answer but I am lost.

    automatically assigns values to each new variable (NumX).
    You will have to use an array or an arraylist to have new places to store what the user enters. The names of variables are assigned when you type in the source.
    Write a loop, read the user's input and store it into the array or arraylist.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Controlling User Inputs
    By time-space in forum New To Java
    Replies: 5
    Last Post: 05-30-2011, 02:50 PM
  2. Sum positive numbers using 10 inputs from user
    By pvictory1 in forum New To Java
    Replies: 15
    Last Post: 10-10-2010, 01:30 AM
  3. if statement with multiple inputs?
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 01-20-2009, 04:44 AM
  4. Replies: 0
    Last Post: 09-13-2008, 12:59 AM
  5. Java program that stores user inputs
    By staticy2003 in forum Advanced Java
    Replies: 6
    Last Post: 01-24-2008, 07:46 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
  •