Jump to content

[JAVA] basic debugging

Go to solution Solved by NoRomanBatmansAllowed,

Go over to /r/JavaHelp for a lot more (and usually a lot quicker) people who help.

 

So what you're doing in line 20 is you're defining a method. A method has 2 arguments. The thing with methods in Java, unlike pleb languages like python or JS, is that you need to set the type of variable you'd input. So in this case:

 

public static void addNums(int a, int b){

 

and you would not redefine the variables after it.

 

You don't have to assign the result to an int either, so a cut down version of this would be

public static void addNums(int a, int b){
	return a+b;
}

 

Hey im learning java and ive been given a task to debug a code and ive been able to debug it for all of it except for 2 errors and im unable to understand them. please help me understand them.

public class Debug5{
   public static void main(String[] args){
	int a = 0, b = 0;
	while(sum < 100){
		addNums(a,b);
		System.out.println(sum);
		b += 5;
		a++;
	}//end while loop
}//end main	

public static void addNums(a, b){ //THIS IS LINE 20
	int a, b;
	int sum = a + b;
	return  sum;
}//end add nums
   
}

HERE are the errors

Debug5.java:20: error: <identifier> expected
public static void addNums(a, b){
                            ^
Debug5.java:20: error: <identifier> expected
public static void addNums(a, b){

 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/893931-java-basic-debugging/
Share on other sites

Link to post
Share on other sites

Go over to /r/JavaHelp for a lot more (and usually a lot quicker) people who help.

 

So what you're doing in line 20 is you're defining a method. A method has 2 arguments. The thing with methods in Java, unlike pleb languages like python or JS, is that you need to set the type of variable you'd input. So in this case:

 

public static void addNums(int a, int b){

 

and you would not redefine the variables after it.

 

You don't have to assign the result to an int either, so a cut down version of this would be

public static void addNums(int a, int b){
	return a+b;
}

 

Want to know which mobo to get?

Spoiler

Choose whatever you need. Any more, you're wasting your money. Any less, and you don't get the features you need.

 

Only you know what you need to do with your computer, so nobody's really qualified to answer this question except for you.

 

chEcK iNsidE sPoilEr fOr a tREat!

Link to comment
https://linustechtips.com/topic/893931-java-basic-debugging/#findComment-11018971
Share on other sites

Link to post
Share on other sites

When creating a method, you need to identify the datatype of your parameters.

public static void addNums(int a, int b){

 

Spoiler

Intel Core i7 8700k | be quiet! Dark Rock 4 | Fatal1ty Z370 Gaming-ITX/ac | 32 GB G.Skill TridentZ | 256 GB Intel® SSD 600p Series | ZOTAC GeForce® GTX 1080 Ti Mini | Fractal Design Node 304 | Cooler Master V750 | Asus MG279Q | Asus VC279 | Logitech G710+ | Corsair M65 Pro RGB

 

Link to comment
https://linustechtips.com/topic/893931-java-basic-debugging/#findComment-11018972
Share on other sites

Link to post
Share on other sites

Just now, Pangea2017 said:

public static void addNums(int a, b){

but i have already declared it inside the program, should i declare it in the parentheses as well ?

 

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/893931-java-basic-debugging/#findComment-11018974
Share on other sites

Link to post
Share on other sites

1 minute ago, bob51zhang said:

Go over to /r/JavaHelp for a lot more (and usually a lot quicker) people who help.

 

So what you're doing in line 20 is you're defining a method. A method has 2 arguments. The thing with methods in Java, unlike pleb languages like python or JS, is that you need to set the type of variable you'd input. So in this case:

 

public static void addNums(int a, int b){

gee thanks for the suggestion on  r/reddit

CPU: FX8320 @ 4.2GHZ, GPU: R9 390 PCS+Cooler: Hyper 212 Evo, MotherBoard: ASRock 970 Extreme 3 r2.0, PowerSupply: EVGA 600b 80+Bronze, Storage: seagate Barracuda 1TB SSHD & Case: Corsair Carbide 200r

Link to comment
https://linustechtips.com/topic/893931-java-basic-debugging/#findComment-11018976
Share on other sites

Link to post
Share on other sites

Just now, huracan said:

 

You should read up on Java variable scopes.

 

https://www.geeksforgeeks.org/variable-scope-in-java/

Want to know which mobo to get?

Spoiler

Choose whatever you need. Any more, you're wasting your money. Any less, and you don't get the features you need.

 

Only you know what you need to do with your computer, so nobody's really qualified to answer this question except for you.

 

chEcK iNsidE sPoilEr fOr a tREat!

Link to comment
https://linustechtips.com/topic/893931-java-basic-debugging/#findComment-11018977
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×