netbeans - what is the error in this java code? -
import java.util.scanner; public class hello{ public static void main(string[]args){ scanner input=new scanner(system.in); system.out.println("enter ur name"); string name=input.nextline(); system.out.println("enter ur gpa"); double gpa=input.nextdouble(); system.out.println("hi"+name+",ur gpa "+ gpa); } }
i getting next exception:
exception in thread "main" java.util.inputmismatchexception @ java.util.scanner.throwfor(scanner.java:909) @ java.util.scanner.next(scanner.java:1530) @ java.util.scanner.nextdouble(scanner.java:2456) @ hello.main(hello.java:12)
the code work fine f proper values passed expected programme i.e string , double. may want add together exception handling it
scanner input=new scanner(system.in); string name = ""; double gpa = 0; boolean correctnameenetered = false; boolean correctgpaeneterd = false; while(true){ seek { if(!correctnameenetered){ system.out.println("enter ur name"); name=input.nextline(); correctnameenetered = true; } }catch(inputmismatchexception ex) { system.out.println("please provide string value name"); continue; } seek { if(!correctgpaeneterd) { system.out.println("enter ur gpa"); gpa = double.parsedouble(input.next()); correctgpaeneterd = true; } }catch(numberformatexception ex) { system.out.println("please provide integer or decimal value gpa"); continue; } break; } system.out.println("hi"+name+",ur gpa "+ gpa); java netbeans
No comments:
Post a Comment