CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Jul 2010
    Posts
    9

    Help with validating data

    I'm trying to do this: http://docs.google.com/leaf?id=0Bzuf...YjJmYTdh&hl=en

    In the requirements, if the user doesn't enter anything at the continue prompt, the program is supposed to display an error message. I tried using a NullPointerException to do this, but it didn't work. When I run the program, if I don't enter anything at the continue prompt, nothing happens.

    Also, if the user enters 'x' instead of 'y' or 'n', the program displays the appropriate error message, but the continue prompt doesn't reappear.

    Below is my code. Please let me know if you can find where I went wrong:
    Code:
    import java.util.*;
    import java.text.*;
    
    public class Five
    {
    	public static void main(String[] args)
    	{
    		String choice = "y";
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Welcome to the Area and Perimeter Calculator");
    		while (choice.equalsIgnoreCase("y"))
    		{int first=0;
    			System.out.println();
    			double length = getDoubleWithinRange (sc, "Enter length: ", 0, 1000000);
    			double width = getDoubleWithinRange (sc, "Enter width: ", 0, 1000000);
    			double area = length * width;
    			double perimeter = (2 * length) + (2 * width);
    			String results = "Area:\t\t" + area + "\n" + "Perimeter:\t" + perimeter;
    			System.out.print(results);
    			System.out.println();
    //problem around here somewhere
    			try
    			{
    				System.out.println();
    				System.out.print("Continue? (y/n): ");
    				do{
    					if(first==1)
    						System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
    					choice = sc.next();
    					first=1;
    				 }
    				while(! (choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n")))
    				;
    
    				System.out.println();
    			}
    			catch (NullPointerException e)
    			{
    
    				System.out.println("Error! This entry is required. Try again.");
    				sc.next();
    			}
    		}
    	}
    	public static double getDouble(Scanner sc, String prompt)
    		{
    			double d = 0.0;
    			boolean isValid = false;
    			while (isValid == false)
    			{
    				System.out.print(prompt);
    				if (sc.hasNextDouble())
    				{
    					d = sc.nextDouble();
    					isValid = true;
    				}
    				else
    				{
    					System.out.println("Error! Invalid decimal value. Try again.");
    					sc.nextLine();
    				}
    			}
    			return d;
    	}
    	public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
    	{
    		double d = 0.0;
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			d = getDouble(sc, prompt);
    			if (d<=min)
    			{
    				System.out.println("Error! Number must be greater than " + min
    				+ ".");
    			}
    			else if (d >= max)
    			{
    				System.out.println("Error! Number must be less than " + max +
    				".");
    			}
    			else
    				isValid = true;
    		}
    		return d;
    	}
    
    }
    Last edited by galagali; August 4th, 2010 at 11:19 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured