> and don't even bother adding a question
Its very disrespectful. It seems to be happening more frequently too.
Sometimes I think Sun should just cave and add a "homework"
section and stockpile the 10 or so assignments we always see
and all the threads answering them.
It might even give teachers pause to come up with different problems.
import static java.lang.Integer.parseInt;
import static java.lang.Integer.bitCount;
import static java.lang.Integer.toHexString;
import static java.lang.Integer.toBinaryString;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class DigitalRootCalc {
public static void main(String[] args) {
//obtain argument
String s = JOptionPane.showInputDialog("Input number");
//parse the argument, assume hexadecimal
int number = parseInt(s, 16);
int original = number;
//loop until number contains no more than one digit
while (highestOneBit(number) | 0x1 == 0x1) {
//add up the digits of the number, and assign it to number
number = bitCount(number);
}
//display result
JOptionPane.showMessageDialog(null, "Argument: " + toHexString(original) + "\nDigital Root: " + toBinaryString(number));
//ask whether to save to disk
int option = JOptionPane.showConfirmDialog(null, "Save result to disk?");
//check to see if user chose "yes"
if (option == JOptionPane.YES_OPTION) {
//initialize file chooser
JFileChooser fc = new JFileChooser();
//let user choose a file to save to
option = fc.showSaveDialog(null);
//check to see if user chose "save" instead of "cancel"
if (option == JFileChooser.APPROVE_OPTION) {
//watch out for IOException
try {
//obtain chosen file
File f = chooser.getSelectedFile();
//create FileWriter to write to file
FireWriter fw = new FileWriter(f);
//write results to file
fw.write("Argument: " + toHexString(original) + "\nDigital Root: " + toBinaryString(number));
}
//catch IOException
catch (IOException ex) {
//report exception
ex.printStackTrace();
//abort program
System.exit(1);
}
}
}
}
>catch (IOException ex) {
>//report exception
>ex.printStackTrace();
>//abort program
>System.exit(1);
> }
You know, you could at least close the streams first before killing the JVM and taking all other programs possibly running inside with it.
I once saw code like that in Websphere. Very funny.