您的位置:首页 > 编程语言 > PHP开发

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐