comp 110 - Joshua Stough

Download Report

Transcript comp 110 - Joshua Stough

COMP 110
Introduction to Programming
Mr. Joshua Stough
September 12, 2007
Announcements
• HW and Prog 2
Review
Assignment Operators
count
count
count
count
+=
-=
*=
/=
5;
5;
5;
5;
//
//
//
//
count
count
count
count
=
=
=
=
count
count
count
count
+
*
/
5;
5;
5;
5;
Review
• Primitive Variables
– primitive data types (int, double, ...)
– stores the data in the memory location
• Reference Variables
– stores an address in the memory location
– "points to" another memory location
Today in COMP 110
• Input/Output using GUI
• String Tokenization
• Format Floating-Point Output
• Read from and Write to Files
Using Dialog Boxes for I/O
• Use a graphical user interface (GUI)
• class JOptionPane
– Contained in package javax.swing
– showInputDialog
• allows user to input a string from the keyboard
– showMessageDialog
• allows the programmer to display results
• Program must end with System.exit(0);
JOptionPane Methods
• showInputDialog
str = JOptionPane.showInputDialog(strExpression);
– stores what the user enters into the String str
• showMessageDialog
JOptionPane.showMessageDialog(parentComponent,
strExpression,
boxTitleString,
messageType);
showMessageDialog
• parentComponent
– parent of the dialog box
– we'll use null
• StrExpression
– what you want displayed in the box
• boxTitleString
– title of the dialog box
• messageType
– what icon will be displayed
messageType
• JOptionPane.ERROR_MESSAGE
– error icon
• JOptionPane.INFORMATION_MESSAGE
– information icon
• JOptionPane.PLAIN_MESSAGE
– no icon
• JOptionPane.QUESTION_MESSAGE
– question mark icon
• JOptionPane.WARNING_MESSAGE
– exclamation point icon
JOptionPane Example
JOptionPane.showMessageDialog (null, "Hello World!",
"Greetings",
JOptionPane.INFORMATION_MESSAGE);
UsingGUI.java example
User Input
• BufferedReader
– reads everything as a string
• Integer.parseInt
– only handles one integer in the string
• How to handle?
Enter 3 numbers: 34 15 75
The StringTokenizer Class
• tokens
– elements that comprise a string
• tokenizing
– process of extracting these elements
• delimiters
– characters that separate one token from another
• StringTokenizer class
– defined in the java.util package
– used to separate a string into tokens
The StringTokenizer
Class
Tokens and Delimiters
"Four score and seven years ago"
delimiter: ' '
tokens: "Four" "score" "and" "seven" "years" "ago"
"Bart:Lisa:Homer:Marge"
delimiter: ':'
tokens: "Bart" "Lisa" "Homer" "Marge"
The StringTokenizer Class
• Default delimiters:
– space, tab, carriage return, new line
• Methods
– StringTokenizer (String str)
– StringTokenizer (String str, String
delimits)
– String nextToken()
– boolean hasMoreTokens()
– int countTokens()
Tokenize.java example
• separated by spaces
• separated by commas
– gotcha
Formatting the Output of
Decimal Numbers
• float: defaults to 6 decimal places
• double: defaults to 15 decimal places
class DecimalFormat
• Import package java.text
• Create DecimalFormat object and initialize
DecimalFormat fmt = new DecimalFormat (formatString);
• FormatString
– "0.00" - limit to 2 decimal places, use 0 if there's no
item in that position
– "0.##" - limit to 2 decimal places, no trailing 0
• Use method format
– rounds the number instead of truncating
• Result of using DecimalFormat is a String
Examples
DecimalFormat twoDecimal =
new DecimalFormat("0.00");
DecimalFormat fmt =
new DecimalFormat("0.##");
System.out.println (twoDecimal.format(56.379)); 56.38
System.out.println (fmt.format(56.379)); 56.38
System.out.println (twoDecimal.format(.3451));
System.out.println (fmt.format(.3451)); 0.35
0.35
System.out.println (twoDecimal.format(.3)); 0.30
System.out.println (fmt.format(.3));
0.3
Reading From Text Files
• Similar to reading from the keyboard
• Create a BufferedReader object, but use a
FileReader object instead of
InputStreamReader
• Create BufferedReader object inside the
main method instead of outside
• Substitute the name of the file for System.in
• When finished reading from the file, we need
to close the file:
– BufferedReader close() method
Exceptions
• FileNotFoundException
– if the file specified to open was not found
• IOException
– some other I/O exception
public static void main (String[] args) throws
FileNotFoundException,
IOException
Reading From Text Files
String file = "data.dat";
BufferedReader inFile = new
BufferedReader (new FileReader (file));
String line = inFile.readLine();
inFile.close();
Writing To Text Files
• Similar to reading from text files
• Use FileWriter and PrintWriter instead of
FileReader and BufferedReader
• PrintWriter
– methods include print() and println(), which we use
just like those in System.out
• Like reading, we need to close the file when we're
done
– PrintWriter close() method
Writing To Text Files
String file = "outfile.dat";
PrintWriter outFile = new PrintWriter
(new FileWriter (file));
outFile.print ("Hi");
outFile.println(" There!");
outFile.close();
FileGUI.java Example
Summary
• JOptionPane
– showInputDialog
– showMessageDialog
• StringTokenizer
– tokens are separated by a delimiter
• DecimalFormat
– pattern tells how many digits after the decimal
point
– 0 - fill in with trailing 0s
– # - don't fill in trailing 0s
Summary
• Reading Data from File
– BufferedReader
– FileReader
• readLine()
• Writing Data to a File
– FileWriter
– PrintWriter
• print(StrExpression)
• println(StrExpression)
• Close files after using them
Next Time in COMP 110
Reading Assignment
Chapter 4 (pgs. 147-164)
• Relational and Logical Operators and
Expressions