简体   繁体   中英

how to only allow one argument at a time

I am allowing the user to enter numbers via command line. I would like to make it so when the user enters more then one number on the command line at a time it displays a message asking for one number then press enter. then carries on.

here is my code. If someone could show me how to implement this I would appreciate it.

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

class programTwo
{   
    private static Double calculate_average( ArrayList<Double> myArr )
    {
        Double sum = 0.0;
        for (Double number: myArr)
        {
            sum += number;
        }
        return sum/myArr.size(); // added return statement
    }

    public static void main( String[] args )
    {
        Scanner scan = new Scanner(System.in);
        ArrayList<Double> myArr = new ArrayList<Double>();
        int count = 0;
        System.out.println("Enter a number to be averaged, repeat up to 20 times:");
        String inputs = scan.nextLine();

        while (!inputs.matches("[qQ]") )
        {
            if (count == 20)
            {
                System.out.println("You entered more than 20 numbers, you suck!");
                break;
            }

            Scanner scan2 = new Scanner(inputs); // create a new scanner out of our single line of input
            try{
                myArr.add(scan2.nextDouble());
                count += 1;
                System.out.println("Please enter another number or press Q for your average");
            }
            catch (InputMismatchException e) {
                System.out.println("Stop it swine! Numbers only! Now you have to start over...");
                main(args);
                return;
            }      

            inputs = scan.nextLine();
        }
        Double average = calculate_average(myArr);
        System.out.println("Your average is: " + average);
    }
}

As suggested in the comments to the question: Just do not scan the line you read for numbers, but parse it as a single number instead using Double.valueOf (I also beautified the rest of your code a little, see comments in there)

public static void main( String[] args )
{
    Scanner           scan  = new Scanner(System.in);
    ArrayList<Double> myArr = new ArrayList<Double>();
    int               count = 0;

    System.out.println("Enter a number to be averaged, repeat up to 20 times:");

    // we can use a for loop here to break on q and read the next line instead of that while you had here.
    for (String inputs = scan.nextLine() ; !inputs.matches("[qQ]")  ; inputs = scan.nextLine())
    {
        if (count == 20)
        {
            System.out.println("You entered more than 20 numbers, you suck!");
            break;
        }
        try{
            myArr.add(Double.valueOf(inputs));
            count++; //that'S even shorter than count += 1, and does the exact same thing.
            System.out.println("Please enter another number or press Q for your average");
        }
        catch (NumberFormatException e) {
            System.out.println("You entered more than one number, or not a valid number at all.");
            continue; // Skipping the input and carrying on, instead of just starting over. 
                      // If that's not what you want, just stay with what you had here
        }      

    }
    Double average = calculate_average(myArr);
    System.out.println("Your average is: " + average);
}

(Code untested, so there may be errors in there. Please notify me if you got one ;))

String[] numbers = inputs.split(" ");
if(numbers.length != 1){
    System.out.println("Please enter only one number");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM