Home > database >  How do i stop program from running until user is finished inputting?
How do i stop program from running until user is finished inputting?

Time:11-21

I want the user to be able to input the amount of numbers they specified BEFORE the code keeps running. Currently, the user is only able to input one number before the code continues. How do i keep the code from running until a certain amount of numbers are inputted?

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);
    System.out.print("\nEnter the amount of numbers you want the array to store: ");
    // reads # of # user wants to enter
    n = sc.nextInt();
    // creates an array in the memory of length 10
    int[] array = new int[10];
    System.out.println("Enter " n " numbers ");
    for (int i = 0; i < n; i  ) {
      // reading array elements from the user
      array[i] = sc.nextInt();
      
    double sum = 0;
    double mode = 0;
    double variance = 0;
    double deviation = 0;

    for (i = 0; i < 2; i  )
      sum = sum   array[i];
//MEAN
    mode = sum / 5;

    sum = 0;
    for (i = 0; i < 2; i  ) {
      sum = sum   Math.pow((array[i] - mode), 2);
    }
//VARIANCE
    variance = sum / 5;
//DEVIATION
    deviation = Math.sqrt(variance);
      //Standard
      System.out.println("Standard Deviation is: "   deviation);
      //mode
      System.out.println("Mode is:"   mode);
        //Variance
      System.out.println("Variance is: "   variance);
    }
  }
}




I tried to let the user decide how many numbers should be in the array, then input that many numbers. However, when i run the code, it doesn't give them enough time to type in the numbers. I need a way to stop this from happening.

CodePudding user response:

The first thing I discovered is that at no time are you using the variable "n" to create a constant value array, so it will always be an array of 10 elements.

One recommendation that I give you is that you do not use the "Scanner" class because it can give you problems if you ask the user for different types of data. Instead it uses BufferedReader because it is more direct. Here is an example of your exercise but fixed:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

    private static Number getNumber(BufferedReader reader) throws IOException {
        // Get input content
        String line = reader.readLine();
        return Double.parseDouble(line);
    }

    public static void main(String[] args) throws IOException {
        // Auto closeable elements
        try (InputStreamReader reader = new InputStreamReader(System.in);
             BufferedReader buffered = new BufferedReader(reader)) {
            // Print message
            System.out.print("The amount of numbers you want: ");
            // Program variables here
            final int totalSize = getNumber(buffered).intValue();
            int[] arrayStore = new int[totalSize];

            // Iterate all elementos of the array
            for (int i = 0; i < totalSize;   i) {
                // Another message
                System.out.print("Insert one number: ");
                // Ask for numbers
                int nextNumber = getNumber(buffered).intValue();
                arrayStore[i] = nextNumber;
            }

            // TODO: Your logic program here
            System.out.println(Arrays.toString(arrayStore));
        }
    }

}

If you were able to notice, the method that asks for the numbers does not check if the content is really a number, for that change the behavior of the getNumber method to the following:

private static Number getNumber(BufferedReader reader) throws IOException {
    Number result = null;
    do {
        try {
            result = Double.parseDouble(reader.readLine());
        } catch (NumberFormatException e) {
            System.err.println("The inserted content is not a valid number");
        }
    } while (result == null);
    return result;
}

I hope it helps you in some way

  •  Tags:  
  • java
  • Related