



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: ALGORITHMIC DESIGN I; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Unknown 1989;
Typology: Study notes
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























public class ReadCountyData{
public static void main(String[ ] args){ Scanner in = new Scanner(
location of input file
while (
the file has another record
read the recordprocess the information in the record } in.close(); } }
import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner; /** Read census data, printing all those lines containing the string "AK" (Alaska).* * @author Byron Weber Becker */ public class ReadCountyData{
public static void main(String[ ] args){^
// Open the file. Scanner in = null;try{ File file = new File(
"2000US_County_data.txt"
in = new Scanner(file); } catch (FileNotFoundException ex){ System.out.println(ex.getMessage());
System.out.println(
"in "
"user.dir"
System.exit(1); }
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;import java.util.Scanner; /** Read census data, writing all those lines containing the string "AK" (Alaska) to a file.* * @author Byron Weber Becker */ public class WriteMatchingLines{
public static void main(String[ ] args){^
// Open the files. Scanner in = null;PrintWriter out = null;try{ in = new Scanner(new File(
"2000US_County_data.txt"
out = new PrintWriter(
"Alaska.txt"
} catch (FileNotFoundException ex){ System.out.println(ex.getMessage());
System.out.println(
"in "
"user.dir"
System.exit(1); }
// Read and process each record. while (in.hasNextLine()){ String record = in.nextLine();
if (record.indexOf(
"AK"
{ out.println(record);} } // Close the files. in.close();out.close(); } }
Anchorage
while (in.hasNextLine()){ String record = in.nextLine();
String stAb = in.next();
// state abbreviation
String cNm = in.next();
// county name
int pop = in.nextInt();
// population
int hh = in.nextInt();
// number of households
int inc = in.nextInt();
// median income
double age = in.nextDouble();
// median age
in.nextLine();if (stAb.equals(
"AK"
{ System.out.println(stAb +
" "
" "
" "
" "
" "
Anchorage
while (in.hasNextLine())
Anchorage
String stAb = in.next();
Anchorage
String cNm = in.next();
Anchorage
Anchorage
Anchorage
int inc = in.nextInt();
Anchorage
Anchorage
double age = in.nextDouble();
Anchorage
Anchorage
in.nextLine();
Anchorage
Anchorage
Bethel
while (in.hasNextLine())
Anchorage
Bethel
String stAb = in.next();
Anchorage
// Read and process each record.^ in.nextLine();
// skip header record
while (in.hasNextLine()){ String stAb = in.next();
// state abbreviation
String cNm = in.next();
// County name – might consist of several tokens
while (!in.hasNextInt()){ cNm +=
" "
} int pop = in.nextInt();
// population
int hh = in.nextInt();
// number of households
int inc = in.nextInt();
// median income
double age = -1;
// median age – might be NA
if (in.hasNextDouble()){ age = in.nextDouble();} else{ in.next();} in.nextLine(); }
+void main(String[ ] args)
-String stateAbbrev-String countyName-int population-int numHouseholds-int medianIncome-double medianAge+County(Scanner in)+boolean inState(String stAbbrev)+String toString( )
import java.util.Scanner;public class County extends Object{ private String stateAbbrev;
private String countyName;private int population;private int numHouseholds;private int medianIncome;private double medianAge; /** Read one record from a file to initialize a new instance of County.* @param in The open input file with the file cursor positioned just before the record to read. */ public County(Scanner in){ super();
this.stateAbbrev = in.next();
// state abbreviation
this.countyName = in.next();while (!in.hasNextInt()){ this.countyName +=
" "
// county name
} this.population = in.nextInt();
// population
this.numHouseholds = in.nextInt();
// number of households
this.medianIncome = in.nextInt();
// median income
this.medianAge = in.nextDouble();
// median age
in.nextLine(); }
/** Is this county in the specified state? / public boolean inState(String stAbbrev){ return this.stateAbbrev.equals(stAbbrev);} /* Represent this county as a string. */ public String toString(){ return this.stateAbbrev +
" "
" "
" "
" "
" "
D:
Co mm...
cs
cs
A
A
o ld _data
Main.javaServerRecord .javaRepo rt.java
2004_counties.txt2005_counties.txt2006_counties.txt
Main.javaE xplorer.java
counties.txt
exp lo rer.ini