Hey,
I was trying to play around a little bit after learning creating multiple classes and stuff.However,i encountered a strange problem with reading a value from the user and then storing it in a variable.The usual way i do it is.But when i trying to print the contents of the variable "variableName" the compiler throws a lot of errors .I am attaching how i have tried that out in my codeScanner variableName=new Scanner(System.in); System.out.println(variableName.nextLine());import java.util.Scanner; class laptop{ private String modelNumber; private boolean hasFan; private float ramSpeed; protected int numCores; //private String input; //public int num; private boolean newInput; public laptop(String modelNumber,boolean hasFan,float ramSpeed,int numCores){ this.modelNumber=modelNumber; this.hasFan=hasFan; this.ramSpeed=ramSpeed; this.numCores=numCores; } public void getInfo(){ System.out.print("This Laptop is "+modelNumber+" model of laptop\n"+ "Fan :"+hasFan+" Number of Cores :"+numCores+ "Ram Speed :"+ramSpeed); } public void setInfo(){ System.out.println("What do you want to modify?");//Assuming user types no:1 for changing the status of hasFan Scanner num=new Scanner(System.in); System.out.println(num.nextInt()); switch(num) { case 1: { System.out.println("Enter the desired status"); Scanner newInput=new Scanner(System.in); System.out.println(newInput.nextBoolean()); System.out.println(newInput); this.hasFan=newInput; break; } } } }
And the main program ispublic class Implement{ public static void main(String[] args){ laptop objectLaptop=new laptop("G580",true,3.2f,8); //apple objectApple=new apple(true); objectLaptop.getInfo(); objectLaptop.setInfo(); } }
As you can see this is not a very functional code but only to cement my learning of the concept.Without the setInfo() in the laptop class the program functions as desired but i intend to ask the user if he wants to modify something and then reflect the same.