1

I am trying to have my program output an error code when the input values are too high, but I can only get it to display the error message in the terminal window, not in the method return. Here is the code:

public class AckermannAdvanced {
   
   public static long ack (long m, long n) {
      long ans;

      System.out.print("\f");
      if (m > 3 && n > 1) {
         System.out.print("Input values are too high");
      } else {
         if (m == 2)      ans = (2*n)+3;
         else if (m == 1) ans = n+2;
         else if (m == 0) ans = n+1;
         else if (n == 0) ans = ack(m-1, 1);
         else             ans = ack(m-1, ack(m, n-1));
         System.out.print(ans);
         return(ans);
      } 
      return 0;
   }
}
Doga Oruc
  • 783
  • 3
  • 16
Calico
  • 149
  • 1
  • 1
  • 5
  • What is the "return window"? – PurkkaKoodari Sep 06 '14 at 23:32
  • By return window, I mean the method result, according to my IDE defines, it. I specialise more in mathematics than computer science, so forgive me if some of my terminology and programming is slightly vague/incorrect. – Calico Sep 07 '14 at 00:20
  • I am not sure what you are looking for. But maybe this will fit your request: `throw new IllegalArgumentException("Input values too high");`. – R2B2 Sep 07 '14 at 00:39
  • That wasn't exactly what I wanted, but it works. Thanks! – Calico Sep 07 '14 at 01:19
  • @R2B2 Could you delete your comment and post it as an answer? Then it can be marked as accepted, and votes can be placed. – Noctis Skytower Jan 15 '16 at 15:55

1 Answers1

0

You could use a Long with a capital L, and then it is an object representing a long. Once you return an object you could return null. Java automatically converts a long to a Long when returning, so the other returns should be the same. So

    public static Long ack(long m, long n) //...
    //...
    if (m > 3 && n > 1) {
        return null;
    }

Alternatively you could have it return an OptionalLong

public static OptionalLong ack(long m, long n) //...
//No result
return OptionalLong.empty();
//Result
return OptionalLong.of(21438999L);

https://www.geeksforgeeks.org/optionallong-orelselong-method-in-java-with-examples/

ope
  • 67
  • 1
  • 6