Java limit console input type of value

Source: Internet
Author: User

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;

public class control input type {
Static InputStreamReader in;
static BufferedReader reader;
Static
{in=new InputStreamReader (system.in);
Reader=new BufferedReader (in);
}


Control input is string
Static String readString ()
{String s= "";
Try
{S=reader.readline ();

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

Control input is Char
static Char ReadChar ()
{char ch= ' a ';
Try
{
String s=readstring ();
Ch=s.charat (0);

}
catch (Exception e)
{System.out.println ("The input data type is incorrect, the program exits");
System.exit (0);
}
return ch;
}

control input is int
static int readInt ()
{String s=readstring ();
int i=0;
try{
I=integer.parseint (s);
}
catch (Exception e)
{System.out.println ("The input data type is incorrect, the program exits");
System.exit (0);
}
return i;
}

Control input is double
Static Double readdouble ()
{String s=readstring ();
Double d=0.0;
Try
{d=double.parsedouble (s);
}
catch (Exception e)
{System.out.println ("The input data type is incorrect, the program exits");
System.exit (0);
}
return D;
}


Control input is float
static float Readfloat ()
{
String s=readstring ();
float f=0.0f;
Try
{
F=float.parsefloat (s);
}
catch (Exception e)
{System.out.println ("The input data type is incorrect, the program exits");
System.exit (0);
}
return F;
}

}

Java limit console input type of value

Related Article

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.