Download presentation
Presentation is loading. Please wait.
1
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218
2
Announcements Assignment simpler than I thought (darn). Office hours (I’m lonely). Precedence of % operator. Why reference variables?
3
Review Assignment Operators count += 5;// count = count + 5; count -= 5; // count = count - 5; count *= 5; // count = count * 5; count /= 5; // count = count / 5;
4
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
5
Today in COMP 14 Input/Output using GUI String Tokenization Format Floating-Point Output Read from and Write to Files
6
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);
7
JOptionPane Methods showInputDialog str = JOptionPane.showInputDialog(strExpression); –stores what the user enters into the String str showMessageDialog JOptionPane.showMessageDialog(parentComponent, strExpression, boxTitleString, messageType);
8
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
9
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
10
JOptionPane Example JOptionPane.showMessageDialog (null, "Hello World!", "Greetings", JOptionPane.INFORMATION_MESSAGE);
11
UsingGUI.java example
12
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
13
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
14
The StringTokenizer Class Tokens and Delimiters "Four score and seven years ago" "Bart:Lisa:Homer:Marge" delimiter: ' ' tokens: "Four" "score" "and" "seven" "years" "ago" delimiter: ':' tokens: "Bart" "Lisa" "Homer" "Marge"
15
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()
16
Tokenize.java example separated by spaces separated by commas –gotcha
17
Formatting the Output of Decimal Numbers float: defaults to 6 decimal places double: defaults to 15 decimal places
18
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
19
Examples DecimalFormat twoDecimal = new DecimalFormat("0.00"); DecimalFormat fmt = new DecimalFormat("0.##"); System.out.println (twoDecimal.format(56.379)); System.out.println (fmt.format(56.379)); System.out.println (twoDecimal.format(.3451)); System.out.println (fmt.format(.3451)); System.out.println (twoDecimal.format(.3)); System.out.println (fmt.format(.3)); 56.38 0.35 0.30 56.38 0.35 0.3
20
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
21
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
22
Reading From Text Files String file = "data.dat"; BufferedReader inFile = new BufferedReader (new FileReader (file)); String line = inFile.readLine(); inFile.close();
23
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
24
Writing To Text Files String file = "outfile.dat"; PrintWriter outFile = new PrintWriter (new FileWriter (file)); outFile.print ("Hi"); outFile.println(" There!"); outFile.close();
25
FileGUI.java Example
26
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
27
Summary Reading Data from File –BufferedReader –FileReader readLine() Writing Data to a File –FileWriter –PrintWriter print(StrExpression) println(StrExpression) Close files after using them
28
Next Time in COMP 14 Reading Assignment Chapter 4 (pgs. 147-164) Relational and Logical Operators and Expressions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.