• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java Help for Newbie

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);
}

 
you could save the inputs in a string, so:

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

could be saved as "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"

then you could calculate the output using by parsing through the sting in groups of four.
 
Use an ArrayList to store the inputs. The Java Collections API is designed to be accessed by interface. So the correct way to declare an ArrayList variable would be:

List userInput = new ArrayList();

I would wrap each line of parsed input in a simple class (basically serving as a C struct). Then add instances of that class into the ArrayList as you go along.
 
Back
Top