Question: Why doesn't my program turn CSV file into KML? Attached below is my java program along with my CSV file
JAVA PROGRAM:
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
public class Library {
private static Scanner s;
private static PrintWriter outFile;
public static void printHeader() {
outFile.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
outFile.println("<Document>");
} // endmethod printHeader...
public static void printData(String secterType,String name, String lat,String lon) {
outFile.println("<Placemark>");
outFile.println(" <Style>");
outFile.println(" <LabelStyle>");
outFile.println(" <scale>0</scale>");
outFile.println(" </LabelStyle>");
outFile.println(" <IconStyle>");
outFile.println(" <Icon>");
if (secterType.equalsIgnoreCase("public")) {
outFile.println(" <href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>");
} else {
outFile.println(" <href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>");
} // endif...
outFile.println(" </Icon>");
outFile.println(" </IconStyle>");
outFile.println(" </Style>");
outFile.println(" <name>"+name+"</name>");
outFile.println(" <description>");
outFile.println(" <![CDATA[");
outFile.println(" <h1>"+name+"</h1>");
outFile.println(" <ul>");
outFile.println(" <li>Type ="+secterType);
outFile.println(" </ul>");
outFile.println(" ]]>");
outFile.println(" </description>");
outFile.println(" <Point>");
outFile.println(" <coordinates>"+lat+","+lon+"</coordinates>");
outFile.println(" </Point>");
outFile.println("</Placemark>");
} // endmenthod printData...
public static void printTail() {
outFile.println("</Document>");
outFile.println("</kml>");
}
public static void parseData(String inString) {
// input line: name,coordinates
String[] tokens = inString.split(",");
String secterType = tokens[0];
String name = tokens[1];
String lat = tokens[2].replace("\"","");
String lon = tokens[3].replace("\"","");
printData(secterType,name,lat,lon);
} // endmethod parseData...
public static void readData(int maxLine) {
String inString = "";
int i=0;
inString = s.nextLine(); // first 2 lines of CSV file are comments...
inString = s.nextLine(); // first 2 lines of CSV file are comments...
while(i < maxLine) {
inString = s.nextLine();
//outFile.println("line "+i+" = "+inString);
parseData(inString);
System.out.println("line "+i+" = "+inString);
i++;
} // endwhile...
} // endmethod readData...
public static void main(String[] args) throws FileNotFoundException {
s = new Scanner(new File("librarylist.csv"));
outFile = new PrintWriter("libraryMap.kml");
printHeader();
readData(1000);
printTail();
s.close();
outFile.close();
} // endmethod main...
}
CSV FILE:
| #type | name | lat | lon |
| public | Martin Luther King Jr. Memorial Library | 38.89869166 | -77.02476625 |
| public | Capitol View Library | 38.88895483 | -76.92957721 |
| public | Georgetown Library | 38.91343456 | -77.06600731 |
| public | Petworth Library | 38.94220022 | -77.02613743 |
| public | Chevy Chase Library | 38.96556949 | -77.07545672 |
| public | Northeast Library | 38.89440688 | -76.99635899 |
| public | Tenley-Friendship Library | 38.94760839 | -77.07988675 |
| public | Palisades Library | 38.9183818 | -77.09717464 |
| public | Southeast Library | 38.88408446 | -76.99651546 |
| public | Library Express | 38.9021849 | -77.044548 |
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
class Library {
private static Scanner s;
private static PrintWriter outFile;
public static void printHeader() {
outFile.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
outFile.println("<Document>");
} // endmethod printHeader...
public static void printData(String secterType,String name, String lat,String lon) {
outFile.println("<Placemark>");
outFile.println(" <Style>");
outFile.println(" <LabelStyle>");
outFile.println(" <scale>0</scale>");
outFile.println(" </LabelStyle>");
outFile.println(" <IconStyle>");
outFile.println(" <Icon>");
if (secterType.equalsIgnoreCase("public")) {
outFile.println(" <href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>");
} else {
outFile.println(" <href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>");
} // endif...
outFile.println(" </Icon>");
outFile.println(" </IconStyle>");
outFile.println(" </Style>");
outFile.println(" <name>"+name+"</name>");
outFile.println(" <description>");
outFile.println(" <![CDATA[");
outFile.println(" <h1>"+name+"</h1>");
outFile.println(" <ul>");
outFile.println(" <li>Type ="+secterType);
outFile.println(" </ul>");
outFile.println(" ]]>");
outFile.println(" </description>");
outFile.println(" <Point>");
outFile.println(" <coordinates>"+lat+","+lon+"</coordinates>");
outFile.println(" </Point>");
outFile.println("</Placemark>");
} // endmenthod printData...
public static void printTail() {
outFile.println("</Document>");
outFile.println("</kml>");
}
public static void parseData(String inString) {
// input line: name,coordinates
String[] tokens = inString.split(" ");// correction 1: here i made
a correction, instead of ',' use space (' ') as a delimiter
String secterType = tokens[0];
String name = tokens[1];
String lat = tokens[2].replace("\"","");
String lon = tokens[3].replace("\"","");
printData(secterType,name,lat,lon);
} // endmethod parseData...
public static void readData(int maxLine) {
String inString = "";
int i=0;
inString = s.nextLine(); // first 2 lines of CSV file are comments...
inString = s.nextLine(); // first 2 lines of CSV file are comments...
while(i < maxLine && s.hasNextLine())// correction 2: we should use s.hasNextLine() to read the file till data exists.
{
inString = s.nextLine();
parseData(inString);
System.out.println("line "+i+" = "+inString);
i++;
} // endwhile...
} // endmethod readData...
public static void main(String[] args) throws FileNotFoundException {
s = new Scanner(new File("librarylist.csv"));
outFile = new PrintWriter("libraryMap.kml");
printHeader();
readData(1000);
printTail();
s.close();
outFile.close();
} // endmethod main...
}
output kml file:-
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Capitol</name>
<description>
<![CDATA[
<h1> Capitol</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>View,Library</coordinates>
</Point>
</Placemark>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Georgetown</name>
<description>
<![CDATA[
<h1> Georgetown</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>Library,
38.91343456</coordinates>
</Point>
</Placemark>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Petworth</name>
<description>
<![CDATA[
<h1> Petworth</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>Library,
38.94220022</coordinates>
</Point>
</Placemark>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Chevy</name>
<description>
<![CDATA[
<h1> Chevy</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>Chase,Library</coordinates>
</Point>
</Placemark>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Northeast</name>
<description>
<![CDATA[
<h1> Northeast</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>Library,
38.89440688</coordinates>
</Point>
</Placemark>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Tenley-Friendship</name>
<description>
<![CDATA[
<h1> Tenley-Friendship</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>Library,
38.94760839</coordinates>
</Point>
</Placemark>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Palisades</name>
<description>
<![CDATA[
<h1> Palisades</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>Library,
38.9183818</coordinates>
</Point>
</Placemark>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Southeast</name>
<description>
<![CDATA[
<h1> Southeast</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>Library,
38.88408446</coordinates>
</Point>
</Placemark>
<Placemark>
<Style>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/library.png</href>
</Icon>
</IconStyle>
</Style>
<name> Library</name>
<description>
<![CDATA[
<h1> Library</h1>
<ul>
<li>Type =public
</ul>
]]>
</description>
<Point>
<coordinates>Express,
38.9021849</coordinates>
</Point>
</Placemark>
</Document>
</kml>
hope you like my answer, thank you
Question: Why doesn't my program turn CSV file into KML? Attached below is my java program...
import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private static final String OUTPUT_FILE = "crimes.txt"; public static void main(String[] args) { int totalCrimes = 0; // read all the rows from the csv file and add the total count in the totalCrimes variable try { Scanner fileScanner = new Scanner(new File(INPUT_FILE)); String line = fileScanner.nextLine(); // skip first line while (fileScanner.hasNext()) { String[] tokens = fileScanner.nextLine().split(","); if (tokens.length == 4) { totalCrimes +=...
How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner { public static void main(String[] args) { /* For Homework! Tokens*/ Scanner file = null; PrintWriter fout= null; ...
Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order This is what I have so far: import java.io.File; import java.util.Scanner; import java.io.PrintWriter; public class integerFile {...
Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...
//Graph Class:
import java.util.ArrayList;
//Graph is a class whose objects represent graphs.
public class Graph {
ArrayList<Node> nodeList;
ArrayList<Edge> edgeList;
public Graph() {
nodeList = new ArrayList<Node>();
edgeList = new ArrayList<Edge>();
}
public ArrayList<Node> getNodeList() {
return nodeList;
}
public ArrayList<Edge> getEdgeList() {
return edgeList;
}
public void addNode(Node n) {
nodeList.add(n);
}
public void addEdge(Edge e) {
edgeList.add(e);
}
public String toString() {
String s = "Graph g.\n";
if (nodeList.size() > 0) {
for (Node n : nodeList) {
// Print node info
String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n";
s = s.concat(t);
}
s = s.concat("\n");
}
return s;
}
}
// Node Class:
import java.util.ArrayList;
// Node is a class whose objects represent nodes (a.k.a., vertices) in the graph.
public class Node {
String name;
String val; // The value of the Node
String abbrev; // The abbreviation for the Node
ArrayList<Edge> outgoingEdges;
ArrayList<Edge> incomingEdges;
String color; //Create the color of the TYPE Node List
int start; //Create the Starting Time
int end; //Create the Ending Time
public Node( String theAbbrev ) {
setAbbrev( theAbbrev );
val = null;
name = null;
outgoingEdges = new ArrayList<Edge>();
incomingEdges = new ArrayList<Edge>();
}
public String getAbbrev() {
return abbrev;
}
public String getName() {
return name;
}
public String getVal() {
return val;
}
public ArrayList<Edge> getOutgoingEdges() {
return outgoingEdges;
}
public ArrayList<Edge> getIncomingEdges() {
return incomingEdges;...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car { private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
This is my current output for my program.
I am trying to get the output to look like
This is my program
Student.java
import java.awt.GridLayout;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
public class Student extends javax.swing.JFrame {
BufferedWriter outWriter;
StudentA s[];
public Student() {
StudentGUI();
}
private void StudentGUI() {
jScrollPane3 = new
javax.swing.JScrollPane();
inputFileChooser = new
javax.swing.JButton();
outputFileChooser = new
javax.swing.JButton();
sortFirtsName = new...
Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int row) String field(int row, int column) Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file. CSVReader.java import java.util.ArrayList; import java.util.Scanner; import java.io.*; /** Class to read and process the contents of a standard CSV file */ public class CSVReader {...