Hey Cyan. I don't have time to write one of my own, but I do have some things I'd like to mention about your code that I'm not particularly fond of!1) Your package name doesn't follow the established naming convention. It should be a reversed internet domain name with that package name at the end. For me, for example, it could be "com.calvinrempel.proper". I'd leave out "calculator" from the package name because then your class can simply be called "Calculator" and you would be free to have other things in the package as well, like "HelloWorld".2) Calling System.exit isn't really needed. The JVM will shutdown just fine without it when it reaches the end of main. It is sometimes useful in more complex programs where you can trigger the exiting of the program from elsewhere in the code - especially when you are running many daemons, since System.exit will shut them all down. The other time it's useful is when you want to return an error code. However, in this case, you are always returning 0 for success (correctly).3) Constants should be declared as such. In this case, you should get rid of "EXIT_SUCCESS" completely, but assuming it had a good reason to be there, it should be "private static final int EXIT_SUCCESS;" Similarly, other constants, such as the version, should be declared final as well.
4) Another matter of convention, but as a general rule in Java, method names are camel case, so "iscommand" should be "isCommand".
5) In "isCommand" you've named your parameter "myInput" which, in this case, is true. However, it isn't a very descriptive name because (a) at this point, the data passed in may have come from user input or it may not have, we don't know at this point, and (b) all parameters are input to a method, so taking (a) into consideration, the only thing this name tells us with certainty is that the data is intended to be input to the method. I'd recommend a more descriptive name, even "command" for example.
6) You've gone comment crazy! The amount of comments you've put in simply pads it and actually makes it more difficult to read. Keep in mind that when you are commenting, you are 99.9% of the time writing comments for other programmers or yourself. In other words, people who understand the language and how it works. What they may not understand is your particular logic. So for example, there is no real value in this comment:
53px; background-color: rgb(252, 252, 252);">//Ask user for next input.
System.out.println("Please input a command.");
It is obvious what that code does. Even if someone didn't know Java, they would recognize that this is going to print out the message asking for the next input. The comment is just getting in the way. Similarly, this is not needed either:
//Check if the input given was in fact a number. If it was add it to the 'left' variable.
if(input.hasNextFloat())
left = input.nextFloat();
7) You are declaring left, right, and result several times per execution. You only need to declare them once outside of the while loop. Some people prefer to declare all variables at the beginning of a function, some say you should declare them when you need them. But I think just about everyone would agree that you shouldn't declare variables every iteration of a loop.-height: 18.200000762939453px; background-color: rgb(252, 252, 252);">
8) Your main function is very long. Again, as a general rule, it's better to break long methods up into smaller methods because (a) they are easier to test and (b) it's easier to read.
9) Your switch statement could be made more efficient. Switches on Strings are, most times, not very efficient, for a few reasons. If a hash table is used for the switch (which it may, but not necessarily, be) then hashing the String takes some time. Depending on collisions, it may also require equality checks which, as you know, require a method call "equals". If the switch is expanded into a series of "if else" statements by the compiler, then those equal methods will definitely need to be called. In this case, you are only using single characters in your switch, so you can the String method charAt() to get the operation code as a char. Since a char is an integer type, comparisons / hashing will be much faster and your switch will be more efficient.
10) A logic error, I think. "if(left != 0 && right != 0) result = left/right;" You are doing an extra equality check that you don't need to do. It shouldn't matter whether left is 0 or not, 0 / x is valid.
11) Even though you've validated your input. It is good practice to include a "default" case at the end of a switch. Even if you think it's unreachable, put it in and throw a runtime error with some info about the value. That way, when it screws up in the future somehow, you will have somewhere to start looking.-size: 15px; line-height: 18.200000762939453px;">
12) Use Javadocs!
13) For programs like this, I like to make the class usable from other classes. In this case, the most you could do from another class is check whether or not a command is valid. However, if you pulled all the functionality out into separate methods and in main simply create a new instance of ProperCalculator and call some function "run" or whatever, then you could use all the functionality of this class from another and at the same time let this class be executable on it's own.
14) Finally, just a little trick for the "iscommand" method.
public boolean isCommand(String command) { if (command.length() != 1) { return false; }
r: rgb(252, 252, 252); color: rgb(51, 51, 51); font-family: Arial, Tahoma, Calibri, Verdana, Geneva, sans-serif; font-size: 15px; line-height: 18.200000762939453px;"> String validCommands = "asmdq"; char c = command.charAt(0); return (validCommands.indexOf(c) != -1);}
I think that's all I've got. Keep in mind that in some cases I'm being picky because you titles this thread "Proper Java Coding". Also, no, I see no reason for Thread.sleep(). Why make the program artificially slower? And, instead of "while(!input.hasNext()) {}" perhaps you could do something like:
while (!input.hasNextFloat()) { System.out.println("Please enter a numeric value: ");}float = input.nextFloat();
And it's totally fine, it's a sentinel-control loop waiting for valid user input.