₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,092 members, 8,420,278 topics. Date: Thursday, 04 June 2026 at 03:31 PM

Toggle theme

Someone, Please Help Debug This Code - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingSomeone, Please Help Debug This Code (1046 Views)

1 Reply (Go Down)

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:
Inventory.trophy = new Inventory();. ----wrong

Inventory trophy = new Inventory();. ----wright
Why so?
Re: Someone, Please Help Debug This Code by SUPREMOTM: 10:45pm On Mar 07, 2021
just2endowed:
Why so?
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:
Why so?
Thanks a lot.
Re: Someone, Please Help Debug This Code by stanliwise(m): 10:56pm On Mar 07, 2021
lapo:
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());
}


}
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:
Inventory.trophy = new Inventory();. ----wrong

Inventory trophy = new Inventory();. ----wright
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:
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 lapo(op): 12:41am On Mar 08, 2021
toke111:
Inventory.trophy = new Inventory();. ----wrong

Inventory trophy = new Inventory();. ----wright
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:
Thanks so much, boss. May God continue enrich your knowledge. Amen.
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:
Thanks so much, boss. May God continue enrich your knowledge. Amen.
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:
pls what is the name of the app you are using?
It's called Dcoder
Available on Play store.
Re: Someone, Please Help Debug This Code by Etinosa1234: 7:53am On Mar 10, 2021
lapo:
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?
*/
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 grin
Re: Someone, Please Help Debug This Code by lapo(op): 10:56am On Mar 11, 2021
Etinosa1234:
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 grin
Thanks for your contribution. I know how scanner works really, but I want a guide on how to adopt it for the program. Thanks.
1 Reply

Someone, Please Help Debug This CodeCan Somebody Help Me Debug This?Debug This Code At Your Spare Time.234

Free Browsing Cheat For Airtel, Mtn, Glo And 9mobile5 Weeks Online Graphics Design Class Starting from January 2022 Register NowPhp Developer Needed For Modification On An Already Existing Website!