Run a Program After Another in One Java File

Peter Zēng

I have made programs with the first one being a finder of the largest user input; the second one multiplies the user input. I would like to combine these programs into one so that after the first one finishes running, the second one begins. However, I have no idea of how to do this as I am new to programming.

Code for first

import java.util.Scanner; class Army{

private static Scanner in;

public static void main(String[] args){
    // declares an array of doubles
    double[] inputArray = new double[10];
    // allocates memory for 10 doubles
    System.out.println("Please enter ten numbers.");
    try {
        in = new Scanner(System.in);
        for (int j = 0; j < inputArray.length ; j++) {
            inputArray[(int) j] = in.nextDouble();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    double maxValue = inputArray[0];
    for (int i = 0; i < inputArray.length; i++) {
       if (inputArray[i] > maxValue) {
            maxValue = inputArray[i];
             // removed print from here
            } else {
             // removed print from here too
            }
      }
    System.out.println("The largest number is "+maxValue+"."); //print max from outside the loop;
            // optional: display only one answer.
}

}

Code for second

import java.util.Scanner; class Battles{

private static Scanner in;

public static void main(String[] args){
    double[] inputArray = new double[10];
    System.out.println("Please enter ten numbers.");
    in = new Scanner(System.in);
    for (int j = 0; j < inputArray.length ; j++) {
        inputArray[(int) j] = in.nextDouble();
        }
    double product = inputArray[0];
    for (int i = 0; i < inputArray.length; i++) {
        product*=inputArray[i];
        }
    System.out.println("The product of these numbers is "+product+".");
}

}

Juned Ahsan

Make use of methods, create two different methods. Move your current two main method code into these two new methods. And from you main ,call new methods one after the other.

public static void main() {
  findLargest();
  multiplyInputs();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to run multiple python file in a folder one after another

From Dev

Eclipse: Chain many 'Run as Java Application' one after another

From Dev

how to run function after another one completes

From Dev

Run one rake task after another completes

From Dev

Run multiple scripts one after another in bash

From Dev

Run one function after another: callback issue

From Dev

RethinkDB - Run query one after another

From Dev

How to open/run java program using cmd in another java program

From Dev

Is there a way to run a java program from within another java program?

From Dev

CodeCompiler - Compile and Run Java Program from another Java Program

From Dev

Run java program with dependencies after compilation with Gradle

From Dev

Run Java program after exception encountered

From Dev

Java copying one file into another

From Dev

Compile and Run Scala File within a Java Program

From Dev

How to run a python program in one folder and import and run a python program from another folder

From Dev

print the sentences of a file one after another

From Dev

How to run java code from another java program?

From Dev

im trying to run a program that copy the text from one file to the other

From Dev

How to run a java from another file?

From Dev

SAS - Passing date from one file to another program variable

From Dev

Change all file type associations from one program to another

From Dev

How to make a javascript function to run after another one get finished?

From Dev

how to run multiple jobs one after another in jenkins

From Dev

add SKAction to Sprite queue run one after another

From Dev

How do i run a for loop in bash one after another

From Dev

How to load 2 Javascript files async and run one after another?

From Dev

How to run one statement after another in a procedural manner in Javascript?

From Dev

add SKAction to Sprite queue run one after another

From Dev

Do commands in a bash script run in parallel or one after another?

Related Related

  1. 1

    How to run multiple python file in a folder one after another

  2. 2

    Eclipse: Chain many 'Run as Java Application' one after another

  3. 3

    how to run function after another one completes

  4. 4

    Run one rake task after another completes

  5. 5

    Run multiple scripts one after another in bash

  6. 6

    Run one function after another: callback issue

  7. 7

    RethinkDB - Run query one after another

  8. 8

    How to open/run java program using cmd in another java program

  9. 9

    Is there a way to run a java program from within another java program?

  10. 10

    CodeCompiler - Compile and Run Java Program from another Java Program

  11. 11

    Run java program with dependencies after compilation with Gradle

  12. 12

    Run Java program after exception encountered

  13. 13

    Java copying one file into another

  14. 14

    Compile and Run Scala File within a Java Program

  15. 15

    How to run a python program in one folder and import and run a python program from another folder

  16. 16

    print the sentences of a file one after another

  17. 17

    How to run java code from another java program?

  18. 18

    im trying to run a program that copy the text from one file to the other

  19. 19

    How to run a java from another file?

  20. 20

    SAS - Passing date from one file to another program variable

  21. 21

    Change all file type associations from one program to another

  22. 22

    How to make a javascript function to run after another one get finished?

  23. 23

    how to run multiple jobs one after another in jenkins

  24. 24

    add SKAction to Sprite queue run one after another

  25. 25

    How do i run a for loop in bash one after another

  26. 26

    How to load 2 Javascript files async and run one after another?

  27. 27

    How to run one statement after another in a procedural manner in Javascript?

  28. 28

    add SKAction to Sprite queue run one after another

  29. 29

    Do commands in a bash script run in parallel or one after another?

HotTag

Archive