More Inputs and Outputs

The Scanner class

We can use the Scanner class to obtain input from the user. To do this, we first import the java.util.Scanner package

import java.util.Scanner;

Then, later in our code, we declare and create a Scanner object in the typical way:

Scanner myScanner = new Scanner(System.in);

Notice in the constructor, we have included "System.in" as a parameter. The constructor requires an input stream be named for the scanner object to watch for input. In this case, "System.in" tells the scanner to watch what the user types in the console window.

Next, (after prompting the user to type something, if appropriate) we can use one of the following methods to actually get our hands on the input typed (depending on the type we want our input to take from then on):

  • nextByte()
  • nextShort()
  • nextInt()
  • nextLong()
  • nextFloat()
  • nextDouble()
  • nextBoolean()
  • nextLine()

* These are just some of the most common methods used with a Scanner -- one can consult the Java API documentation on the Scanner class for the rest.

So for example, if we wanted the user to type the concentration of a mixture (which will necessarily need to be of type double); and then declare and construct a scanner object to assign the value typed to the (double) variable named "concentration", we could do the following:

System.out.print("Enter the concentration of the mixture: ");
Scanner myScanner = Scanner(System.in);
double concentration = myScanner.nextDouble();

Using the JOptionPane class

You can also obtain input from the user using the JOptionPane class. Of course, one first needs to import the appropriate package to use it. This means adding the following in the appropriate spot in your program:

import javax.swing.JOptionPane;

Then, later in your code, you can invoke one of the methods of the JOptionPane class to get information from the user. For example, you might use

String s = JOptionPane.showInputDialog("Enter a year: ");

We should -- more generally, of course -- change the "Enter a year" prompt to something more appropriate for the information we wish to collect from the user in a different context.

The result of the statement above will be that the program displays the following dialog box:

If we want more control over the content of the dialog box, we can call the same method with additional parameters, like the following:

String s = JOptionPane.showInputDialog(null,
             "Enter a year: ",
             "My Query",
             JOptionPane.INFORMATION_MESSAGE);

which yields:

You can see that the title of the dialog box has changed to "My Query", and the style of the dialog box (at least to the extent that the icon has changed) has also been modified. The style of a dialog box is determined by the last parameter given to the showInputDialog() method call. The possible values are:

  • ERROR_MESSAGE
  • INFORMATION_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE

(Note: Since we didn't give any information to the user in the last example, we probably should not have used the INFORMATION_MESSAGE style. A QUESTION_MESSAGE would have been more appropriate, and would have given us the same icon we saw in the first picture.)

You may have noticed that the first parameter passed to the method was "null". We will talk more about this later, but for now, think of "null" as something to pass to a method when you have to pass something, but don't really have anything to pass. In our example, the first parameter is supposed to represent a "parent component" (again, more on what that is later), but we don't have one in this program, so we give pass "null" as a "space-holder" of sorts.

As the other styles mentioned above suggest, we can use the JOptionPane class to do more than just get input from the user. We can also use it to display messages to the user. For example, the following code:

JOptionPane.showMessageDialog(null,
  "Chris Columbus sailed the ocean blue.",
  "In 1942...",  
  JOptionPane.ERROR_MESSAGE); 

displays the dialog box

This just scratches the surface, however. If you want to know all of the other things the JOptionPane class can do, check out the class documentation in the Java API.

Converting from String to numeric types

Frequently, we have need to convert from a string type to a numeric type -- especially when collecting numeric input from the user through either the command line arguments or through a GUI (graphical user interface) object like the dialog boxes produced with the JOptionPane class.

In both of the aforementioned situations, a user might be asked to input a number (like 1234) only to have it stored in a String variable (as "1234"). In this string form, we can't add it with other numbers. We can't subtract it from other numbers. We can't do any arithmetic operations on it at all. We need some method for turning a string like this into a numeric type so that we can work with it.

Unfortunately, String variables can't be turned into a numeric type by casting.

However, there are some classes that come to our aid in this circumstance. The "Integer" class, for example, comes with a static method called "parseInt" that can be used to translate a string into a variable of type "int". (We'll talk more about this later, but a "static" method is a method that belongs to the entire class, and not a specific object / instance of that class.)

Example

String myString = "1234";
int n = Integer.parseInt(myString);
int m = 2 * n;
Sytem.out.println(m);      // prints 2468

In the same way, the "Double" class (note the capital 'D', this is a different class than "double") also comes with a static method called "parseDouble" that can be used to translate a string into a variable of type "double".

Example

String myString = "3.14";
double d1 = Double.parseDouble(myString);
double d2 = d1 % 2;
System.out.println(d2);    // prints 1.14