Jack Malgam

Greenhorn
+ Follow
since Jun 07, 2012
Jack likes ...
Firefox Browser Java
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jack Malgam

I have found that typing javac Program.java, followed by java Program, every time I want to run a program is annoying. However, there's no need to swap over to a GUI just for that one feature, since I'm still learning the language. I suspect that this is a common gripe among Java beginners. Fortunately, there is a solution that lets you sharpen your coding skills while saving yourself the trouble of typing commands into the console over and over again! Batch files are very easy to adjust to your needs, and let you reduce all that repetitive typing down to a little programming and one click.

What are batch files? Essentially a set of commands that the Windows console runs. Batch files let you save all those commands you type over and over again into a list that executes when you click it. Batch files can be extremely useful, but they can also mess up your computer! For this reason you should avoid running batch files made by other people, especially if you do not understand what the batch file's commands will do. With that disclaimer out of the way, let's get on to a nifty way to save some keystrokes!

If you've ever coded a Java program in Notepad, you have the skills to write a batch file. The format is very simple, no headers or footers, all you have to do is save the commands with the ".bat" extension and Windows will recognize it as a batch file. Commands are not case-sensitive, but it's good form to type your batch commands in UPPERCASE so you can easily distinguish batch commands from commands that will be executed by the console. Batch files will also run out of the directory they are placed in, so put your batch file in the same directory as your .java program that you want compiled and run.

On to the good stuff, here's a .bat file I made for a program called "Converter":

@ECHO OFF
TITLE Converter
javac "Converter.java"
PAUSE
ECHO.
java "Converter"
PAUSE


Most of the code is easy to understand, the javac statement is one we've all typed before, and "PAUSE" seems self-explanitory enough. Let me break this down line-by-line.

@ECHO OFF
-Tells the batch file not to print (or in the language of batch files, echo) all the commands in the file while it's executing them.

title
<title>
-Sets the title of the console window that the batch file will open to execute your commands.

javac <program.java>
-We all know what javac does, compiles a .java file. That's the meat of what this batchfile does, except it saves you the bother of typing it over and over every time you make a small change.

PAUSE
-Pauses command execution until it recieves keyboard input. I put one right after javac so the batch file does not charge on before I can read the debugging messages. That way I know if compiling was successful, or what I need to fix. Best of all, if the program does not compile, I can click the "X" button in the upper-right corner and make some adjustments, without being forced to run the rest of the commands.

ECHO.
-ECHO with a period directly after it creates a blank line. An excellent spacer, much like System.out.println(""); in Java.

java <program>
-Runs your program. A cool thing to note is that if your program did not compile properly, the previous version of your program is still saved as the .class file. The .class file is what is actually run, so you can test out your old iteration if you so desire, or just close the window.

PAUSE (again)
-Yet another pause. I have this so if my program is simple, such as a math formula that calculates a result and prints it, the window will remain open until I press a button.

This simple batch file can be easily changed for any program you are writing. Replace every time the word "Converter" is mentioned with the title of your program, save it as a .bat, and you can now compile and run your java program in a single click. No more opening the console and searching for a directory, no more typing the same code over and over again just to debug a glitch, and you even get a nice named console window!

These commands work on Windows 7, and since they are based on Microsoft's MS-DOS, you should be able to run them on XP as well. Mac has files that work similarly to batch files, but since I don't have a Mac I can't advise on those. Again, be careful running batch files you haven't written or do not fully understand! Batch files can be used to screw up your computer, just like many other types of code. I take no responsibility for what you do on your own computer, and share this information only with the hope that it might be helpful to other aspiring Java programmers.

If you are interested in using more advanced batch files to launch your programs, such as conditional statements to launch or compile different programs depending on the situation, I found this site to be very helpful: http://www.computerhope.com/batch.htm

Hope this is of use to someone! The Coderanch community has been very helpful to me, and I'd like to give back a mite.
7 years ago
Heyo,

Your error is due to Java trying to shoehorn String data into a Double. That won't work, and thankfully you don't have to make it work from the sound of things. All you have to do is tell the misguided user that they are trying to hammer a square peg into a round hole. However, first you have to tell Java to disregard anything that's not round. To that end, have you considered the Scanner.hasNextDouble() method?

hasNextDouble() is one of the hasNext () group of methods, designed to let you identify if Scanner input is acceptable before your program chokes on it. It returns a boolean true|false if the next piece of input is acceptable to its format, but most importantly does not advance the Scanner. This means you can check the input, then if it's what you want to use you can take it. If not, you can carry out a different set of instructions.

The Oracle documentation is here: http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextDouble%28%29 Since they can be a little opaque at times, here's an example of one way you can use hasNextDouble.




Hopefully things are a bit clearer now! Oh, and if you have the book Just Java 2, check out Chapter 17 "Simple Input Output". The author has some good clarification on the Scanner class, and Pg. 393 explains more about the hasNext methods.
7 years ago
Hi folks,

I'm using a sledgehammer to smash a gnat. Working on a little program to convert Celsius to Fahrenheit and decided to use BigDecimals so I can get better acquainted with how Java uses math on BigDecimals. Just to be clear, I know a float would do the job but I want to use BigDecimal for the practice. Anyway, I noticed after some bugtesting that I can't drop regular numbers into a BigDecimal math equation, I have to declare each number in the equation a BigDecimal. This is kind of bulky, even though it makes perfect sense, and I was wondering if there was shorthand for the declarations.

Here's a chunk of my code. This compiles just fine and calculates the temperature acceptably, but I'd like to avoid, for instance, having to declare 'new BigDecimal("9.0")' if supported by the language.



Thanks!
7 years ago
Thanks a lot folks, removing the "-" worked like a charm and helped me bum out some unneeded code. Much prefer using this for simple tasks instead of a StringBuffer!

In case anyone looks at this for reference down the line, the final code for right and left padding is:



Hope this helps someone else!
7 years ago
Howdy,

I am trying to right-justify some text in the console. I have read that the best way to do this is to use String.format(), but every time I use the following code:



I get the following error:



This really irks me, since this code works as expected to left-justify text:



If I'm reading the console output right, the problem is with the "#" flag. I could not find an understandable entry on the Java documentation regarding these flags.

Thanks in advance!
7 years ago
Howdy everyone, I'm just a wet behind the ears rookie who's taking a class on Java. I have some experience with JavaScript and know it's not the same thing by a long shot, and I have worked with XHTML and CSS a bit.

I know that this site's Java forum is a different branch, but I didn't want to muck up all the nice questions there with my first post. Hope I can learn a lot from the Coderanch!
7 years ago