Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to enter integers in java
How to enter integers in java
Input report can be realized by directly inputting parameters through the "scanner" function, such as:

Import java.util.scanner;

Common class test {

Public static void main(String[] args) {

Scanner input = new scanner (system. in);

System.out.println ("Please enter an integer:");

int length = input . nextint(); //Enter an integer.

System.out.println ("The integer entered is:"+length); }

}

Note: The Scanner function is used for statement input. The above example is an integer type, or you can use the following statement "String length=input.next ()", and the input is a string type.

Method for judging whether it is an integer:

Method one

Package operation;

Import java.io.bufferedreader;

Import java.io.ioexception;

Import java.io.inputstreamreader;

Public class Test2 {

Public static void main(String[] args) {

BufferedReader bReader = null

bReader = new buffered reader(new InputStreamReader(system . in));

Try {

String line = null

int sum = 0;

for(int I = 0; I<2; i++) {

System.out.println ("Please enter the first one" +(i+ 1)+ "integer");

//Get the data input by the keyboard

line = breader . readline();

//Judge whether it is a number.

if(line . matches("[0-9]+"){

//Converts a numeric string to an integer type.

sum+= integer . parse int(line);

} Otherwise {

I-;

Continue;

}

}

system . out . println(sum);

} catch (IOException e) {

// TODO automatically generated catch block

e . printstacktrace();

}

}

}

//Method 2

Package operation;

Import java.io.bufferedreader;

Import java.io.ioexception;

Import java.io.inputstreamreader;

Public class Test3 {

Public static void main(String[] args) {

int sum = 0;

int count = 0;

String line = null

BufferedReader bReader = null

Do {

System.out.println ("Please enter the first one"+(count+1));

//Create a buffered character input stream and input data from the keyboard.

bReader = new buffered reader(new InputStreamReader(system . in));

Try {

//Get data

line = breader . readline();

//Determine whether it is an integer.

if(line . matches("[0-9]+"){

sum+= integer . parse int(line);

count++;

}

} catch (IOException e) {

// TODO automatically generated catch block

e . printstacktrace();

}

//Judge whether the data quantity meets the demand.

} while(count & lt; 2);

System.out.println ("the sum of two integers is"+sum ");

Try {

If (the baker! = null) {

breader . close();

}

} catch (IOException e) {

e . printstacktrace();

}

}

}