Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Weird Errors  RSS feed

 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm trying to get calculate the GCD of tow numbers; My code is not complete and I came across these errors:

recursivegcd.java:18: error: <identifier> expected
        public static void int gcd(int a, int b)
                          ^
recursivegcd.java:18: error: '(' expected
        public static void int gcd(int a, int b)
                           ^
recursivegcd.java:18: error: invalid method declaration; return type required
        public static void int gcd(int a, int b)
                               ^
3 errors


 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is void int?  void means method has no return type.  int means method returns int type.  You can't have both together, its a wrong method declaration.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you have a look on your other post?
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what comes out after running the code:

Enter first number(must be larger than the second):18
Enter second number:12
The GCD of 12 and 6 is 6The GCD of 18 and 12 is 0


This is not wanted I wanted what should I do?

It should be:
The GCD of 18 and 12 is 6



 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this good?

 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting the desired output?
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

You are unnecessarily declaring a new local variable. Your variable names are incorrect format (they shou‍ld have capitalLettersIn). I think you are  misinterpreting Euclid's algorithm. Stop writing code and write down Euclid's algorithm the quick way with the remainder operator. You do not need repeated subtraction as in Euclid's original. What are you going to do if:-
  • 1: Both numbers are equal to 0?
  • 2: One number is equal to 0 and the other is non‑zero?
  • 3: Both numbers are equal to each other and non‑zero?
  • As far as I know there is no requirement for the numbers to be in any order.
     
    Lindsey Brooks
    Ranch Hand
    Posts: 58
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How about this?

     
    Lindsey Brooks
    Ranch Hand
    Posts: 58
    Java
     
    Campbell Ritchie
    Marshal
    Posts: 55793
    164
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In which case wikihow missed out that corner case. gcd(n, 0) is equal to n.
    Don't print error messages from a method intended to return something. Don't use System.exit. It is only an error when both arguments are 0.
    Don't create multiple Scanners when you only need one.
    Your variable capitalisation is not correct. You don't need a keepa variable.
    A recursive method shou‍ld not print things, but shou‍ld return a result. It stops when it reaches a base case, and my bullet points 2 and 3 together constitute the base case.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!