I'm writing a program which accepts the price of a tour. A tour is priced between 29.95 and 249.99. It's supposed to repeat until a valid price is entered... However, even when it is, the program continues to repeat. Here's what I have so far.

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

public class TourPrices
{
   public static void main(String args[])
   {

   Scanner scan = new Scanner(System.in);
   int tourPrice = 0;
   boolean ok;

   do
   {
      ok = true;
      try
      {
         System.out.println("Enter a score");
         if(tourPrice < 29.95 || tourPrice >= 249.99)
	 tourPrice = scan.nextInt();
         ok = false;
      }
         catch(InputMismatchException e)
	 {
	    ok = false;
	    scan.nextLine();
         }
   }
   while(!ok);
      System.out.println("The valid price is " + tourPrice);

   }	
}

How can I modify it so it ends when a valid price is entered?

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

public class TourPrices
{
   public static void main(String args[])
   {

   Scanner scan = new Scanner(System.in);
   int tourPrice = 0;
   boolean ok = false;

   do
   {
      try
      {
         System.out.println("Enter a score");
         tourPrice = scan.nextInt();
         if(tourPrice >= 29.95 && tourPrice <= 249.99)
         ok = true;
      }
      catch(InputMismatchException e)
      {
	    scan.nextLine();
       }
   }
   while(!ok);
      System.out.println("The valid price is " + tourPrice);

   }	
}

...

Thank you! That helped me a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.