0

I am filling a Double Array with a for-loop, the loop runs for how many laps the user enters on line 4. However, as I demonstrate in my picture included:

I enter 3 laps and the question prompts 3 times however my data validation eats one of the inputs I am looking for.

How do I fix this? I feel like its something very simple.

import java.util.Scanner;
public class JK_FINALPRAC1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Time-Trial Statistics Program\n");
        System.out.println("This program will ask you to input split-times for each lap or length of a race. The ");
        System.out.println("program will then calculate some basic statistics from the split-times.\n");

        System.out.print("Enter the number of laps or lengths in the race: ");
        int arraySize = sc.nextInt();
        Double[] lapArray = new Double[arraySize];
        System.out.println("Now input the elapsed time (split time) in seconds for each lap/length of the race.");
        int index = 1;
        double currentNum = 0.0;
        for(int i=0;i<arraySize;i++){
            System.out.print("Time for lap or length #" + index + ": ");
            currentNum = sc.nextDouble();
            if(currentNum > 0 && currentNum < 61){
            lapArray[i] = currentNum;
            index++;
            }else if(currentNum<0 || currentNum >60){
            System.out.println("Invalid input! Time must be between 0 and 60.");
            }
        }

    }

Output

2 Answers 2

1
        double currentNum = 0.0;
        while (index < arraySize) {
            System.out.print("Time for lap or length #" + (index + 1) + ": ");
            currentNum = sc.nextDouble();
            if (currentNum > 0 && currentNum < 61) {
                lapArray[index] = currentNum;
                index++;
            } else if (currentNum < 0 || currentNum > 60) {
                System.out.println("Invalid input! Time must be between 0 and 60.");
            }
        }

Since you're running for-loop, i is getting incremented every time (even if your validation fails).

Sign up to request clarification or add additional context in comments.

Comments

1

It's very simple: in order to be sure you have inserted a double without eat a lap, put your scanner input line inside a while-loop

 do{
    double x = scanner.nextDouble(); 
 }while(x < 0 || x > 60);

This will not afford for-loop increasing counter, unless the condition of do-while is false.

In order to print validation, too:

 double x = scanner.nextLine();
 while( x < 0 || x > 60){
      //print message
      //scanner again
 }

2 Comments

How do i still have my Validation rules printed (ex. Invalid input! Time must be between 0 and 60.) if they do go out of bounds?
You mean that you wanna print validation rule every time double insrted is wrong? Then I suggest you to use a while-loop and not a do-while (this in order to not print your validation also the first input). You just need to make the input, check it in a while, and if it is wrong, print your validation rule and remake input. I am gonna edit my post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.