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);
}
}
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));
}
}
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.