Jingleheimer
Member
I need to read 4 DOUBLE values from each line of input from the user. (i.e. "1.2 3 4.56 0). The user keeps entering line after line until all input values are 0. (i.e. "0 0 0 0"). The program then calculates one DOUBLE value from each line using a specified equation in the program (using the four values) and outputs after all the inputs are done. For example, the program would look like this:
Input:
1 2 3 4
3 4.5 0.678 0
1 1 1 1
3 0 0 0
1.1 2.2 3.3 4.4
0 0 0 0
Output:
15.2
17.983
1
1.34
2.7
We are not allowed to use arrays for the program as the user is able to input as many lines as they like. We should be using WHILE loops. I already know how to parse the doubles and read in the input. My question is: After reading in the values from the first line, how do I save the output it should have until the user is done inputting. For example, if I knew the user was only going to input five lines, then I could just declare five output variables. However, I don't know how many lines the user will input so I am having difficulty.
Here is what I have so far:
import java.io.*;
import java.util.StringTokenizer;
import java.text.*;
import java.lang.Math;
public class Assignment4 {
//--------------------------------------------------------------------
// Reads four inputs from each line (weight of the plane, direction of
// air flow, acceleration of the vehicle, and the constant of
// airflow for the wing.
//--------------------------------------------------------------------
public static void main (String[] args) throws IOException {
double weight, airFlowDegrees, acceleration, constantAirflowWing;
String str;
weight = 1;
airFlowDegrees = 1;
acceleration = 1;
constantAirflowWing = 1;
BufferedReader stdin =
new BufferedReader (new InputStreamReader (System.in));
//----------------------------------------------------------
// If any line is 0 0 0 0, then end the program at that
// that line. For the other lines, analyze the inputs and calculate
// the outputs for each line.
//----------------------------------------------------------
while (weight != 0 || airFlowDegrees != 0 || acceleration != 0 || constantAirflowWing != 0)
{
StringTokenizer tokenizerDouble = new StringTokenizer (stdin.readLine ());
str = tokenizerDouble.nextToken ();
weight = Double.parseDouble (str);
str = tokenizerDouble.nextToken ();
airFlowDegrees = Double.parseDouble (str);
str = tokenizerDouble.nextToken ();
acceleration = Double.parseDouble (str);
str = tokenizerDouble.nextToken ();
constantAirflowWing = Double.parseDouble (str);
}
Input:
1 2 3 4
3 4.5 0.678 0
1 1 1 1
3 0 0 0
1.1 2.2 3.3 4.4
0 0 0 0
Output:
15.2
17.983
1
1.34
2.7
We are not allowed to use arrays for the program as the user is able to input as many lines as they like. We should be using WHILE loops. I already know how to parse the doubles and read in the input. My question is: After reading in the values from the first line, how do I save the output it should have until the user is done inputting. For example, if I knew the user was only going to input five lines, then I could just declare five output variables. However, I don't know how many lines the user will input so I am having difficulty.
Here is what I have so far:
import java.io.*;
import java.util.StringTokenizer;
import java.text.*;
import java.lang.Math;
public class Assignment4 {
//--------------------------------------------------------------------
// Reads four inputs from each line (weight of the plane, direction of
// air flow, acceleration of the vehicle, and the constant of
// airflow for the wing.
//--------------------------------------------------------------------
public static void main (String[] args) throws IOException {
double weight, airFlowDegrees, acceleration, constantAirflowWing;
String str;
weight = 1;
airFlowDegrees = 1;
acceleration = 1;
constantAirflowWing = 1;
BufferedReader stdin =
new BufferedReader (new InputStreamReader (System.in));
//----------------------------------------------------------
// If any line is 0 0 0 0, then end the program at that
// that line. For the other lines, analyze the inputs and calculate
// the outputs for each line.
//----------------------------------------------------------
while (weight != 0 || airFlowDegrees != 0 || acceleration != 0 || constantAirflowWing != 0)
{
StringTokenizer tokenizerDouble = new StringTokenizer (stdin.readLine ());
str = tokenizerDouble.nextToken ();
weight = Double.parseDouble (str);
str = tokenizerDouble.nextToken ();
airFlowDegrees = Double.parseDouble (str);
str = tokenizerDouble.nextToken ();
acceleration = Double.parseDouble (str);
str = tokenizerDouble.nextToken ();
constantAirflowWing = Double.parseDouble (str);
}