Large repositories of recorded measurement data are available on the World Wide
ID: 3675483 • Letter: L
Question
Large repositories of recorded measurement data are available on the World Wide Web from a wide spectrum of applications such as stock market data, weather/climate data, etc.
Use your Web browser to view the following URL:
http://earthquake.usgs.gov/earthquakes/map/
This site is maintained by the US Geological Service and reports recent earthquake activity around the world. If you examine the upper left corner of this web page, you will see a link marked 'download'. Select this link and then choose 'CSV' as the file format. Now save the downloaded file in your home directory and then use a text editor to examine it.
The file contains a large quantity of information that is detailed in the first line of the file. We will only be interested in the magnitude of the earthquake and the place (the string indicating where the event occurred, not the latitude/longitude). As you examine the file, note that the place is actually two comma separated strings. The first begins with a double-quote and the second ends with another double quote. The "general" location we are interested in is the second of the two strings that make up the place description.
First, write a program that will read the file and output the categories from the first line, along with their relative location in the line:
Example:
0 time
1 latitude
2 longitude
3 depth
4 mag
5 mag Type
6 nst
7 gap
8 dmin
9 rms
10 net
11 id
12 updated
13 place
14 type
You should use the getline() function to read the first line of the file and the split function provided on the website to create the value array
Explanation / Answer
java code:
package readdata;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class ReadData {
String fileName="data.txt";
String line=null;
public void read() throws FileNotFoundException, IOException{
try{
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
line = bufferedReader.readLine();
StringTokenizer st=new StringTokenizer(line,",");
int i=0;
while(st.hasMoreTokens())
{
System.out.println(i+" "+st.nextToken());
i++;
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
ReadData rd=new ReadData();
rd.read();
}
}
C++ code
#include <iostream>
#include<fstream>
#include<sstream>
using namespace std;
std::string line;
int main()
{
//string value;
ifstream myfile("data1.txt");
int i=0;
int num;
while(getline(myfile, line))
{
std::stringstream linestream(line);//to get the entire line
std::string value;
while(getline(linestream,value,','))//to split line where ',' is find
{
std::cout << i <<" "<< value<<" " ; //print each token
i++;//token number
}
//std::cout << "Line Finished" << std::endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.