Java help with repeating method


#1

Hi

I want to write a program that keeps track of how many times a bus is late.

So the user is asked to enter an int value, indicating how many minutes late.

Once a negative int is entered, the program should stop.

What I’m having trouble with is making the program repeat only for inputs of 0 or more.

The program repeats regardless of what int is inputted.

I did something like below:

import java.util.Scanner;

public class LateBus {

    public static void main(String[] args) {
        int enter_minutes = enterMinutes();
        loop(enter_minutes);
    }

    public static int enterMinutes() {
        Scanner enter = new Scanner(System.in);
        System.out.print("How many minutes late was the bus? ");

        int late = enter.nextInt();
        return late;
    }

    public static void loop(int a) {
        while (a >= 0) {
            enterMinutes();
        }
    }

}

#2

Ask yourself what needs to happen to accomplish that and then really check if those things are happening, similar to how you might need to pop open the hood of a car to figure out what has gone wrong when the engine fails. Look and compare.

You’ve also got some other things going on there, multiple methods, user input, conversions - making another program containing only the parts you try to get to work would reduce the amount of things you have to look at.