Inputs from user

We will accept variables of type integer , string, boolean , double,long, float,char, byte from user and process the same in our sample scripts.

Create a New Class under same package , ( Pack1 ) . Give the name my_inputs
package Pack1;

public class my_inputs {

}
We have to use import java.util.Scanner;
package Pack1;
import java.util.Scanner;

public class my_inputs {
	public static void main(String args[]) {
	
}
}
Now let us create one Scanner object and name it my_input. After creating the object We will ask the user to enter the number.
package Pack1;
import java.util.Scanner;

public class my_inputs {
 public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
}
}
We will print message asking user to inter the input and then accept the value from user. To confirm the input we will again display the same value to the user as an output. Here is the complete code.
package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
 System.out.println("Enter input: ");//String output asking user input
 int my_int=my_input.nextInt();  // Collecting input from user 
 System.out.println(my_int);     // Printing the same integer output
}
}
Ask two inputs from user and then display the sum of the two inputs.
package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter first input: "); // asking user input
 int my_int1=my_input.nextInt();  // Collecting input from user

 System.out.println("Enter second input: "); //asking user input
 int my_int2=my_input.nextInt();  // Collecting input from user
	
 System.out.println("Sum of two inputs" + my_int1+my_int2);// output
}
}
If you enter 30 and 40 as inputs then output will be 3040 ( Why ? )

To get the mathemetical sum of two inputs we will change the last lines like this.
int sum=my_int1+my_int2;
System.out.println("Sum of two inputs" + sum);// Printing the sum
Let us collect other type variables like string, boolean , double etc ..

String input

Collecting two input strings from user and printing the output
package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter first Name"); //asking user input
 String my_str1=my_input.next(); //  input from user
	
 System.out.println("Enter Last Name: "); //asking user input
 String my_str2=my_input.next();  // input from user
	
	
 System.out.println("Welcome " + my_str1 + my_str2);// Output
}
}

Closing the scanner object

After using the scanner object it is good practice to close this scaner object.
my_input.close(); // Closing the scanner object 

Boolean input

Collecting one boolean input from the user
package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter true or false: "); //asking user input
 Boolean my_boolean=my_input.nextBoolean(); //input from user
	
 my_input.close(); // Closing the scanner object 	
 System.out.println("value is  " + my_boolean);//
}
}

Double input

package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter Double value: "); //asking user input
 Double my_double=my_input.nextDouble(); // input from user
	
 my_input.close(); // Closing the scanner object 	
 System.out.println("value is  " + my_double);//
}
}

float input

package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter float value: "); //asking user input
 float  my_float =my_input.nextFloat(); // input from user
	
 my_input.close(); // Closing the scanner object 	
 System.out.println("value is  " + my_float);//Output
}
}

char input

package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter Char value: "); //asking user input
 char my_char =my_input.next().charAt(0); // input from user
	
 my_input.close(); // Closing the scanner object 	
 System.out.println("value is  " + my_char);//
}
}

long input

package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter long value: "); //asking user input
 Long my_long =my_input.nextLong(); // input from user
	
 my_input.close(); // Closing the scanner object 	
 System.out.println("value is  " + my_long);//
}
}

short input

package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter short value: "); //asking user input
 Short my_short =my_input.nextShort(); // input from user
	
 my_input.close(); // Closing the scanner object 	
 System.out.println("value is  " + my_short);//
}
}

Byte input

package Pack1;
import java.util.Scanner;

public class my_inputs {
public static void main(String args[]) {
 Scanner my_input = new Scanner(System.in); // Scanner object created
	
 System.out.println("Enter byte  value: "); //asking user input
 Byte my_byte =my_input.nextByte(); // input from user
	
 my_input.close(); // Closing the scanner object 	
 System.out.println("value is  " + my_byte);//
}
}



Java

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer