Lecture Codes
Lecture Codes
Lecture Codes
import java.io.IOException;
import java.nio.file.Files;
CustomerUtility.class.getResource(filename))
.substring(6)); // will return a substring of
the URI where it will delete 'http:/" which is generated automatically
return Files.readString(file);
} catch (IOException e) {
// something went wrong
return "Did you forget to create the file?\n" +
"Is the file in the right location?\n" +
e.toString();
}
}
}
// this will store a list of customer objects which maps to the key
customers in the json object
@JsonProperty("customers")
public List<Customer> customersList;
// Then set this variable to the table id and APPEND this table to the
div
let tableRef = document.createElement("table");
divRef.appendChild(tableRef);
// Creating a tbody variable and then appending the header to the table
let tableBodyRef = document.createElement("tbody");
tableRef.appendChild(tableBodyRef);
// creating a response
Response myResp = Response.status(200)
.header("Access-Control-Allow-Origin",
"http://localhost:8448") // this will allow the localhost 8448 to be
hosted
.header("Content-Type", "application/json") // the data
returning will be json format
.entity(val) // the data is customers
.build();
return myResp;
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
}
/**
* Function makes a HTTP request to an API
* **/
function requestData(apiCallURL){
// the fetch request is an asynchronous request to an API
fetch(apiCallURL, {
// defining request headers in the fetch request
method: 'GET',
headers: {
'Accept': 'application/json',
},
})
.then(response => response.json())
.then(response => loadDataAsTable("customers", response)); // this
will dynamically make the data inputted into the table in the html file
}
// finding the average for each column (e.g assignment1 grades, etc)
public String findColumnAVG(String col){
// File inFile = new File();
String columnName = col;
String msg = "Not found or error occurred";
try {
FileReader fileInput = new FileReader(directory);
BufferedReader input = new BufferedReader(fileInput);
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}
}
public class BookParser {
// iterate over each file in the dir and count their words
for (int i = 0; i<numFiles; i++){
Map<String, Integer> wordMap = countWordFile(filesInDir[i]);
// get a frequency map between the word and its frequency
if(frequencies.containsKey(word)){
// increment
int oldCount = frequencies.get(word);
frequencies.put(word, count + oldCount);
}
else{
frequencies.put(word, count);
}
}
return frequencies;
}
}
Function to count the words in the file
private Map<String, Integer> countWordFile(File file) throws
IOException {
Map<String, Integer> wordMap = new TreeMap<>();
if(file.exists()){
// load all the data and process it into words
Scanner scanner = new Scanner(file); // note: a scanner will
by default use a white space as a delimiter
while(scanner.hasNext()){
// ignore the casing for words
String word = (scanner.next()).toLowerCase(); // will
convert all uppercase letters to lowercase so the same word is not
duplicated
if (isWord(word)){ // only if the string is a word, then
add it in the map
// add the word if not exists yet
if(!wordMap.containsKey(word)){
wordMap.put(word, 1);
}
// increment the count if exists
else{
int oldCount = wordMap.get(word);
wordMap.put(word, oldCount+1);
}
}
}
}
return wordMap;
}
return false;
if(myFile!=null){
PrintWriter output = new PrintWriter(myFile);
output.print(content);
output.close();
}
}
}
// Will read the data in the specified url and find the column name
and then store it in the string
public String readData(@PathParam("col") String colName) {
URL url =
this.getClass().getClassLoader().getResource("/records/data.csv"); // This
will get the url for the specified file we want to read
System.out.print(url);
return res;
}
Response myResp =
Response.status(200).header("Access-Control-Allow-Origin",
"http://localhost:8448")
.header("Content-Type", "application/json")
.entity(mapper.writeValueAsString(freq)) // since we have
a map object and we need a json object, we will use the jackson library
.build();
return myResp;
}
URL url =
this.getClass().getClassLoader().getResource("/records");
File data = null;
try {
data = new File(url.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return myResp;
System.out.println("Hello world!");
String url =
"http://localhost:8080/ParsingFiles-1.0-SNAPSHOT/api/read/book";
URL netURL = new URL(url);
System.out.println(url);
System.out.println(jsonData);
inStream.close();
}
}
}
}
}
return null;
}
}