In order for the user to enter a floating point number, try using the functions in the java.lang.Double class with the following signatures:public static Double valueOf( String s);andpublic double doubleValue();For example:BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in)); String usersInput = stdin.readLine(); Double theNumberAsAnObject = Double.valueOf( usersInput); double theValue = theNumberAsAnObject.doubleValue();In other notes you should make sure you use curly braces around the code you want executed inside an if statement. Also, in order to catch out of bounds values from a user, you probably want to ask for the input inside a loop. There's an example or two in the book, but the general idea is this (in pseudo-code):boolean answerIsInBounds = false; double theanswer = -1; // This need to be declared outside the while loop!!!! while( !answerIsInBounds) { printThePrompt(); theanswer = usersInput(); if(( theanswer > 0) && (theanswer < 100)) { // bounds of 0 to 100 answerIsInBounds = true; } } // Use the answer here.
The views and opinions expressed in this page are strictly those of the page author.
The contents of this page have not been reviewed or approved by the University of Minnesota.