Java IO _ IO operation instance notes

Source: Internet
Author: User

Java IO _ IO operation instance notes

Objectives of this chapter:
Master Java data operation instances

Example 1: Addition

Example 2: menu display

3. Knowledge Used in this instance

1. Standard Format of keyboard input data
2. date conversion and use of packaging classes
3. class design ideas.

4. Details

Sample Code:

Import Java. io. *; public class execdemo01 {public static void main (string ARGs []) throws exception {int I = 0; Int J = 0; bufferedreader Buf = NULL; buf = new bufferedreader (New inputstreamreader (system. in); string STR = NULL; system. out. println ("Enter the first number"); STR = Buf. readline (); // receives data I = integer. parseint (STR); system. out. println ("enter the second number"); STR = Buf. readline (); // receives data J = integer. parseint (STR); system. out. println (I + "+" + I + "=" + (I + J ));}}

The above is done, but there are the following problems:
A. The current information is input by the user. What if the user does not enter a number?
B. This program can only enter Integers
C. The code is repeated. bufferedreader must be used when the input data is used, so the code is repeated.

In this case, you need to divide classes more rationally.
The most likely input data is integers, decimals, dates, and strings. Therefore, it is best to design a special input data type to complete the input data function, in addition, the input data can be verified.
The following describes the classes used to process input data, but only integers and strings can be obtained.

Import Java. io. *; import Java. util. *; import Java. text. *; public class inputdata {private bufferedreader Buf = NULL; Public inputdata () {// use this statement as long as the input data is required. buf = new bufferedreader (New inputstreamreader (system. in);} Public String getstring (string info) {// obtain the string information string temp = NULL; system. out. print (Info); // print the prompt message try {temp = This. buf. readline (); // receives data} catch (ioexception e) {e. printst Acktrace ();} return temp;} public int getint (string info, string ERR) {// obtain the input data of an integer int temp = 0; string STR = NULL; boolean flag = true; // defines a flag while (FLAG) {STR = This. getstring (Info); // receives data if (Str. matches ("^ \ D + $") {// determines whether a number is composed of temp = integer. parseint (STR); // transformation flag = false; // end loop} else {system. out. println (ERR); // print error message} return temp;} public float getfloat (string I Nfo, string ERR) {// obtain the input data float temp = 0; string STR = NULL; Boolean flag = true; // define a flag while (FLAG) {STR = This. getstring (Info); // receives data if (Str. matches ("^ \ D + .? \ D + $ ") {// determines whether a number is composed of temp = float. parsefloat (STR); // transformation flag = false; // end loop} else {system. out. println (ERR); // print error message} return temp;} public date getdate (string info, string ERR) {// obtain a decimal input data date temp = NULL; string STR = NULL; Boolean flag = true; // defines a flag while (FLAG) {STR = This. getstring (Info); // receives data if (Str. matches ("^ \ D {4}-\ D {2}-\ D {2} $ ")) {// determine whether a number is composed of simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); try {temp = SDF. parse (STR); // convert string to date data} catch (exception e) {} flag = false; // end loop} else {system. out. println (ERR); // print error message }}return temp ;}};

Test the inputdate class

Import Java. io. *; import Java. util. *; public class testinput {public static void main (string ARGs []) throws exception {inputdata input = new inputdata (); // float F = input. getfloat ("Please enter decimal places:", "the input is not a decimal point. Please enter it again! "); // System. out. println ("decimal:" + F); Date d = input. getdate ("Enter the date in the format of (yyyy-mm-dd):", "the input date format is incorrect. Please enter it again"); system. out. println ("date" + d );}};

Final code:

Import Java. io. *; public class execdemo01 {public static void main (string ARGs []) throws exception {int I = 0; Int J = 0; bufferedreader Buf = NULL; // receives the input data from the keyboard. Buf = new bufferedreader (New inputstreamreader (system. in); string STR = NULL; // receives the data system. out. print ("Enter the first number:"); STR = Buf. readline (); // receives data I = integer. parseint (STR); // converts a string to an integer system. out. print ("enter the second number:"); STR = Buf. readline (); // receives data J = integer. parseint (STR); // rename system. out. println (I + "+" + J + "=" + (I + J ));}};

4.2 menu display

Menu display is a common function in various systems.
Currently, a menu is required to be displayed, but in fact each option will certainly have its own operations. To facilitate the operation, create the following classes:
Menu (menu information is displayed)-> operate (Operation class)

Public class operate {public static void add () {// Add operation system. out. println ("** select Add operation");} public static void Delete () {// delete operation system. out. println ("** delete operation selected");} public static void Update () {// update operation system. out. println ("** the update operation is selected");} public static void find () {// view the operation system. out. println ("** check operation selected ");}};
Public Class Menu {public menu () {While (true) {This. show (); // unlimited call menu display} public void show () {system. out. println ("==== XXX system ===="); system. out. println ("[1], add data"); system. out. println ("[2], delete data"); system. out. println ("[3], modify data"); system. out. println ("[4], View data"); system. out. println ("[0], system logout \ n"); inputdata input = new inputdata (); int I = input. getint ("select:", "enter the correct option! "); Switch (I) {Case 1: {operate. add (); // call the add operation break;} Case 2: {operate. delete (); // call the delete operation break;} Case 3: {operate. update (); // call the update operation break;} case 4: {operate. find (); // call to view the break;} case 0: {system. exit (1); // exit break;} default: {system. out. println ("select the correct operation! ");}}}};
import java.io.* ;public class ExecDemo03{    public static void main(String args[]) throws Exception{        new Menu() ;    }};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.