Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Java help...

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Cx
Master Cheater
Reputation: 0

Joined: 27 Jul 2007
Posts: 367

PostPosted: Mon Jul 21, 2008 8:03 pm    Post subject: Java help... Reply with quote

I've just started to learn Java. I'm not too familiar with it yet and I don't really know any of the conventions. This calc program works (though because of the way I made it it's not useful at all), but I'm not sure that the code is "quality".

Especially because of the exception catching and stuff. An exception can be thrown at either the Integer.parseInt or Integer.toString (or the exception that I throw), so do I just enclose the whole thing in a try block?

asfgagdsgs it just seems bad.

Please look over it and tell me if anything should be fixed for better code, etc.

pastebin.

Code:
public class calc {
   public static void main(String[] args) {
      if (args.length == 3) {
         try {
            String errorOverflow = "Int overflow: Result out of integer range.";
            int a = Integer.parseInt(args[1]);
            int b = Integer.parseInt(args[2]);
            int remainder = 0;

            if (args[0].equals("add")) {
               if (a > (Integer.MAX_VALUE - b)) {
                  throw new NumberFormatException(errorOverflow);
               }
               System.out.println(Integer.toString(a+b));
            }
            else if (args[0].equals("sub")) {
               if (a < (Integer.MAX_VALUE + b)) {
                  throw new NumberFormatException(errorOverflow);
               }
               System.out.println(Integer.toString(a-b));
            }
            else if (args[0].equals("mul")) {
               if (a > (Integer.MAX_VALUE / b)) {
                  throw new NumberFormatException(errorOverflow);
               }
               System.out.println(Integer.toString(a*b));
            }
            else if (args[0].equals("div")) {
               remainder = a%b;
               if (remainder == 0) {
                  System.out.println(Integer.toString(a/b));
               }
               else {
                  System.out.println(Integer.toString(a/b) + " " + Integer.toString(remainder) + "/" + Integer.toString(b));
               }
            }
            else {
               System.out.println("Error: Invalid args.\nError: Invalid operator.");
            }

         } catch (NumberFormatException e) {
            System.out.println("Invalid args.\nException: " + e.getMessage());
         }
      }
      else {
         System.out.println("Error: Invalid args.\nFormat: calc [ operator ] [ #1 ] [ #2 ]");
         System.out.println("\t\toperator: add, sub, mul, or div");
         System.out.println("\t\t#1: first number in operation");
         System.out.println("\t\t#2: second number in operation");
      }
   }
}

_________________

armed with this small butterfly net
i will face the world alone
& never be lonely.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Jul 22, 2008 4:42 am    Post subject: Re: Java help... Reply with quote

Cx wrote:
An exception can be thrown at either the Integer.parseInt or Integer.toString (or the exception that I throw), so do I just enclose the whole thing in a try block?
You might want to catch different exceptions in different situations, for example, different error messages, just ignoring the exception, etc. That's kinda only benefit I can see.

Cx wrote:
asfgagdsgs it just seems bad.
Maybe this is one of the reasons people usually don't like Java :>

I'm not an expert either, but I've made some basic programs, for example talk, and p2p-irc-with-a-tracker-thing, because they made me to make them at the uni -_- The subject of the course wasn't Java, but networking generally (including sockets -> that's why the programs). And I didn't know any Java before I this class...
Back to top
View user's profile Send private message
DoomsDay
Grandmaster Cheater
Reputation: 0

Joined: 06 Jan 2007
Posts: 768
Location: %HomePath%

PostPosted: Tue Jul 22, 2008 9:26 am    Post subject: Reply with quote

I can only suggest to utilize Scanner(a class)
Code:
import java.util.*;
class Test
{
  public static void main(String [] args)
  {
     Scanner input = new Scanner(System.in);
     
     System.out.print("Enter two numbers: ");
     int num1 = input.nextInt();
     int num2 = input.nextInt();
     System.out.println("so... pick an action: add; sub; mul; div;");
     while (!input.hasNext()); //waiting for an input
     String str1 = input.next().toString();
     
     System.out.print("Result: ");
     if (str1.equalsIgnoreCase("add"))
        System.out.println(num1 + num2);
     else
        if (str1.equalsIgnoreCase("sub"))
           System.out.println(num1 - num2);
      else
           if (str1.equalsIgnoreCase("mul"))
              System.out.println(num1 * num2);
           else
              if (str1.equalsIgnoreCase("div") && (num2 != 0))
                 System.out.println((double)num1 / num2);
              else
                 System.out.println("OK, wtf is wrong with you??"); 
     return;
  }
}
Back to top
View user's profile Send private message
Cx
Master Cheater
Reputation: 0

Joined: 27 Jul 2007
Posts: 367

PostPosted: Wed Jul 30, 2008 11:11 am    Post subject: Reply with quote

Ah. Thank you both.
_________________

armed with this small butterfly net
i will face the world alone
& never be lonely.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites