0% found this document useful (0 votes)
14 views11 pages

03-1 Taking Inputs in To The Program

Uploaded by

Karansingh Bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

03-1 Taking Inputs in To The Program

Uploaded by

Karansingh Bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Programming - inputs

Inputting to Java
Program
Java Programming - inputs

Using public static void main(String[ ]


args)
• public static void main(String[] args)

Starting point
• Following are valid declaration of the main() of execution
• public static void main(String[] args)
• static public void main(String[] a)
• public static void main(String… ar)
Java Programming - inputs

Let’s write a program to display user’s


name
• Requirement
• Take input from user
Java Programming - inputs

Solution – I (Command Line Argument)


public class MyName {
public static void main(String args[]){
String name = args[0];
}
}
Java Programming - inputs

Solution - II
public class MyName {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String name = sc.next();
System.out.println(name);
}
}
Java Programming - inputs

Let’s see one more example

• public class Addition {


• public static void main(String args[]){
• int input1 = args[0]; Error: args is type of
• int input2 = args[1]; String
• System.out.println(input1 + input2);
• }
•}
Java Programming - inputs

Corrected program
• import java.util.Scanner;

• public class Addition {


• public static void main(String args[]){
• int input1 = Integer.parseInt(args[0]);
• int input2 = Integer.parseInt(args[1]);
• System.out.println(input1 + input2);
• }
•}
Java Programming - inputs

Import Statements and the Java API


• Can we create String class????
• Yes, it is perfectly legal to declare your own String class

public class String{


public static void main(String... arg){
System.out.println("Hello World");
}
} Program will compile but
program will not run as JRE
need inbuilt String class
Java Programming - inputs

Correct the program so user can


execute it
• Mention the package of the String class

public class String{


public static void main(java.lang.String... arg){
System.out.println("Hello World");
}
} Program will compile,
program will run as JRE will
find inbuilt String class
Java Programming - inputs
Java Programming - inputs

You might also like