Credits

Saturday, February 28, 2009

Keyboard Input in Java with Array

A keyboard input is required in any program, check the sample keyboard class below with array integration.



[+/-] show/hide




class OneDarray{
public static void main(String args[])
throws java.io.IOException{
int t, i;
int num []=new int[3];
int sum=0;

int xnum;
for (t=0;t<3;t++){
System.out.println("Input a number and hit enter key :" );
do
{
xnum=(int)System.in.read(); /*this line allows type casting from char to int from keyboard input*/
num[t]=xnum;
System.out.print(num[t]);
}while(xnum!='\n');

sum=sum+num[t];
}
System.out.println("The sum of 3 numbers "+sum);}}

AddThis