-
August 4th, 2010, 11:13 AM
#1
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.
-
August 4th, 2010, 01:00 PM
#2
Re: Help with validating data
. I tried using a NullPointerException to do this, but it didn't work.
It doesn't work because nothing will throw a NullPointerException. If the user presses enter without typing any letters the nextLine() method will return an empty string so you need to test for that ie if the string length is 0..
When I run the program, if I don't enter anything at the continue prompt, nothing happens.
Try using the Scanner's nextLine() method rather than the next() method. The next() method returns the next token, if you haven't entered anything then there is no token to return. Also next() doesn't remove the newline character so once all the tokens up to the newline character have been returned it will have nothing else to return and so will block.
-
August 6th, 2010, 06:30 PM
#3
Re: Help with validating data
I had some success when I changed the code according to your suggestions and put in a few new twists. This time, the program reprompted the user after displaying the error message, but when the user doesn't enter any value, nothing happens. Here is my modified code:
Code:
boolean cool = true;
while (true)
{
String newprompt = "Continue? (y/n): ";
System.out.print(newprompt);
choice = sc.next();
String theString = sc.nextLine();
if (choice.equals(null))
{
System.out.println("Error! This entry is required. Try again.");
cool = true;
}
else if (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
{
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
cool = true;
}
else if (choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n"))
{
break;
}
}
-
August 7th, 2010, 06:16 AM
#4
Re: Help with validating data
Always read the API docs for the methods you are trying to use. The docs for the next() method say:
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan,
The last sentence says it all.
-
August 7th, 2010, 10:23 AM
#5
Re: Help with validating data
Oh that explains it! Thank you for letting me know! I modified the code again, and it's ALMOST working perfectly.
This time, the appropriate error messages were displayed at the right time, but there was one extra thing. Let me write down the output I got:
Welcome to the Area and Perimeter Calculator
Enter length: 4 --user inputs value and presses 'enter'
Enter width: 5 --user inputs value and presses 'enter'
Area: 20.0-output value
Perimeter: 18.0-output value
--Here comes the extra part
Continue? (y/n): Error! This entry is required! Try again.
Continue? (y/n): n
Press any key to continue...
The extra part is that error message that comes without me inputing any value at the continue prompt. The second continue prompt that comes works properly (displays the proper error messages, etc.), but I'm not sure why the first error message appears spontaneously.
Here is the specific part that validates the continue prompt:
Code:
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
{
if (choice.equals(""))
{
System.out.println("Error! This entry is required. Try again.");
System.out.print("Continue? (y/n): ");
}
else
{
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
System.out.print("Continue? (y/n): ");
}
choice = sc.nextLine();
}
Here is the code for the entire program:
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"))
{
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();
System.out.println();
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
{
if (choice.equals(""))
{
System.out.println("Error! This entry is required. Try again.");
System.out.print("Continue? (y/n): ");
}
else
{
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
System.out.print("Continue? (y/n): ");
}
choice = sc.nextLine();
}
}
}
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;
}
}
-
August 8th, 2010, 12:00 PM
#6
Re: Help with validating data
Your main method calls the nextDouble() method calls the scanner's nextDouble() method which returns the next double and leaves the carriage return in the scanner's buffer(Why do I feel like I'm repeating myself here!). Your main method then calls the scanner's nextLine() method which immediately returns because there is a carriage return in the buffer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|