Posts Tagged ‘Scanner’

Prompt user for console input and validate their response

Java 6.12.2015 No Comments

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…

Read More

Using Scanner to read words from text file

Java, Tip 20.11.2014 1 Comment

Often you need to read a file line by line. Alternatively sometimes you want to read text word by word (for example to count the occurrence of different words). The Scanner classes next() method can be used for this as shown in the following example. You can find an example of the use of the…

Read More

High Low Guessing Game

Java 17.5.2011 5 Comments

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 is to generate a random…

Read More

Simple Mortgage Calculator

Simple Mortgage Calculator

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() method handles generating the schedule…

Read More

Monthly Payment Calculator

Java 2.6.2010 1 Comment

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 input). The Scanner class comes…

Read More
s2Member®