| View previous topic :: View next topic |
| Author |
Message |
Cx Master Cheater
Reputation: 0
Joined: 27 Jul 2007 Posts: 367
|
Posted: Mon Jul 21, 2008 8:03 pm Post subject: Java help... |
|
|
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 |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 22, 2008 4:42 am Post subject: Re: Java help... |
|
|
| 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 |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Tue Jul 22, 2008 9:26 am Post subject: |
|
|
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 |
|
 |
Cx Master Cheater
Reputation: 0
Joined: 27 Jul 2007 Posts: 367
|
Posted: Wed Jul 30, 2008 11:11 am Post subject: |
|
|
Ah. Thank you both.
_________________
armed with this small butterfly net
i will face the world alone
& never be lonely. |
|
| Back to top |
|
 |
|