I'ver got 5 errors with the % method and one with the main method. Please help if able, thanks


#1




Calculator.java:36: error: illegal start of type
else { //start of modulo else block
^
Calculator.java:36: error: ';' expected
else { //start of modulo else block
^
Calculator.java:37: error: illegal start of type
return a%b;}//end of modulo else block
^
Calculator.java:37: error: ';' expected
return a%b;}//end of modulo else block
^
Calculator.java:37: error: expected
return a%b;}//end of modulo else block
^
Calculator.java:39: error: class, interface, or enum expected
public static void main(String [ ] args) {//start of main block
^
6 errors


  public int modulo(int a, int b) {//start of block 7
    if (b==0) {//start of if statement block for modulo
     	System.out.println ("Error! Dividing by zero is not allowed.");
   		 return 0; }//end of if statement block for modulo
  } //end of block 7
 		esle { 
    	return a%b;}

 public static void main(String [ ] args) {//start of main block
  
  
  
  }//end of main block
  
  
 }//end of block 1 for class Calculator


#2

You misspelled else :smile:


#3

Thanks, I will start checking for misspelled words when I have errors.


#4

You really need to do something about your formatting, what you're doing is very bug-prone.

Your code posted above should look like:

public int modulo(int a, int b) {
    if (b == 0) {
        System.out.println("Error! Dividing by zero is not allowed.");
        return 0;
    }
    else {
        return a % b;
    }
}

public static void main(String[] args) {

}
  • no comments for start/end of blocks
  • space around operators
  • no space between function name and (
  • closing } on its own line
  • 4 spaces or tab character as indentation (whichever your editor does on TAB)

#5

Thanks for your help. I was starting to put return statements like {return a % b} with { } on the same line. I was trying to find a way to keep up with the { } to make sure they were all pairs. I thought it was a matter of personal preference as to the formatting. I didn't realize that formatting could cause bugs. Does using comments increase the chance of causing bugs or is it just annoying to other people reading the code. I admit my block start and end comments were unnecessary. I would like to know the answer about this, but also I want to code the professional way, so I will take a lesson from you.

Thanks.


#6

ionatan, I will redo the calculator project keeping your instructions in mind.


#7

You're right about that it's a matter of preference. The goal is making it as easy as possible to reason about the code and formatting-related bugs would be from not seeing the code for what it is.

Above all be consistent. Unless you can argue for doing something differently, just do what you find in documentation/high quality projects/whatever seems more common.

There's nothing saying that you mustn't do things differently, it's just that it's probably a bad idea. You gotta learn the rules and their motivations before you get to break them kind of thing.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.