Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Using Sacnner's nextFloat()

Thread view: 
sandie - 25 Mar 2006 19:48 GMT
   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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.