Skip to content

Instantly share code, notes, and snippets.

@bytecodeman bytecodeman/Library.java Secret
Created Dec 11, 2018

Embed
What would you like to do?
CSC-111 State Data Retrieval Systems
package statedata;
public class Library {
public static int search(String[] array, String key) {
for (int i = 0; i < array.length; i++)
if (array[i].equalsIgnoreCase(key))
return i;
return -1;
}
public static int search(int[] array, int key) {
for (int i = 0; i < array.length; i++)
if (array[i] == key)
return i;
return -1;
}
}
package statedata;
public class StateData {
public String name;
public String capital;
public double area;
public int year;
public int order;
}
New Hampshire
Concord
9304 1788 9
Massachusetts
Boston
8257 1788 6
Vermont
Montpelier
9609 1791 14
Rhode Island
Providence
1214 1790 13
Connecticut
Hartford
5009 1788 5
Maine
Augusta
33215 1820 23
New York
Albany
49576 1788 11
Pennsylvania
Harrisburg
45333 1787 2
Delaware
Dover
2057 1787 1
Maryland
Annapolis
10577 1788 7
Washington
Olympia
68192 1889 42
Oregon
Salem
96981 1859 33
Virginia
Richmond
40815 1788 10
North Carolina
Raleigh
52586 1789 12
South Carolina
Columbia
31055 1788 8
Georgia
Atlanta
58876 1788 4
New Jersey
Trenton
7836 1787 3
Hawaii
Honolulu
6450 1959 50
Iowa
Des Moines
56290 1846 29
Florida
Tallahassee
58560 1845 27
Tennessee
Nashville
42244 1796 16
Michigan
Lansing
58216 1837 26
Ohio
Columbus
41222 1803 17
Wyoming
Cheyenne
97914 1890 44
Colorado
Denver
104247 1876 38
Nebraska
Lincoln
77227 1867 37
Wisconsin
Madison
56154 1848 30
South Dakota
Pierre
77047 1889 39
Alabama
Montgomery
51609 1819 22
Mississippi
Jackson
47716 1817 20
Louisiana
Baton Rouge
48523 1812 18
Arkansas
Little Rock
53104 1836 25
Missouri
Jefferson City
69686 1821 24
Kentuckey
Frankfort
40395 1792 15
Minnesota
Saint Paul
84068 1858 32
North Dakota
Bismark
70665 1889 41
Kansas
Topeka
82264 1861 34
Oklahoma
Oklahoma City
69919 1907 46
Texas
Austin
267338 1845 28
New Mexico
Santa Fe
121666 1912 47
Arizona
Phoenix
113909 1912 48
Utah
Salt Lake City
84916 1896 45
Nevada
Carson City
110540 1864 36
Idaho
Boise
83557 1890 43
West Virginia
Charleston
24181 1863 35
California
Sacramento
158706 1850 31
Alaska
Juneau
586412 1959 49
Indiana
Indianapolis
36291 1816 19
Illinois
Springfield
56400 1818 21
Montana
Helena
147138 1889 41
package statedata;
public class StateData2 {
public String name;
public String capital;
public double area;
public int year;
public int order;
public StateData2(String sn, String sc, double sa, int sy, int so) {
this.name = sn;
this.capital = sc;
this.area = sa;
this.year = sy;
this.order = so;
}
public String toString() {
return String.format("%20s%20s", this.capital, this.name);
}
}
/*
* State Data Information Retrieval System V1.0
* A.C. Silvestri
* 12/11/2018
* CSC-111 Intro to Java Programming
*/
package statedata;
import java.io.*;
import java.util.*;
public class StateDataSearch1 {
private static final int STATECOUNT = 50;
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
String stateNames[] = new String[STATECOUNT];
String stateCapitals[] = new String[STATECOUNT];
double stateArea[] = new double[STATECOUNT];
int stateYear[] = new int[STATECOUNT];
int stateOrder[] = new int[STATECOUNT];
System.out.print("Enter Datafile name: ");
String filename = sc.nextLine();
grabDataFromFile(filename, stateNames, stateCapitals, stateArea, stateYear, stateOrder);
outputData(stateNames, stateCapitals);
searchByStateOrder(sc, stateNames, stateOrder);
searchByStateName(sc, stateNames, stateCapitals);
}
private static void grabDataFromFile(String filename, String[] stateNames, String[] stateCapitals,
double[] stateArea, int[] stateYear, int[] stateOrder)
throws FileNotFoundException {
System.out.println("Processing input datafile: " + filename);
Scanner in = new Scanner(new File(filename));
for (int i = 0; i < STATECOUNT; i++) {
String sn = in.nextLine();
String sc = in.nextLine();
double sa = in.nextDouble();
int sy = in.nextInt();
int so = in.nextInt();
in.nextLine();
stateNames[i] = sn;
stateCapitals[i] = sc;
stateArea[i] = sa;
stateYear[i] = sy;
stateOrder[i] = so;
}
in.close();
System.out.println("Finished Processing input datafile: " + filename);
}
private static void outputData(String[] stateNames, String[] stateCapitals) {
for (int i = 0; i < stateNames.length; i++)
System.out.printf("%15s%15s\n", stateNames[i], stateCapitals[i]);
System.out.println();
}
private static void searchByStateOrder(Scanner sc, String[] stateNames, int[] stateOrder) {
System.out.println("Entering Searching By State Order Mode . . .");
do {
System.out.print("Enter State Order [1-50] (0 to Quit): ");
int order = sc.nextInt();
sc.nextLine(); // Consume Line
if (order == 0)
break;
int index = Library.search(stateOrder, order);
if (index != -1)
System.out.println(stateNames[index] + " is state no. " + order);
else
System.out.println("No state matches order no. = " + order);
} while (true);
}
private static void searchByStateName(Scanner sc, String[] stateNames, String[] stateCapitals) {
System.out.println("Enter a State Name to get its Capital . . .");
do {
System.out.print("Enter State Name (EXIT to Quit): ");
String name = sc.nextLine();
if (name.equals("EXIT"))
break;
int index = Library.search(stateNames, name);
if (index != -1)
System.out.println(stateCapitals[index] + " is the capital of " + name);
else
System.out.println("No such state " + name);
} while (true);
}
}
/*
* State Data Information Retrieval System V2
* A.C. Silvestri
* 12/11/2018
* CSC-111 Intro to Java Programming
*/
package statedata;
import java.io.*;
import java.net.URL;
import java.util.*;
public class StateDataSearch2 {
private static final int STATECOUNT = 50;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String stateNames[] = new String[STATECOUNT];
String stateCapitals[] = new String[STATECOUNT];
double stateArea[] = new double[STATECOUNT];
int stateYear[] = new int[STATECOUNT];
int stateOrder[] = new int[STATECOUNT];
System.out.print("Enter Datafile URL: ");
String filename = sc.nextLine();
grabDataFromFile(filename, stateNames, stateCapitals, stateArea, stateYear, stateOrder);
outputData(stateNames, stateCapitals);
searchByStateOrder(sc, stateNames, stateOrder);
searchByStateName(sc, stateNames, stateCapitals);
}
// https://cs.stcc.edu/~silvestri/csc111/statedata.txt
private static void grabDataFromFile(String filename, String[] stateNames, String[] stateCapitals,
double[] stateArea, int[] stateYear, int[] stateOrder)
throws IOException {
System.out.println("Processing input datafile: " + filename);
Scanner in = new Scanner(new URL(filename).openStream());
for (int i = 0; i < STATECOUNT; i++) {
String sn = in.nextLine();
String sc = in.nextLine();
double sa = in.nextDouble();
int sy = in.nextInt();
int so = in.nextInt();
in.nextLine();
stateNames[i] = sn;
stateCapitals[i] = sc;
stateArea[i] = sa;
stateYear[i] = sy;
stateOrder[i] = so;
}
in.close();
System.out.println("Finished Processing input datafile: " + filename);
}
private static void outputData(String[] stateNames, String[] stateCapitals) {
for (int i = 0; i < stateNames.length; i++)
System.out.printf("%15s%15s\n", stateNames[i], stateCapitals[i]);
System.out.println();
}
private static void searchByStateOrder(Scanner sc, String[] stateNames, int[] stateOrder) {
System.out.println("Entering Searching By State Order Mode . . .");
do {
System.out.print("Enter State Order [1-50] (0 to Quit): ");
int order = sc.nextInt();
sc.nextLine(); // Consume Line
if (order == 0)
break;
int index = Library.search(stateOrder, order);
if (index != -1)
System.out.println(stateNames[index] + " is state no. " + order);
else
System.out.println("No state matches order no. = " + order);
} while (true);
}
private static void searchByStateName(Scanner sc, String[] stateNames, String[] stateCapitals) {
System.out.println("Enter a State Name to get its Capital . . .");
do {
System.out.print("Enter State Name (EXIT to Quit): ");
String name = sc.nextLine();
if (name.equals("EXIT"))
break;
int index = Library.search(stateNames, name);
if (index != -1)
System.out.println(stateCapitals[index] + " is the capital of " + name);
else
System.out.println("No such state " + name);
} while (true);
}
}
/*
* State Data Information Retrieval System V3
* A.C. Silvestri
* 11/18/2017
* CSC-111 Intro to Java Programming
*/
package statedata;
import java.io.*;
import java.util.*;
public class StateDataSearch3 {
static final int STATECOUNT = 50;
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
StateData states[] = new StateData[STATECOUNT];
System.out.print("Enter Datafile name: ");
String filename = sc.nextLine();
grabDataFromFile(filename, states);
outputData(states);
searchByStateOrder(sc, states);
searchByStateName(sc, states);
}
private static void grabDataFromFile(String filename, StateData[] states) throws FileNotFoundException {
System.out.println("Processing input datafile: " + filename);
Scanner in = new Scanner(new File(filename));
for (int i = 0; i < STATECOUNT; i++) {
states[i] = new StateData();
states[i].name = in.nextLine();
states[i].capital = in.nextLine();
states[i].area = in.nextDouble();
states[i].year = in.nextInt();
states[i].order = in.nextInt();
in.nextLine(); // Consume Line
}
in.close();
System.out.println("Finished Processing input datafile: " + filename);
}
private static void outputData(StateData[] states) {
// TODO Auto-generated method stub
}
private static void searchByStateOrder(Scanner sc, StateData[] states) {
System.out.println("Entering Searching By State Order Mode . . .");
// Put state orders in separate array to use library method
int stateOrders[] = new int[states.length];
for (int i = 0; i < stateOrders.length; i++)
stateOrders[i] = states[i].order;
do {
System.out.print("Enter State Order [1-50] (0 to Quit): ");
int order = sc.nextInt();
sc.nextLine(); // Consume Line
if (order == 0)
break;
int index = Library.search(stateOrders, order);
if (index != -1)
System.out.println(states[index].name + " is state no. " + order);
else
System.out.println("No state matches order no. = " + order);
} while (true);
}
private static void searchByStateName(Scanner sc, StateData[] states) {
System.out.println("Entering Searching By State Name Mode . . .");
// Put state names in separate array to use library method
String stateNames[] = new String[states.length];
for (int i = 0; i < stateNames.length; i++)
stateNames[i] = states[i].name;
do {
System.out.print("Enter State Name (EXIT to Quit): ");
String name = sc.nextLine();
if (name.equals("EXIT"))
break;
int index = Library.search(stateNames, name);
if (index != -1)
System.out.println(states[index].capital + " is the capitol of " + name);
else
System.out.println("No such state " + name);
} while (true);
}
}
/*
* State Data Information Retrieval System V4
* A.C. Silvestri
* 11/18/2017
* CSC-111 Intro to Java Programming
*/
package statedata;
import java.io.*;
import java.util.*;
public class StateDataSearch4 {
static final int STATECOUNT = 50;
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
StateData2 states[] = new StateData2[STATECOUNT];
System.out.print("Enter Datafile name: ");
String filename = sc.nextLine();
grabDataFromFile(filename, states);
outputData(states);
searchByStateOrder(sc, states);
searchByStateName(sc, states);
}
private static void grabDataFromFile(String filename, StateData2[] states) throws FileNotFoundException {
System.out.println("Processing input datafile: " + filename);
Scanner in = new Scanner(new File(filename));
for (int i = 0; i < STATECOUNT; i++) {
String sn = in.nextLine();
String sc = in.nextLine();
double sa = in.nextDouble();
int sy = in.nextInt();
int so = in.nextInt();
in.nextLine();
states[i] = new StateData2(sn, sc, sa, sy, so);
}
in.close();
System.out.println("Finished Processing input datafile: " + filename);
}
private static void outputData(StateData2[] states) {
for (int i = 0; i < states.length; i++)
System.out.println(states[i]);
System.out.println();
}
private static void searchByStateOrder(Scanner sc, StateData2[] states) {
System.out.println("Entering Searching By State Order Mode . . .");
// Put state orders in separate array to use library method
int stateOrders[] = new int[states.length];
for (int i = 0; i < stateOrders.length; i++)
stateOrders[i] = states[i].order;
do {
System.out.print("Enter State Order [1-50] (0 to Quit): ");
int order = sc.nextInt();
sc.nextLine(); // Consume Line
if (order == 0)
break;
int index = Library.search(stateOrders, order);
if (index != -1)
System.out.println(states[index].name + " is state no. " + order);
else
System.out.println("No state matches order no. = " + order);
} while (true);
}
private static void searchByStateName(Scanner sc, StateData2[] states) {
System.out.println("Entering Searching By State Name Mode . . .");
// Put state names in separate array to use library method
String stateNames[] = new String[states.length];
for (int i = 0; i < stateNames.length; i++)
stateNames[i] = states[i].name;
do {
System.out.print("Enter State Name (EXIT to Quit): ");
String name = sc.nextLine();
if (name.equals("EXIT"))
break;
int index = Library.search(stateNames, name);
if (index != -1)
System.out.println(states[index].capital + " is the capitol of " + name);
else
System.out.println("No such state " + name);
} while (true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.