SlideServe
Menu
Préférences
© SlideServe · v2026

Java Text File Input/Output - Group ID Rule and Scanner Class

helmut avatar
8 diapositives · 183 Views · Sep 20, 2014

Learn Java text file input/output, the Group ID rule, and Scanner class. Submit homework by emailing solutions. Modify Census household problem for homework #2. Use Scanner and Printf for reading and writing files.

Java Text File Input/Output - Group ID Rule and Scanner Class - slide 1
01 / 8Java Text File Input/Output - Group ID Rule and Scanner Class
1 / 17

Télécharger la présentation

Java Text File Input/Output - Group ID Rule and Scanner Class
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. 20 Oct - Overview • Homework #1 • Group-Id rule • Notes on Java text file input/output • Scanner class • Printf (like C)

  2. Homework #1 http://pmik.petrsu.ru/pub/ourusoff/Intro to JSE/Home Work/ due noon, Friday, 21 Oct email solution as (zipped) attachment to: nourusoff@psu.karelia.ru Please send me: • 4 .jsp files that show the design of the original output table and each of the 3 modifications • Extra credit: 3 .jsp files that show the modification of the bad multiplication table to produce the 3 modifications

  3. Group-ID Rule We frequently wish to process groups in a file sorted by group. Problem: Census file is sorted on a household number. Data for each household is to be accumulated. Detailed problem statement • System diagram • Data structures for: • input - censusFile.jsp • output - censusReport.jsp • Composite data model showing correspondences • Basic Program Structure - censusBasicStr.jsp • List and allocate operations • Produce elaborated program structure - censusElabStr.jsp • Generate structure text or code • Structure text • Java code

  4. Group ID Rule • Group-id Rule: “If a structure contains a component that is a group of records all having the same value of a (usually sorted) identifier, then there must be an operation that stores the value of the identifier. This operation should be allocated once per group, at the beginning of the group.” • Design focuses on: • a static not a dynamic view of the problem; • data structures (objects), not on dynamic flow of control

  5. Homework #2 (due 28 October) The Census household problem is modified as follows: (The course page has a sample file, censusFile.txt in the HW#2 folder. Test your program with this file.) • The census text file has a city code in positions 8-10. Accumulate the total number of males and females as well as the average household size in each city and produce the summary as before. • Put sensible column headings in the report: (Household Id, # Males, # Females, etc.) • Use Scanner and printf for reading and writing.

  6. Using the Scanner utility class • Scanner is in the util package, not the lang package so you need to include the line  import.util.Scanner; at the top of the class. • To create a Scanner object and connect it to a file you must create a File object based on the name of that file. This can be done with one line; Scanner s = new Scanner( new File(“census.txt"));Remember all programs are judged assuming the file is in the same directory as the compiled class file so do not put any path information on the file, just the file name. To use the file class you must include the line import.io.File; at the top of your program.

  7. Using the Scanner utility class (cont.) • Unfortunately instantiating a File object can result in in an IOException and this is a checked exception so you either have to use a try-catch block or declare that the method that contains the code that generates the checked exception throws an IOException. • In actual programming practice it would be much better to include a try-catch block, but for educational purposes and programming contest purposes it is much easier to simply declare that main throws an IOException. • Thus the declaration of the main method looks like this:public static void main(String[] args) throws IOException • IOException must also be imported. This can either be done as a separate import statement:import java.io.IOException;or since the program now needs multiple classes from the io package we can use the wildcard import. Again, not the best programming practice, but for Olympiads, not a bad idea.import java.io.*;

  8. Java I/O with Scanner and Formatter Java documentation Tutorial on printf • Printf.demo.java Tutorial on Using Scanner for Input from Console • ScanConsoleApp.java