Someone, Please Help Debug This Code - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Someone, Please Help Debug This Code (1046 Views)
| Someone, Please Help Debug This Code by lapo(op): 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()); } } |
| Re: Someone, Please Help Debug This Code by toke111: 9:44pm On Mar 07, 2021 |
Inventory.trophy = new Inventory();. ----wrong Inventory trophy = new Inventory();. ----wright |
| Re: Someone, Please Help Debug This Code by just2endowed: 9:54pm On Mar 07, 2021 |
toke111:Why so? |
| Re: Someone, Please Help Debug This Code by 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 |
| Re: Someone, Please Help Debug This Code by lapo(op): 10:47pm On Mar 07, 2021 |
just2endowed:Thanks a lot. |
| Re: Someone, Please Help Debug This Code by stanliwise(m): 10:56pm On Mar 07, 2021 |
lapo:Show your imports, what the heck is trophy? Is it an object? |
| Re: Someone, Please Help Debug This Code by lapo(op): 10:57pm On Mar 07, 2021 |
toke111:Thanks so, so much. Once I removed the dot separator, the code executed immediately |
| Re: Someone, Please Help Debug This Code by lapo(op): 11:13pm On Mar 07, 2021 |
lapo:
|
| Re: Someone, Please Help Debug This Code by lapo(op): 12:41am On Mar 08, 2021 |
toke111:Thanks so much, boss. May God continue enrich your knowledge. Amen. |
| Re: Someone, Please Help Debug This Code by etoluw: 1:59pm On Mar 08, 2021 |
lapo:pls what is the name of the app you are using? |
| Re: Someone, Please Help Debug This Code by toke111: 5:00pm On Mar 08, 2021 |
lapo:You welcome |
| Re: Someone, Please Help Debug This Code by lapo(op): 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 users input. Please, can someone guide me? */ |
| Re: Someone, Please Help Debug This Code by lapo(op): 4:56pm On Mar 09, 2021 |
etoluw:It's called Dcoder Available on Play store. |
| Re: Someone, Please Help Debug This Code by Etinosa1234: 7:53am On Mar 10, 2021 |
lapo:To get user 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 ![]() |
| Re: Someone, Please Help Debug This Code by lapo(op): 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. |
Someone, Please Help Debug This Code • Can Somebody Help Me Debug This? • Debug This Code At Your Spare Time. • 2 • 3 • 4
Free Browsing Cheat For Airtel, Mtn, Glo And 9mobile • 5 Weeks Online Graphics Design Class Starting from January 2022 Register Now • Php Developer Needed For Modification On An Already Existing Website!
