Skip to content

Commit 0e95fb9

Browse files
author
Harshilbhardwaj47
committed
how to take input in java
1 parent a81b233 commit 0e95fb9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

01Day1/input/input.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package DAY1;
2+
3+
import java.util.Scanner;
4+
// And this is how we import the Scanner utility in our code
5+
6+
public class Input {
7+
public static void main(String[] args) {
8+
Scanner in = new Scanner(System.in);
9+
// In JAVA we have a Scanner class that allows us to take input and this is the
10+
// statement how we call it in our main class
11+
// So lets say i need you age , your name and your grades in 'double'
12+
int age = in.nextInt();// to take age from user we call .nextInt() function from our imported Scanner
13+
// class
14+
double grade = in.nextDouble();
15+
// And this function is used to take double input from user
16+
// String name = in.next();now for string we have two functions the first
17+
// function stops taking input as soon as you hit space
18+
String name = in.nextLine();// but this function takes space between two words as well thats why i used this
19+
// in my code
20+
if (name.isEmpty()) {// Bonus information if you take input like thid String after int java will Show
21+
// you error hit is almost written in this code try to find out why XD you rhome
22+
// work. so to remove that error we need to check that if from the first input
23+
// statement name does not take any value we need to store again a value in it
24+
name = in.nextLine();// and finally your code works
25+
}
26+
System.out.println("Your Age :" + age);
27+
System.out.println("Your name :" + name);
28+
System.out.println("Your grade :" + grade);
29+
}
30+
}
31+

0 commit comments

Comments
 (0)