Java for Complete Beginners (Video), Part 7: Getting User Input

How to get console user input in your Java programs using the JDK Scanner class.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.

Code for this tutorial:

import java.util.Scanner;

public class App {
	public static void main(String[] args) {
		
		// Create scanner object
		Scanner input = new Scanner(System.in);
		
		// Output the prompt
		System.out.println("Enter a floating point value: ");
		
		// Wait for the user to enter something.
		double value = input.nextDouble();
		
		// Tell them what they entered.
		System.out.println("You entered: " + value);
	}
}

Note: if you’re in Europe outside the UK, you might need to enter floating point numbers in the format x,y rather than x.y as is normal in the USA and UK.

Enter a floating point value: 
5,6
You entered: 5.6

Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

3 Responses to Java for Complete Beginners (Video), Part 7: Getting User Input

  1. kat says:

    hi, i would just like to ask about the difference between, JOptionPane, buffer reader and Scanner? do they have differences at all. Do I have to learn them all or i could just stick to Scanner? Im preparing for my Java exam this Feb and im learning through your videos. Thanks!

    • Squiffy says:

      Hi Kat, JOptionPane pops up a dialog that can let you input stuff (or may just present buttons or a message). Scanner is used to parse input that’s typed on the command line or in the console of your IDE (Eclipse or whatever); it doesn’t create any dialogs. BufferedReader is a class that’s used usually to read lines of input from a file (in connection with a bunch of other classes). Or you could probably use it to figure out when the user has entered a line of text in the console. So they are all quite different. If you’re lucky you can stick to Scanner, which is the simplest to work with for simple programs.

  2. kat says:

    Oh, now I get it! Thanks! That was really helpful!:)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Tutorial, Java Video Tutorials (Beginners) | Tagged , , , |