Java Exception | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java Exception

package learn2; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.println("Enter your name "); String name = input.nextLine(); // I entered No. 2 , and still working. why there is no exception here !!!!!! System.out.println("You are welcome "); } } // Why the code is executed even if I put an int. I declared the variable name as a string.

26th Jun 2017, 12:53 AM
John
1 Answer
+ 1
2 is read in as a string. So there is no exception. Also, 2 is a valid string.
26th Jun 2017, 1:32 AM
Venkatesh Pitta
Venkatesh Pitta - avatar