0

My code works fine except for the part where I am supposed to ask the user if they would like to quit the program. It's a simple y/n that should trigger the entire program to repeat if 'n' is entered and terminates when 'y' is entered. I know i need to use a while loop but i just can't figure out exactly how I need to write it in code so that it works like expected.

import java.util.*;

public class MilitaryTime 

{

public static void main (String [] args)    
{

            Scanner in = new Scanner(System.in);
            int hour1, minute1, hour2, minute2;

            System.out.print("Please enter the first time: ");
            int time1 = in.nextInt();
            System.out.println();
            hour1 = time1 / 100;
            minute1 = time1 % 100;

            System.out.print("Please enter the second time: ");
            int time2 = in.nextInt();
            System.out.println();
            hour2 = time2 / 100;
            minute2 = time2 % 100;

            System.out.println("Time elapsed: " + (Math.abs(hour1 - hour2) + " Hours, " + (Math.abs(minute1 - minute2) + " Minutes \n")));

            System.out.print("Do you want to quit? (y/n): ");
            String answer = in.next();

            while (answer != "n")
            {

            }

    }

}
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
user3765527
  • 11
  • 1
  • 4

2 Answers2

1

You should probably split your code in (at least) two different methods, but I'll just try to point out a way to achieve what you want with minimal changes:

public static void main (String [] args)    
{
    String answer = null;  // you have no answer yet...
    do {   // ... but you want to execute your stuff at least once
        Scanner in = new Scanner(System.in);
        int hour1, minute1, hour2, minute2;

        System.out.print("Please enter the first time: ");
        int time1 = in.nextInt();
        System.out.println();
        hour1 = time1 / 100;
        minute1 = time1 % 100;

        System.out.print("Please enter the second time: ");
        int time2 = in.nextInt();
        System.out.println();
        hour2 = time2 / 100;
        minute2 = time2 % 100;

        System.out.println("Time elapsed: " + (Math.abs(hour1 - hour2) + " Hours, " + (Math.abs(minute1 - minute2) + " Minutes \n")));

        System.out.print("Do you want to quit? (y/n): ");
        answer = in.next();
    } while (!answer.equalsIgnoreCase("n"));

}
Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
0

Until then rerun the main method.

while (answer.equals("n")) // comment by drewmoore; its helpfull for strings..
{
    // you said, program should repeat if n is pressed. 
    // so, if answer is equal to n then execute the main function.
    main();
}

This would execute until the user presses some other button.

Secondly, you don't need to use while loop. This can be done using if too.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • (1) Where exactly would the code that invokes `main()` be located ? I assume you wanted to advice that all that code should be extracted in a method other than `main`. (2) Since the `answer` is only available after the first iteration, this can only be achieved with a `do...while` loop. – Costi Ciudatu Jun 22 '14 at 21:21