/* * JavaTemplate.java * Created by Richard Johnson * 9/28/04 * Provides code for use in a variety of Java programs */ import javax.swing.*; // for GUIs import java.util.*; // for tokenizing import java.text.*; // for formatting import java.io.*; // for input/output public class JavaTemplate { public static void main (String[] args) throws IOException, FileNotFoundException { // ============================================== // Preparation // ============================================== // declare all variables, objects, etc., for future use in the program String inputMessage1, outputMessage1, endOfProgramMessage, guiTitle1; String userInput1 = ""; // initialize variable for use w/ tokenizer double aDouble, newDouble; // some doubles String formattedDouble; String firstToken, secondToken; String fileInputString, fileOutputString; // create an object to get input from the keyboard BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); // declare tokenizer objects for breaking up strings into tokens StringTokenizer tokenizer1, tokenizer2; // create decimal formatting object DecimalFormat twoDecimal = new DecimalFormat("0.00"); // prepare for getting input from a file BufferedReader inFile = new BufferedReader(new FileReader("prog.out")); // ============================================== // Input // ============================================== inputMessage1 = "As an example, enter your first name and last name.\n\n"; System.out.println(inputMessage1); // display input message to the console userInput1 = keyboard.readLine(); // get input from user w/ the keyboard System.out.println("This is what was just entered by the user: " + userInput1); // display input message and get input w/ simple GUI userInput1 = JOptionPane.showInputDialog(inputMessage1); System.out.println("This is what was just entered by the user: " + userInput1); // get input from a file fileInputString = inFile.readLine(); System.out.println("This is what was read from the file: " + fileInputString); // ============================================== // Processing // ============================================== // create a tokenizer object for breaking up a string into tokens tokenizer1 = new StringTokenizer(userInput1); // tokenizing a string firstToken = tokenizer1.nextToken(); // get first token in string, store secondToken = tokenizer1.nextToken(); // get next token in string, store // a sample calculation aDouble = 15.7892; // just a sample double newDouble = 2 * aDouble; // ============================================== // Output // ============================================== // display output message w/ simple GUI outputMessage1 = "This is a message to the user for output.\n\n"; guiTitle1 = "GUI Title 1"; JOptionPane.showMessageDialog(null,outputMessage1,guiTitle1,JOptionPane.PLAIN_MESSAGE); // decimal formatting formattedDouble = twoDecimal.format(newDouble); // file output fileOutputString = "This is a string and the double: " + formattedDouble; PrintWriter outFile = new PrintWriter(new FileWriter("prog.out")); outFile.println(fileOutputString); outFile.close(); // close the file endOfProgramMessage = "End of Program"; System.out.println(endOfProgramMessage); System.exit(0); // terminate GUI thread } }