Input and Output(easy to use)
2007-06-05 14:02
288 查看
Input and Output
To make our example programs more interesting, we want to accept input and properly format the program output. Of course, modern programs use a GUI for collecting user input. However, programming such an interface requires more tools and techniques than we have at our disposal at this time. Because the first order of business is to become more familiar with the Java programming language, we make do with the humble console for input and output for now. GUI programming is covered in Chapters 7 through 9.
Reading Input
You saw that it is easy to print output to the "standard output stream" (that is, the console window) just by calling System.out.println. Oddly enough, before JDK 5.0, there was no convenient way to read input from the console window. Fortunately, that situation has finally been rectified.
To read console input, you first construct a Scanner that is attached to the "standard input stream" System.in.
Scanner in = new Scanner(System.in);
Now you use the various methods of the Scanner class to read input. For example, the nextLine method reads a line of input.
System.out.print("What is your name? ");
String name = in.nextLine();
Here, we use the nextLine method because the input might contain spaces. To read a single word (delimited by whitespace), call
String firstName = in.next();
To read an integer, use the nextInt method.
System.out.print("How old are you? ");
int age = in.nextInt();
Similarly, the nextdouble method reads the next floating-point number.
The program in Example 3-2 asks for the user's name and age and then prints a message like
Hello, Cay. Next year, you'll be 46
Finally, add the line
import java.util.*;
at the beginning of the program. The Scanner class is defined in the java.util package. Whenever you use a class that is not defined in the basic java.lang package, you need to use an import directive. We look at packages and import directives in more detail in Chapter 4.
Example 3-2. InputTest.java
1. import java.util.*;
2.
3. public class InputTest
4. {
5. public static void main(String[] args)
6. {
7. Scanner in = new Scanner(System.in);
8.
9. // get first input
10. System.out.print("What is your name? ");
11. String name = in.nextLine();
12.
13. // get second input
14. System.out.print("How old are you? ");
15. int age = in.nextInt();
16.
17. // display output on console
18. System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
19. }
20. }
NOTE
If you do not have JDK 5.0 or above, you have to work harder to read user input. The simplest method is to use an input dialog (see Figure 3-6).
String input = JOptionPane.showInputDialog(promptString)
The return value is the string that the user typed.
For example, here is how you can query the name of the user of your program:
String name = JOptionPane.showInputDialog("What is your name?");
Reading numbers requires an additional step. The JOptionPane.showInputDialog method returns a string, not a number. You use the Integer.parseInt or Double.parseDouble method to convert the string to its numeric value. For example,
String input = JOptionPane.showInputDialog("How old are you?");
int age = Integer.parseInt(input);
If the user types 45, then the string variable input is set to the string "45". The Integer.parseInt method converts the string to its numeric value, the number 45.
The JOptionPane class is defined in the javax.swing package, so you need to add the statement
import javax.swing.*;
Finally, whenever your program calls JOptionPane.showInputDialog, you need to end it with a call to System.exit(0). The reason is a bit technical. Showing a dialog box starts a new thread of control. When the main method exits, the new thread does not automatically terminate. To end all threads, you call the System.exit method. (For more information on threads, see Chapter 1 of Volume 2.) The following program is the equivalent to Example 3-2 prior to JDK 5.0.
import javax.swing.*;
public class InputTest
{
public static void main(String[] args)
{
String name = JOptionPane.showInputDialog("What is your name?");
String input = JOptionPane.showInputDialog("How old are you?");
int age = Integer.parseInt(input);
System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
System.exit(0);
}
}
To make our example programs more interesting, we want to accept input and properly format the program output. Of course, modern programs use a GUI for collecting user input. However, programming such an interface requires more tools and techniques than we have at our disposal at this time. Because the first order of business is to become more familiar with the Java programming language, we make do with the humble console for input and output for now. GUI programming is covered in Chapters 7 through 9.
Reading Input
You saw that it is easy to print output to the "standard output stream" (that is, the console window) just by calling System.out.println. Oddly enough, before JDK 5.0, there was no convenient way to read input from the console window. Fortunately, that situation has finally been rectified.
To read console input, you first construct a Scanner that is attached to the "standard input stream" System.in.
Scanner in = new Scanner(System.in);
Now you use the various methods of the Scanner class to read input. For example, the nextLine method reads a line of input.
System.out.print("What is your name? ");
String name = in.nextLine();
Here, we use the nextLine method because the input might contain spaces. To read a single word (delimited by whitespace), call
String firstName = in.next();
To read an integer, use the nextInt method.
System.out.print("How old are you? ");
int age = in.nextInt();
Similarly, the nextdouble method reads the next floating-point number.
The program in Example 3-2 asks for the user's name and age and then prints a message like
Hello, Cay. Next year, you'll be 46
Finally, add the line
import java.util.*;
at the beginning of the program. The Scanner class is defined in the java.util package. Whenever you use a class that is not defined in the basic java.lang package, you need to use an import directive. We look at packages and import directives in more detail in Chapter 4.
Example 3-2. InputTest.java
1. import java.util.*;
2.
3. public class InputTest
4. {
5. public static void main(String[] args)
6. {
7. Scanner in = new Scanner(System.in);
8.
9. // get first input
10. System.out.print("What is your name? ");
11. String name = in.nextLine();
12.
13. // get second input
14. System.out.print("How old are you? ");
15. int age = in.nextInt();
16.
17. // display output on console
18. System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
19. }
20. }
NOTE
If you do not have JDK 5.0 or above, you have to work harder to read user input. The simplest method is to use an input dialog (see Figure 3-6).
String input = JOptionPane.showInputDialog(promptString)
The return value is the string that the user typed.
For example, here is how you can query the name of the user of your program:
String name = JOptionPane.showInputDialog("What is your name?");
Reading numbers requires an additional step. The JOptionPane.showInputDialog method returns a string, not a number. You use the Integer.parseInt or Double.parseDouble method to convert the string to its numeric value. For example,
String input = JOptionPane.showInputDialog("How old are you?");
int age = Integer.parseInt(input);
If the user types 45, then the string variable input is set to the string "45". The Integer.parseInt method converts the string to its numeric value, the number 45.
The JOptionPane class is defined in the javax.swing package, so you need to add the statement
import javax.swing.*;
Finally, whenever your program calls JOptionPane.showInputDialog, you need to end it with a call to System.exit(0). The reason is a bit technical. Showing a dialog box starts a new thread of control. When the main method exits, the new thread does not automatically terminate. To end all threads, you call the System.exit method. (For more information on threads, see Chapter 1 of Volume 2.) The following program is the equivalent to Example 3-2 prior to JDK 5.0.
import javax.swing.*;
public class InputTest
{
public static void main(String[] args)
{
String name = JOptionPane.showInputDialog("What is your name?");
String input = JOptionPane.showInputDialog("How old are you?");
int age = Integer.parseInt(input);
System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
System.exit(0);
}
}
相关文章推荐
- How to use the CONVERSION_EXIT_PARVW_INPUT and CONVERSION_EXIT_PARVW_OUTPUT
- Easy way to use JText 2.1.7 and HTML-style template for generating report
- Sodium: A Modern and Easy-to-Use Crypto Library
- The most simple and easy to use Qt GUI library plug-in FTStyle (2)The latest version - 2017.10
- Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly
- java call so :only use one struct as input and output parameter
- Effective C++ Item 18 Make interfaces easy to use correctly and hard to use incorrectly
- Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. This makes tabs and backspa
- 条款18:让接口容易被正确使用,不易被误用(Make interface easy to use correctly and hard to use incorrectly)
- Effective C++ 18. Make interfaces easy to use correctly and hard to use incorrectly
- c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow
- Chapter 3 Introduction to Objects and Input/Output
- Use a String.Format format and transform its output to its inputs?
- The most simple and easy to use Qt GUI library plug-in FTStyle (1)
- I learned several ASP.NET's AJAX ability today! It is so interesting and so easy to use AJAX in ASP.NET.
- Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly
- Easy way to use JText 2.1.7 and HTML-style template for generating report
- A Fast and Easy to Use AES Library
- The most simple and easy to use Qt GUI library plug-in FTStyle (3)How do Qt4 and Qt5 choose
- [20160704]Addition program that use JOptionPane for input and output