Results 1 to 2 of 2
- 03-19-2012, 12:03 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
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; }
- 03-19-2012, 01:11 AM #2
Re: How to store multiple user inputs - should be a simple answer but I am lost.
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.automatically assigns values to each new variable (NumX).
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
-
Controlling User Inputs
By time-space in forum New To JavaReplies: 5Last Post: 05-30-2011, 02:50 PM -
Sum positive numbers using 10 inputs from user
By pvictory1 in forum New To JavaReplies: 15Last Post: 10-10-2010, 01:30 AM -
if statement with multiple inputs?
By soc86 in forum New To JavaReplies: 3Last Post: 01-20-2009, 04:44 AM -
[SOLVED] How do I make user inputs appear bolded??
By mjm1189 in forum EclipseReplies: 0Last Post: 09-13-2008, 12:59 AM -
Java program that stores user inputs
By staticy2003 in forum Advanced JavaReplies: 6Last Post: 01-24-2008, 07:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks