You are on page 1of 13

ACCEPTING INPUT FROM USER

➢ Using Command Line //before java 1.5


➢ Using Scanner Class// popular
➢ Using GUI (Applets.servlets,jsp…):TODO

Using Command line arguments:


The java command-line argument is an argument i.e. passed at the time of running the java
program.
The arguments passed from the console can be received in the java program and it can be used as
an input.
So, it provides a convenient way to check the behavior of the program for the different values. You
can pass N (1,2,3 and so on) numbers of arguments from the command prompt.

class CommandLine1{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
}
}

String args[]
saurabh jain 1234 @@@@
0 1 2 3 4 n-1

Save it: CommandLine1.java

Compile it: javac CommandLine1.java

Execute it: java CommandLine1

Object Oriented Programming (CSEG2016)


class CommandLine2{
public static void main(String args[]){

for(int i=0;i<args.length;i++) //you can pass any number of values


System.out.println(args[i]);

}
}

Object Oriented Programming (CSEG2016)


class AddCline
{
public static void main(String args[])
{
double a =Double.parseDouble(args[0]);//convert the String Value in double
double b =Double.parseDouble(args[1]);
double c = a + b;
System.out.println("Sum of a and b is :"+ c);
}
}

args
23.5 44.6
0 1

Object Oriented Programming (CSEG2016)


Note: To convert primitive data type (like int, long, double etc) into Non-primitive data type
(String) ‘toString()’ function is used and ‘parse()’ function is used for vice-versa.

All the primitive data types are wrapped in the wrapper classes and are wrapped as an object of it.

S.No. Data type Size Wrapper Class

1 byte 1 Byte
2 short 2 Short
3 int 4 Int
4 long 8 Long
5 float 4 Float
6 double 8 Double
7 char 2 Char

Object Oriented Programming (CSEG2016)


Using Scanner class:

Accepting input from the user in an interactive manner. A separate Scanner class is defined in
‘java.util.Scanner’ package which defines various methods for taking different data values from
user. Some of the methods of Scanner class are as follows:

Scanner is a class in java.util package used for obtaining the input of the primitive types like int,
double, etc. and strings. It is the easiest way to read input in a Java program.

To create an object of Scanner class, we usually pass the predefined object System.in, which
represents the standard input stream. We may pass an object of class File if we want to read input
from a file.

To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For
example, to read a value of type short, we can use nextShort()

To read strings, we use nextLine().

To read a single character, we use next().charAt(0). next() function returns the next token/word in
the input as a string and charAt(0) function returns the first character in that string.

The Scanner class reads an entire line and divides the line into tokens. Tokens are small elements
that have some meaning to the Java compiler.
For example, Suppose there is an input string: How are you

In this case, the Scanner object will read the entire line and divides the string into tokens: “How”,
“are” and “you”. The object then iterates over each token and reads each token using its different
methods.

S.No. Data Type Method


1 Byte nextByte()
2 Short nextShort()
3 Int nextInt()
4 Long nextLong();
5 Float nextFloat()
6 Double nextDouble()
7 Boolean nextBoolean()
8 String next()

Object Oriented Programming (CSEG2016)


WAP in JAVA that takes two integer values from user using Scanner class, and
print their sum.

import java.util.*;
class UsingScanner
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int a,b;
System.out.println("Enter first number");
a=s.nextInt();
System.out.println("Enter Second number");
b=s.nextInt();
int c=a+b;
System.out.println("Addition is:"+c);
}
}

Object Oriented Programming (CSEG2016)


WAP in JAVA takes student information from the user and prints their record.

Object Oriented Programming (CSEG2016)


import java.util.*;
import java.lang.*;//default package
public class UsingScanner1
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int roll;
double per;
String name;
System.out.println("Enter roll number");
roll=s.nextInt();
System.out.println("Enter percentage");
per=s.nextDouble();
System.out.println("Enter name");
name=s.next();
System.out.println("Roll number is:"+roll);
System.out.println("Percentage is:"+per);
System.out.println("Name is:"+name);
}
}

Object Oriented Programming (CSEG2016)


Assignment 1
Q.2 Differentiate Java and C++.
Q.3: What is the difference between the next() and nextLine() method in java, Justify your answer
with the program.

Object Oriented Programming (CSEG2016)


WAP in JAVA shows arithmetic operations. (Take inputs from user using Scanner class).

Object Oriented Programming (CSEG2016)


WAP in JAVA takes radius as an input from the user and print area and circumference.

WAP to find the largest of three numbers.

import java.util.*;
class Largest
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter Three Integer values:");
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
if(a>b && a>c)
{

Object Oriented Programming (CSEG2016)


System.out.println("greater number is:"+a);
}
else if (b>a && b>c)
{
System.out.println("greater number is:"+b);
}
else
{
System.out.println("greater number is:"+c);
}
}
}

Object Oriented Programming (CSEG2016)


// Java program to read data of various types using Scanner class.
import java.util.Scanner;
public class ScannerDemo1
{
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);

// String input
String name = sc.nextLine();

// Character input
char gender = sc.next().charAt(0);

// Numerical data input


// byte, short and float can be read
// using similar-named functions.
int age = sc.nextInt();
long mobileNo = sc.nextLong();
double cgpa = sc.nextDouble();

// Print the values to check if the input was correctly obtained.


System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Age: "+age);
System.out.println("Mobile Number: "+mobileNo);
System.out.println("CGPA: "+cgpa);
}
}

Output :

Name: Geek
Gender: F
Age: 40
Mobile Number: 9876543210
CGPA: 9.9

3. Using GUI: will discuss later. (Applets, JSP, Servlets)

Object Oriented Programming (CSEG2016)

You might also like