Site hosted by Angelfire.com: Build your free website today!

[^^pizoig HOME page] 

PKDA2001: Java

On this page: {Common errors and what they mean} {Java via the DOS window} (or unix prompt) {Command Line args: main ( String args[] ) } {strings} {TextArea} (as a popup window) {A brief note on try {} and catch {}}

Java via the DOS window

(or unix prompt) The following (which i use almost extensively) works fine for dos machines (or pure unix w/out x-win, etc). 0. Copy your source files to the bin dirctory careful of course to not wipe something out. 1. javac MyHello.java say i get errors? switch to the >2 mode javac MyHello.java 2>e.out 2. From the windows main screen (eg, XP screen), i open a text file from the bin directory: Start --> NotePad --> e.out i usually tile the err window at the bottom, and my current working source file above it. For example: -[
Sample editing session]- 3. I then compile, and when i get an error. I tack on the 2>e.out at the end. Then i re-fresh/re-load the notepad file at the bottom of the screen to pull up the just created error output. This has the effect of "sort of" acting like an IDE (Integrated Development Env) - which are (when???) coming soonest. Frank, via: fleeding AT hotmail DOT com {Back to the TOP of this page}

Command Line args: main ( String args[] )

When you evoke a java program from the command prompt, you can (optionally) add command line args. >java MyFileCopier001 myfile.txt destfile.out ... args[0] args[1] ... this gets passed in via the main() method. Viz: public static void main (String[] args) { fileWriter.open_and_copy_file (args[0]); // from command-line ... } Note: The use of "args[]" is arbitrary, but most people do it that way for portability. The first arg is [0] in keeping with the zero-offset nature of arrays in java. Note: (apparently) the sunsoft notes are sketchy on this... Frank, via: fleeding AT hotmail DOT com To "trap" missing args, you can use this method: args.length public static void main (String[] args) { if (args.length < 1) { system.out.println ("*** Err: You must supply a file name"); system.out.println (" eg: java MyFileCopier001 poem01.txt"); system.out.println (" "); System.exit(1); // ie, unix std: 0 = ok, any other means err. } //-- let's go! fileWriter.open_and_copy_file (args[0]); // from command-line ... } {
Back to the TOP of this page}

Strings

In this section: {
Simple Strings} (compare methods, etc) {String Arrays} {A brief note on try {} and catch {}} See: -[the sun.com REF page]- -[String arrays]- (string arrays s[] ) -[string arrays]- the whole ball of wax!! -[]- -[]- -[]- -[]- -[]- -[]-

Simple Strings

(compare methods, etc) Very nice format and info: -[
www.javaclass.info]- Declaring: String my_option; String[] my_string_array_of_strings; Comparing: methods: x.compareTo() and x.equals() if (f1.compareTo(f2) < 0) THEN: f1 comes BEFORE f2 if (f1.compareTo(f2) > 0) THEN: f1 comes AFTER f2 if (f1.compareTo(f2) == 0) THEN: f1 EXACTLY EQUALS f2 You MAY USE: if (f1.equals(f2)) .... DO NOT use: if (f1 == f2) this compares the STORAGE ADDRESSES NOT their contents. Also note: ASCII (unicode) uppercase preceeds LOWER CASE. Hence, McDaniels would be sorted AFTER MCzongle since 'c' is HEX 0x63 and 'C' is 0x43 thus, according to ascii: 'C' < 'c' you might want to TEMPORARILY compare them using if (name1.toUpperCase() < name2.toUpperCase()) then PUT name1 before name2 Note: Using this format does NOT change name1 or name2 (see examples below)... this problem nearly drove me nuts!!! Frank, via: fleeding AT hotmail DOT com SAMPLE CODE ============================ 0. Declare and initialise the strings.... String f1, f2; f2 = "this is that"; f1 = f2; // f1 now has a copy of f2 1. System.out.println ("--- f1: >" + f1 +"<"); System.out.println ("--- f2: >" + f2 +"< \n\n"); --- f1: >this is that< --- f2: >this is that< 2. f2 = f1.toUpperCase(); System.out.println ("--- f2 = f1.toUpperCase(); gives..."); System.out.println ("--- f1: >" + f1 +"<"); System.out.println ("--- f2: >" + f2 +"< \n\n"); --- f2 = f1.toUpperCase(); gives... --- f1: >this is that< --- f2: >THIS IS THAT< 3. System.out.println ("--- Note you can end uppercase, without changing:\n"); System.out.println ("--- f1: >" + f1 +"<"); System.out.println ("--- System.out.println ( f1.toUpperCase() ); : >" + f1.toUpperCase() +"<"); System.out.println ("--- f1: >" + f1 +"< \n"); System.out.println ("--- ie: f1 remains unchanged. \n"); --- Note you can end uppercase, without changing: --- f1: >this is that< --- System.out.println ( f1.toUpperCase() ); : >THIS IS THAT< --- f1: >this is that< --- ie: f1 remains unchanged. ========================= and the actual output: --- f1: >this is that< --- f2: >this is that< --- f2 = f1.toUpperCase(); gives... --- f1: >this is that< --- f2: >THIS IS THAT< --- Note you can end uppercase, without changing: --- f1: >this is that< --- System.out.println ( f1.toUpperCase() ); : >THIS IS THAT< --- f1: >this is that< --- ie: f1 remains unchanged.

String arrays

Initially declare them (no alloction yet), viz: final int inDataLine_max = 1025; public String [] inDataLine = new String [inDataLine_max]; public String inDataLine_file_name; public int inDataLine_n; // how many are loaded Later, to add one in, inDataLine [ n ] = new String (value); In general, you will use code like this: try //----------------------------------- try { System.out.println ("--- File to load :--->" + inFileName + "<---"); inDataLine_file_name = new String (inFileName); inFile = new BufferedReader (new FileReader (inFileName)); nextLine = inFile.readLine(); // read a line from the file inDataLine_n++; inDataLine [ inDataLine_n ] = new String(nextLine); while ((nextLine != null) && (inDataLine_n < inDataLine_max)) { nextLine = inFile.readLine(); inDataLine_n++; inDataLine [inDataLine_n] = nextLine; } System.out.println ("--- file processed: " + inFileName); inFile.close(); } // end try catch (Exception e) { System.out.println ("*** [Line-577] writeHtmlPage: " + e.getMessage() ); } // end try/catch ---------------------------- end try

A brief note on try {} and catch {}

Note: The try { } catch () { } form is a VERY standard way of detecting exceptions (ie, run-time errors). Think of it like this: if (will work) // the try {} block else (e is set) // the catch (e) { } block if the try works ok, then the catch is NOT executed.l {
Back to the TOP of this page}

TextArea

(as a popup window) http://www.roseindia.net/java/java-tips/io/10file/readIntoTextarea.shtml


// FileIoDemo/FileInputGUI.java -- Demonstrate File Input
// Fred Swartz - December 2004

package FileIoDemo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

class FileInputGUI extends JFrame {
    JTextArea    m_fileContents = new JTextArea(20, 80);
    JFileChooser m_fc = new JFileChooser(".");
    
    //======================================================= constructor
    FileInputGUI() {
        //... Create a button to open a file.
        JButton openFileBtn = new JButton("Open");
        openFileBtn.addActionListener(new OpenAction());
        
        //... Create a "controls" panel with button on it.
        JPanel controls = new JPanel();
        controls.setLayout(new FlowLayout());
        controls.add(openFileBtn);
        
        //... Create the content pane with controls and a textarea.
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(controls, BorderLayout.NORTH);
        content.add(new JScrollPane(m_fileContents), BorderLayout.CENTER);
        
        this.setContentPane(content);
        this.pack();
        this.setTitle("File Input");
    }
    
    ////////////////////////////////////// inner listener class OpenAction
    class OpenAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int retval = m_fc.showOpenDialog(FileInputGUI.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File inFile = m_fc.getSelectedFile();
                try {
                    FileReader fr = new FileReader(inFile);
                    BufferedReader bufRdr = new BufferedReader(fr);
                    
                    String line = null;
                    while ((line = bufRdr.readLine()) != null){
                        m_fileContents.append(line);
                        m_fileContents.append("\n");
                    }
                    bufRdr.close();
                    
                } catch (IOException ioex) {
                    System.err.println(ioex);
                    System.exit(1);
                }
            }
        }//end actionPerformed
    }//end inner listener class OpenAction
}



{

Bargain Basement

Common errors and what they mean

package XYZ does not exist most commonly, you used a method from package "xyz" (eg, "System", "JFrame", "IOException", etc. ) but mis-spelled the package. eg, Systum.output.println - ditzy spelling system.output.println - ERK: lower case "s" setJmenuBar (menuBar) - ERK: setJMenuBar (again CAP's) ^ (clearly MY fav misteak ;) OR: You included the wrong package for that method. Usually something like forgetting to IMPORT javax.swing.JPanel (sheesh, i had import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JCheckBox; import javax.swing.JComboBox; !! razzah frazzing J thing!!!!