Can do this guys. Showing null value.Plz Help me out. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can do this guys. Showing null value.Plz Help me out.

import java.util.*; class Mobile { static String name; static double cp,sp,d; static int pid; static void input() { Scanner sc = new Scanner(System.in); System.out.println("Enter name of product"); String name = sc.next(); System.out.println("Enter price"); double cp = sc.nextDouble(); System.out.println("Enter pid"); int pid = sc.nextInt(); } static void calculation() { d=(20.0/100)*cp; sp=cp+d; } static void display() { System.out.println("Product name: "+name); System.out.println("Final Price: Rs"+sp); System.out.println("----"+pid+"----"); } public static void main(String args[]) { Mobile ph = new Mobile(); ph.input(); ph.calculation(); ph.display(); } }

20th May 2020, 5:22 PM
Student 31
Student 31 - avatar
1 Answer
+ 1
You're redefining your variables in the input method, so they are local to the input method itself and destroyed at the end of the method. Remove the type from the redeclaration of each. name = sc.next() cp = sc.nextDouble() pid = sc.nextInt()
20th May 2020, 5:34 PM
ChaoticDawg
ChaoticDawg - avatar