For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2006 > Using Sacnner's nextFloat()









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using Sacnner's nextFloat()
sandie

2006-03-25, 10:02 pm

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

2006-03-25, 10:02 pm

On 25 Mar 2006 10:48:07 -0800, "sandie" <amanda772007@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>(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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green

2006-03-25, 10:02 pm

On 25 Mar 2006 10:48:07 -0800, "sandie" <amanda772007@yahoo.com>
wrote, quoted or indirectly quoted someone who said :

>(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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
sandie

2006-03-25, 10:02 pm


Roedy Green wrote:
> On 25 Mar 2006 10:48:07 -0800, "sandie" <amanda772007@yahoo.com>
> wrote, quoted or indirectly quoted someone who said :
>
>
> I see two alternatives,
>
> 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.
>
> In most cases 1 would be preferable.
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.


sandie

2006-03-25, 10:02 pm

[..]

>
> 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
> *
> * @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
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com