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.

How to skip when there is no input for Scanner object

Thread view: 
sandie - 25 Mar 2006 22:47 GMT
I want to enter a float value and the unit for a Tmeperature object but
when not given, want to use default values.  So I am testing default
constructor but do not know how to handle Scanner when no input.

Here is the code; the program hangs waitng for input.  Any help
appreciated.

   float temp1=0;
   Scanner keyboard1 =  new Scanner(System.in);
   System.out.println("Enter a float value with 2 decimal position for
a temperature.");
   if (keyboard1.hasNextFloat())
        temp1 = keyboard1.nextFloat();
   else
    keyboard1.skip("");

   String strScale1 ="C";
   System.out.println("Enter one character for degree scale (C or
F).");
   if (keyboard1.hasNext())
       strScale1 = keyboard1.next();
   else
    keyboard1.skip("");
   
   char scale1 = (strScale1.toUpperCase()).charAt(0);
Oliver Wong - 28 Mar 2006 19:24 GMT
>I want to enter a float value and the unit for a Tmeperature object but
> when not given, want to use default values.  So I am testing default
[quoted text clipped - 21 lines]
>
>    char scale1 = (strScale1.toUpperCase()).charAt(0);

   How do you distinguish between "input not given" and "taking a really
long time to give input"? If I start up your program, wait 500 years, and
then enter in "26 degrees Celcius", how do you want your program to react?
Depending on your answer, there are different solutions.

   - Oliver


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.