java-input Basic class

Source: Internet
Author: User
Tags exit integer return
Java beginners, must be difficult to enter data from the keyboard, use the following class input, you can
Easy to enter data from the keyboard:
Examples of using methods: String s=input.readstring (); Read into string
int I=input.readint (); Read integer
Below is the Java input and output basic class input class source code:
Import java.io.*;
Class Input
{static InputStreamReader in;
static BufferedReader reader;
Static
{in=new InputStreamReader (system.in);
Reader=new BufferedReader (in);
}


Static String readString ()
{String s= "";
Try
{S=reader.readline ();

}

catch (IOException E)
{System.out.println (e);
System.exit (0);
}
return s;
}

static Char Readchar ()
{char ch= ' a ';
Try
{
String s=readstring ();
Ch=s.charat (0);

}
catch (Exception e)
{SYSTEM.OUT.PRINTLN ("The data type entered is not correct, the program will exit");
System.exit (0);
}
return ch;
}


static int readInt ()
{String s=readstring ();
int i=0;
try{
I=integer.parseint (s);
}
catch (Exception e)
{SYSTEM.OUT.PRINTLN ("The data type entered is not correct, the program will exit");
System.exit (0);
}
return i;
}
Static Double readdouble ()
{String s=readstring ();
Double d=0.0;
Try
{d=double.parsedouble (s);
}
catch (Exception e)
{SYSTEM.OUT.PRINTLN ("The data type entered is not correct, the program will exit");
System.exit (0);
}
return D;
}
static float Readfloat ()
{
String s=readstring ();
float f=0.0f;
Try
{
F=float.parsefloat (s);
}
catch (Exception e)
{SYSTEM.OUT.PRINTLN ("The data type entered is not correct, the program will exit");
System.exit (0);
}
return F;
}
}

Use examples, enter 10 integers from the keyboard:
Class Inoutdata
{public static void main (String args[])
{int a[]=new int[10];
for (int i=0;i<10;i++)
{System.out.println ("Please enter a number");
A[i]=input.readint ();
}
for (int i=0;i<10;i++)
System.out.println ("a[" +i+ "]=" +a[i));
}
}




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.