//****************************************************************************** // problem2.java: Applet for MAS962 ps#1 // // Author: Ingeborg Endter 9/12/97 //****************************************************************************** import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; //============================================================================== // Main Class for applet problem2 // //============================================================================== public class problem2 extends Applet { TextField mInputField; //for user input Button mTheButton; //Button TextArea mOutputBox; //for output //String mFilePath = "D:\\IEROOT\\MLCourses\\MAS962\\PS1\\problem2\\code.txt"; //***** TEST - local String mFilePath = "http://acg/mas962/ps1/code.txt"; //***** TEST - for web //-------------------------------------------------------------------------- public void init() { resize(540, 382); mInputField = new TextField("Type something here",40); mTheButton = new Button("Transform it!"); mOutputBox = new TextArea(); GridBagConstraints gbc = new GridBagConstraints(); GridBagLayout gridbag = new GridBagLayout(); setLayout(gridbag); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(mInputField, gbc); add(mInputField); gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.VERTICAL; gridbag.setConstraints(mTheButton, gbc); add(mTheButton); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; gridbag.setConstraints(mOutputBox, gbc); add(mOutputBox); } //-------------------------------------------------------------------------- public boolean action (Event event, Object obj) { // when the button is clicked if (event.target == mTheButton) { String someInput; String replacement; StringBuffer someOutput = new StringBuffer(); char tempChar; try { // get the user's input from the TextField someInput = mInputField.getText(); int length = someInput.length(); // read the user's input a char at a time, then search the codes file // for the replacement text for (int i=0; i= 'A' && tempChar <= 'z') { replacement = SearchStream(mFilePath, tempChar); someOutput.append(replacement); } else { someOutput.append(' '); } } } catch (StringIndexOutOfBoundsException e) { System.err.println(e); } mOutputBox.setText(TextWrap(someOutput.toString(),50)); return true; } return false; } //****** // SearchStream: searches a file at some URL, which contains lines // consisting of a "key" (a character) and its replacement (a string). // The method reads the file a line at a time, searching the keys, // and returns the replacement string if a match is found. //****** public String SearchStream(String inFile, char ch) { String testString, returnString; int keyIndex = 0; //the key is at this index int replaceIndex = 2; //the replacement string is at this index URL webFile; //***** TEST - for web DataInputStream inStream; try { webFile = new URL(inFile); //***** TEST - for web inStream = new DataInputStream(webFile.openStream()); //***** TEST - for web //inStream = new DataInputStream(new FileInputStream(inFile)); //***** TEST - local testString = inStream.readLine(); //read first line while (testString != null) { if (testString.charAt(keyIndex) == Character.toLowerCase(ch)) { returnString = testString.substring(replaceIndex); inStream.close(); return returnString; } testString = inStream.readLine(); } //if we got here, the substring was not found System.out.println("Substring not found"); inStream.close(); } catch (MalformedURLException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } catch (StringIndexOutOfBoundsException e) { System.err.println(e); } return null; } //****** // TextWrap: wraps text in a text area. // Input: a String and the size of the line in characters // Output: the formatted string //****** public String TextWrap(String inString, int lineSize) { String outString = ""; //initialize the string int numChars = inString.length(); char tempChar; int charCount = 0; try { for (int i=0; i< numChars; i++) { tempChar = inString.charAt(i); if (tempChar == '\n') charCount = 0; //reset counter at each newline else charCount++; if (charCount % lineSize == 0) outString = outString + '\n'; outString = outString + tempChar; } } catch (StringIndexOutOfBoundsException e) { System.err.println(e); } return outString; } } //end class problem2