{"id":375,"date":"2015-12-06T14:34:36","date_gmt":"2015-12-06T03:34:36","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=375"},"modified":"2015-12-06T14:34:36","modified_gmt":"2015-12-06T03:34:36","slug":"prompt-user-console-input-validate-response","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/prompt-user-console-input-validate-response\/","title":{"rendered":"Prompt user for console input and validate their response"},"content":{"rendered":"

We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used.<\/p>\n

It reads the input from the user and checks if it is a valid double. If its not it prompts the user again. Could easily be modified for different validation that you may require.<\/p>\n

\r\n  Scanner input = new Scanner( System.in );\r\n  Double value = null;\r\n  while (value==null) {\r\n\t  System.out.print("Please enter number: ");\r\n\t  String s = input.nextLine();\r\n\t  if (s.length()==0) {\r\n\t\t  System.out.println("You did not enter a value; Try again");\r\n\t  } else {\r\n\t\t  try {\r\n\t\t\t  value = new Double(s);\r\n\t\t  } catch (Exception ex) {\r\n\t\t\t  System.out.println("You did not enter a valid value; Try again");\r\n\t\t  }\r\n\t  }\r\n  }\r\n  double d = value.doubleValue();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used. It reads the input from the user and checks if it is a valid double. If its not it prompts the user again. Could easily be modified…<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[10,112,11,110,111],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-63","jetpack-related-posts":[{"id":56,"url":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","url_meta":{"origin":375,"position":0},"title":"Monthly Payment Calculator","date":"June 2, 2010","format":false,"excerpt":"This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":182,"url":"https:\/\/learn-java-by-example.com\/java\/high-guessing-game\/","url_meta":{"origin":375,"position":1},"title":"High Low Guessing Game","date":"May 17, 2011","format":false,"excerpt":"The assignment here is to write a simple game where the user tries to guess a randomly selected number. After each guess the application tells the user if the guess is too high or too low. This is repeated until the user finally guesses the number. First thing we need\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":375,"position":2},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":175,"url":"https:\/\/learn-java-by-example.com\/java\/money-change-breakdown\/","url_meta":{"origin":375,"position":3},"title":"Money Change Breakdown","date":"March 28, 2011","format":false,"excerpt":"What is the smallest number of coins (or notes) that are required to give a specified amount of change? This is the next problem we will address. To solve this problem we use basically the same process a shop keeper would when giving change to customers. That is we look\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":235,"url":"https:\/\/learn-java-by-example.com\/java\/check-java-string-integer\/","url_meta":{"origin":375,"position":4},"title":"How to check if a Java String is an integer?","date":"June 5, 2015","format":false,"excerpt":"The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":96,"url":"https:\/\/learn-java-by-example.com\/java\/simple-mortgage-calculator\/","url_meta":{"origin":375,"position":5},"title":"Simple Mortgage Calculator","date":"June 5, 2010","format":false,"excerpt":"This example expands on the Monthly Payment Calculator example we posted earlier. As well as calculating the monthly payment for a loan, it then goes on to use that to calculate the balance of the loan after each payment. This is often referred to as an amortisation schedule. The displayMonthlyBalance()\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/375"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":2,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":377,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/375\/revisions\/377"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}