NewStats: 3,263,904 , 8,181,827 topics. Date: Sunday, 08 June 2025 at 05:19 PM 4914586z3e3g |
Someone, Please Help Debug This Code (980 Views)
(1) (Go Down)
lapo(m): 9:34pm On Mar 07, 2021 |
Hello all, please I can't pinpoint what I am getting wrong here. Someone help please. public class Myclass{ public static void main(String [] args){ Inventory.trophy = new Inventory(); trophy.getlastbalance(); //Inventory.goldberg = new Inventory(); trophy.name = "trophy"; trophy.initialbalance = 20; trophy.bought = 40; trophy.sold = 10; System.out.println("Stock of Trophy is "+ trophy.getlastbalance()); /*goldberg.initialbalance = 10; goldberg.bought = 24; goldberg.sold = 10; System.out.println("Stock of Goldberg is "+ goldberg.getlastbalance()); */ } } public class Inventory{ String name = "trophy"; int initialbalance; int bought; int sold; public int getlastbalance(){ return (initialbalance + bought - sold); //System.out.println("Stock of Trophy is "+ trophy.getlastbalance()); } } |
toke111: 9:44pm On Mar 07, 2021 |
Inventory.trophy = new Inventory();. ----wrong Inventory trophy = new Inventory();. ----wright 2 Likes |
just2endowed: 9:54pm On Mar 07, 2021 |
toke111: Why so? 1 Like |
SUPREMOTM: 10:45pm On Mar 07, 2021 |
just2endowed: Inventory ------- Class Name trophy ----------- Object Reference Variable new --------------- keyword for creating an object Inventory ( ) ---- Constructor of Inventory class SYNTAX for creating objects from a class is: Inventory trophy = new Inventory ( ) Inventory goldberg = new Inventory ( ) After you have created the 2 Inventory objects above, you can go ahead to set values for the properties of the objects or invoke the methods in the class; you do that by using the member access operator "." @toke111 is right 4 Likes |
lapo(m): 10:47pm On Mar 07, 2021 |
just2endowed: Thanks a lot. |
stanliwise(m): 10:56pm On Mar 07, 2021 |
lapo: Show your imports, what the heck is trophy? Is it an object? |
lapo(m): 10:57pm On Mar 07, 2021 |
toke111: Thanks so, so much. Once I removed the dot separator, the code executed immediately |
lapo(m): 11:13pm On Mar 07, 2021 |
lapo:
|
lapo(m): 12:41am On Mar 08, 2021 |
toke111:Thanks so much, boss. May God continue enrich your knowledge. Amen. |
etoluw: 1:59pm On Mar 08, 2021 |
lapo:pls what is the name of the app you are using? |
toke111: 5:00pm On Mar 08, 2021 |
lapo:You welcome |
lapo(m): 4:44pm On Mar 09, 2021 |
public class Myclass{ public static void main(String [] args){ Inventory trophy = new Inventory(); trophy.name = "trophy"; trophy.initialbalance = 20; trophy.bought = 40; trophy.sold = 10; System.out.println("Stock of Trophy is "+ trophy.getlastbalance()); Inventory goldberg = new Inventory(); goldberg.name = "goldberg"; goldberg.initialbalance = 10; goldberg.bought = 24; goldberg.sold = 10; System.out.println("Stock of Goldberg is "+ goldberg.getlastbalance()); } } public class Inventory{ String name; int initialbalance; int bought; int sold; public int getlastbalance(){ return (initialbalance + bought - sold); } } /*This runs perfectly now. But am looking at the possibility of using Scanner class to get s input. Please, can someone guide me? */ |
lapo(m): 4:56pm On Mar 09, 2021 |
Etinosa1234: 7:53am On Mar 10, 2021 |
lapo: To get input, u can use either BufferedReader or Scanner... I prefer Scanner tho First create a Scanner object Scanner scan = new Scanner(System.in); To get an Integer input scan.nextInt(); To get String input scan.nextLine() or scan.next(); To get double input scan.nextDouble(); And so on.. I'm still Learning Java like u but I'm focusing on app development ... I dey find money ![]() 1 Like |
lapo(m): 10:56am On Mar 11, 2021 |
Etinosa1234: Thanks for your contribution. I know how scanner works really, but I want a guide on how to adopt it for the program. Thanks. |