- First of all we need to create a package
- My package is saved by HelloWorld name so this is showing package HelloWorld;
- At the end of my package syntax I have entered semi colon ; because in java we are telling the browser that our statement is ending here .
- import java.util.Scanner; = this is basically a syntax for scan data , get input from console or others, the thing to note is this first letter of scanner will always in Capital form Scanner and then dont forget to enter semi colon ;
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
it's called boiler plate
This syntax will inform to browser ----------- I'm starting code from here
Now let me explain this---------
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
here scan is name for scanner (you can use any name instead of scan) = new Scanner means we are creating a new scanner and (System.in) is inform you that I'm getting input from console
Note: जब हमें output चाहिए तो वहाँ हम syntax रखेंगे
Note: जब हमें output चाहिए तो वहाँ हम syntax रखेंगे
System.out.print("HelloWorld");
In this code we are trying to print HelloWorld
Point to highlight that: डबल quote के use से जो भी वैल्यू होगी वो प्रिन्ट हो जाएगी !
Point to highlight that: डबल quote के use से जो भी वैल्यू होगी वो प्रिन्ट हो जाएगी !
Now let me come to the topic -----------
After write Scanner code
After write Scanner code
Because I want to take character input from console so I will use a data type, that is Char
Now PFB - -- - 👇
Now PFB - -- - 👇
char ch = scan.next().charAt(0);
// char ch = scan.next().charAt(0); this is only for reading
Now let me explain this---------
- char is a data type for character and ch is a random name for char
- scan.next().charAt(0); ======= charAt(0) is basically used to inform to the crawler or browser that take character from console 0 means if I'm writing a sentence on console like Divyanshu then because here is syntax ...... charAt(0) so browser will print only 1st Character That is D from this name.
int i = ch;
System.out.print(i);
}
}
You can see that I have entered Divyanshu on console but its printing only 1 letter from Divyanshu which is D
Important Points to Learn----------------
- You can use another data type to get character input on java That is ;;;;; String
String s = scan.next();
System.out.print(s);
- Now here you can see on console I have entered Divyanshu Khare but once I'm trying to print what is coming out ? Answer is Only Divyanshu .. Why this is happening ? 😒 bcse of I'm using a Syntax String that is scan.next();
- Now if u want to print all Characters with Space also, then You have to use another syntax.
- Syntax to print all character from console:
String st = scan.nextLine();
System.out.print(st);
- Next Line means console se aaye data me ek line ke character ke bad next line me aaye character ko print kiya jata hai .
Comments
Post a Comment