Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter one character for degree scale (C or
F).");
String strScale2 = keyboard2.next();
char scale2 = (strScale2.toUpperCase()).charAt(0);
(1) In the above scenario, if the user doesn't enter anything and want
to use default value, how would I go about making the program contiue
instaed of it sitting and waiting for the input?
(2) Also, if the user enters anyhting other than 'c''C', 'f', 'F', I
need to ask the user to enter a character from these 4 choices only.
How would I go about it?
Roedy Green - 25 Mar 2006 20:02 GMT
>(1) In the above scenario, if the user doesn't enter anything and want
>to use default value, how would I go about making the program contiue
>instaed of it sitting and waiting for the input?
I see two alternatives,
1. entering some character to indicate the default, e.g. enter or
space.
2. having a timeout. If the user can't make up his mind in 10 seconds,
you choose the default for him.
In most cases 1 would be preferable.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
sandie - 26 Mar 2006 00:54 GMT
> >(1) In the above scenario, if the user doesn't enter anything and want
> >to use default value, how would I go about making the program contiue
[quoted text clipped - 4 lines]
> 1. entering some character to indicate the default, e.g. enter or
> space.
Okay, that approach is more suitable to me for tis program.
Thanks.
> 2. having a timeout. If the user can't make up his mind in 10 seconds,
> you choose the default for him.
[quoted text clipped - 3 lines]
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 25 Mar 2006 20:05 GMT
>(2) Also, if the user enters anyhting other than 'c''C', 'f', 'F', I
>need to ask the user to enter a character from these 4 choices only.
>How would I go about it?
You tell the user his choices and what they mean, (don't bother with
both lower and upper case. He assumes that unless told otherwise.).
When you get the answer, you check if it is in the list, using code
like this from http://mindprod.com/jgloss/products1..html#COMMON11
StringTools
/**
* Ensure the string contains only legal characters
*
* @param candidate string to test.
* @param legalChars characters than are legal for candidate.
*
* @return true if candidate is formed only of chars from the
legal set.
*/
public static boolean isLegal( String candidate, String legalChars
)
{
for ( int i = 0; i < candidate.length(); i++ )
{
if ( legalChars.indexOf( candidate.charAt( i ) ) < 0 )
{
return false;
}
}
return true;
}
If it is false you loop back and ask the question again.
You can do this most easily with a while loop. See
http://mindprod.com/jgloss/jcheat.html#LOOPS

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
sandie - 26 Mar 2006 00:55 GMT
[..]
> When you get the answer, you check if it is in the list, using code
> like this from http://mindprod.com/jgloss/products1..html#COMMON11
> StringTools
Thanks for this too.
> /**
> * Ensure the string contains only legal characters
[quoted text clipped - 25 lines]
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.