NewStats: 3,263,904 , 8,181,827 topics. Date: Sunday, 08 June 2025 at 05:19 PM 491458

6z3e3g

Someone, Please Help Debug This Code - Programming - Nairaland 6f3j8

Someone, Please Help Debug This Code (980 Views)

(4)

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

Inventory trophy = new Inventory();. ----wright

Why so?

1 Like

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

4 Likes

lapo(m): 10:47pm On Mar 07, 2021
just2endowed:


Why so?

Thanks a lot.
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?
lapo(m): 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
lapo(m): 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());
}


}

lapo(m): 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.
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?
toke111: 5:00pm On Mar 08, 2021
lapo:

Thanks so much, boss. May God continue enrich your knowledge. Amen.
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
etoluw:


pls what is the name of the app you are using?

It's called Dcoder
Available on Play store.
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 s input. Please, can someone guide me?
*/


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 grin

1 Like

lapo(m): 10:56am On Mar 11, 2021
Etinosa1234:


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 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)

Self-taught Programmers

(Go Up)

Sections: How To . 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or s on Nairaland.