8. //BufferedReader data = new BufferedReader(new
InputStreamReader(dataSource.openStream()));
// get wind gust data
ArrayList tempData = new ArrayList();
String inputLine;
while ((inputLine = buffer.readLine()) != null) {
tempData.add(Double.parseDouble(inputLine.substring(20)));
}
buffer.close();
return tempData;
}
// Getting Baro Data.
public static ArrayList getbaroData(String[] arrDt) throws NumberFormatException,
IOException {
// construct the url if necessary
//URL dataSource = new URL("https://lpo.dt.navy.mil/data/DM/" + arrDt[0] + "/" +
arrDt[0] + "_" + arrDt[1] + "_" + arrDt[2] + "/" + label); URL url = new
URL("http://umd-fmis.d.umn.edu/cpblee/data/" + arrDt[0] + "/" + arrDt[0] + "_" + arrDt[1]
+ "_" + arrDt[2] +".txt/");
URL url = new URL("http://umd-fmis.d.umn.edu/cpblee/data/" + arrDt[0] + "/" +
arrDt[0] + "_" + arrDt[1] + "_" + arrDt[2] +"/" + arrDt[0] + "_" + arrDt[1] + "_" +
arrDt[2] +".txt/");
BufferedReader buffer = new BufferedReader(new
InputStreamReader(url.openConnection().getInputStream()));
// alternatively,
//BufferedReader data = new BufferedReader(new
InputStreamReader(dataSource.openStream()));
// get wind gust data
ArrayList baroData = new ArrayList();
String inputLine;
while ((inputLine = buffer.readLine()) != null) {
9. baroData.add(Double.parseDouble(inputLine.substring(20)));
}
buffer.close();
return baroData;
}
//GEtting all data
public static ArrayList getAllData(String[] arrDt) throws NumberFormatException,
IOException {
//URL dataSource = new URL("https://lpo.dt.navy.mil/data/DM/" + arrDt[0] + "/" +
arrDt[0] + "_" + arrDt[1] + "_" + arrDt[2] + "/" + label);
//URL url = new URL("http://umd-
fmis.d.umn.edu/cpblee/data/2016/2016_01_04/2016_01_04.txt");
//URL url = new URL("http://umd-fmis.d.umn.edu/cpblee/data/" + arrDt[0] + "/" +
arrDt[0] + "_" + arrDt[1] + "_" + arrDt[2] +"/" + arrDt[0] + "_" + arrDt[1] + "_" +
arrDt[2] +".txt");
URL url = new URL ("http://umd-fmis.d.umn.edu/cpblee/data/2016/ + arrData[0] + “_”+
arrDt[1]” + “_” + arrDt[2]+“/”+“ arrDt[0]” + “_”+ “arrDt[1]” + “_” + arrDt[2]+“.txt”");
// http://umd-fmis.d.umn.edu/cpblee/data/2016/2016_01_04/2016_01_04.txt
BufferedReader buffer = new BufferedReader(new
InputStreamReader(url.openConnection().getInputStream()));
//BufferedReader data = new BufferedReader(new
InputStreamReader(dataSource.openStream()));
// get date, time, and wind gust data (replicate data)
ArrayList allData = new ArrayList();
String inputLine;
while ((inputLine = buffer.readLine()) != null) {
allData.add(inputLine.substring(0));
}
10. buffer.close();
return allData;
}
public static void displayData(ArrayList a) {
for (String s : a) {
System.out.println(s);
}
}
public static double mean(ArrayList a)
{
double sum = 0;
for (double i : a) {
sum += i;
}
return sum / a.size();
}
public static double median(ArrayList a)
{
Collections.sort(a);
if (a.size() / 2 * 2 == a.size()) {
return (a.get(a.size() / 2) + a.get(a.size() / 2 - 1)) / 2;
} else {
return a.get(a.size() / 2);
}
}
}
**** this is the error i am getting:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
arrDate cannot be resolved or is not a field
arrDate cannot be resolved to a variable
Syntax error on token "else", delete this token
arrDate cannot be resolved to a variable
arrDate cannot be resolved to a variable
Syntax error, insert "}" to complete Block
at hw4p1.MeanMedianHWv2.main(MeanMedianHWv2.java:153)
11. Solution
There is a minor mistake in the else block you wrote. You just mistyped it seems. An open
paranthesis ( should be there in the else if condition, but you used open braces { it seems. Here is
the modificeation for you:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class MeanMedian {
public static void main(String[] args) throws IOException {
String[] arrDate = { "", "", "","" };
Scanner in = new Scanner(System.in);
Boolean day = true;
// Year
do
{
System.out.println("Enter a year (2016)");
arrDate[0] = in.next();
if (!arrDate[0].equalsIgnoreCase("2016"))
{
System.out.println("Wrong Input. '2016' only! Try again.");
}
else
{
arrDate[0] = "2016";
}
} while(!arrDate[0].equalsIgnoreCase("2016"));
12. // Month
do
{
System.out.println("Enter a month (01)");
arrDate[1] = in.next();
if (!arrDate[1].equalsIgnoreCase("01"))
{
System.out.println("Wrong Input. '01' only! Try again!");
}
else
{
arrDate[1] = "01";
}
} while(!arrDate[1].equalsIgnoreCase("01"));
// Day
do
{
System.out.println("Enter a Day (04-10)");
arrDate[2] = in.next();
//if (arrDate[2] > 10 || arrDate[2] < 4)
if (!arrDate[2].equalsIgnoreCase("04"))
{
System.out.println("Wrong Inpit. '04' - '10' only! Try again!");
}
//else if (arrDate[2] == 4)
else if (!arrDate[2].equalsIgnoreCase("05"))
{
System.out.println("Error! Invalid day. Try again!");
}
13. else if (!arrDate[2].equalsIgnoreCase("06"))
{
System.out.println("Error! Invalid day. Try again!");
}
else if (!arrDate[2].equalsIgnoreCase("07"))
{
System.out.println("Error! Invalid day. Try again!");
}
else if (!arrDate[2].equalsIgnoreCase("08"))
{
System.out.println("Error! Invalid day. Try again!");
}
else if (!arrDate[2].equalsIgnoreCase("09"))
{
System.out.println("Error! Invalid day. Try again!");
}
else if (!arrDate[2].equalsIgnoreCase("10"))
{
System.out.println("Error! Invalid day. Try again!");
}
else
{
arrDate[2] = "04";
day = false;
arrDate[2] = "05";
day = false;
arrDate[2] = "06";
day = false;
arrDate[2] = "07";
14. day = false;
arrDate[2] = "08";
day = false;
arrDate[2] = "09";
day = false;
arrDate[2] = "10";
day = false;
}
} while (day);
do {
System.out.println("which data? 'w' for wind, 'b' for baro pressure, 't' for temp");
arrDate[3] = in.next();
if (!arrDate[3]. equalsIgnoreCase ("w") && !arrDate[3].equalsIgnoreCase("b") &&
!arrDate[3].equalsIgnoreCase("t"))
{
System.out.println("Wrong input. 'w' / 'b' / 't' only! Try again");
}
//while(!arrDate[2].equalsIgnoreCase("04") && !arrDate[2].equalsIgnoreCase("05") &&
!arrDate[2].equalsIgnoreCase("05"));
if (arrDate[3].equalsIgnoreCase("w"))
{
System.out.println("w");
}
else if (arrDate[3].equalsIgnoreCase("b"))
{
System.out.println("b");
}
else if (arrDate[3].equalsIgnoreCase("t"))
{
System.out.println("t");
}
} while (!arrDate[3].equalsIgnoreCase("w") && !arrDate[3].equalsIgnoreCase("b") &&
!arrDate[3].equalsIgnoreCase("t"));
in.close();
18. // alternatively,
//BufferedReader data = new BufferedReader(new
InputStreamReader(dataSource.openStream()));
// get wind gust data
ArrayList tempData = new ArrayList();
String inputLine;
while ((inputLine = buffer.readLine()) != null) {
tempData.add(Double.parseDouble(inputLine.substring(20)));
}
buffer.close();
return tempData;
}
// Getting Baro Data.
public static ArrayList getbaroData(String[] arrDt) throws NumberFormatException,
IOException {
// construct the url if necessary
//URL dataSource = new URL("https://lpo.dt.navy.mil/data/DM/" + arrDt[0] + "/" + arrDt[0]
+ "_" + arrDt[1] + "_" + arrDt[2] + "/" + label); URL url = new URL("http://umd-
fmis.d.umn.edu/cpblee/data/" + arrDt[0] + "/" + arrDt[0] + "_" + arrDt[1] + "_" + arrDt[2]
+".txt/");
URL url = new URL("http://umd-fmis.d.umn.edu/cpblee/data/" + arrDt[0] + "/" + arrDt[0] +
"_" + arrDt[1] + "_" + arrDt[2] +"/" + arrDt[0] + "_" + arrDt[1] + "_" + arrDt[2]
+".txt/");
BufferedReader buffer = new BufferedReader(new
InputStreamReader(url.openConnection().getInputStream()));
// alternatively,
//BufferedReader data = new BufferedReader(new
InputStreamReader(dataSource.openStream()));
// get wind gust data
ArrayList baroData = new ArrayList();
String inputLine;
19. while ((inputLine = buffer.readLine()) != null) {
baroData.add(Double.parseDouble(inputLine.substring(20)));
}
buffer.close();
return baroData;
}
//GEtting all data
public static ArrayList getAllData(String[] arrDt) throws NumberFormatException,
IOException {
//URL dataSource = new URL("https://lpo.dt.navy.mil/data/DM/" + arrDt[0] + "/" + arrDt[0]
+ "_" + arrDt[1] + "_" + arrDt[2] + "/" + label);
//URL url = new URL("http://umd-
fmis.d.umn.edu/cpblee/data/2016/2016_01_04/2016_01_04.txt");
//URL url = new URL("http://umd-fmis.d.umn.edu/cpblee/data/" + arrDt[0] + "/" + arrDt[0]
+ "_" + arrDt[1] + "_" + arrDt[2] +"/" + arrDt[0] + "_" + arrDt[1] + "_" + arrDt[2]
+".txt");
URL url = new URL ("http://umd-fmis.d.umn.edu/cpblee/data/2016/ + arrData[0] + “_”+
arrDt[1]” + “_” + arrDt[2]+“/”+“ arrDt[0]” + “_”+ “arrDt[1]” + “_” + arrDt[2]+“.txt”");
// http://umd-fmis.d.umn.edu/cpblee/data/2016/2016_01_04/2016_01_04.txt
BufferedReader buffer = new BufferedReader(new
InputStreamReader(url.openConnection().getInputStream()));
//BufferedReader data = new BufferedReader(new
InputStreamReader(dataSource.openStream()));
// get date, time, and wind gust data (replicate data)
ArrayList allData = new ArrayList();
String inputLine;
while ((inputLine = buffer.readLine()) != null) {
allData.add(inputLine.substring(0));
20. }
buffer.close();
return allData;
}
public static void displayData(ArrayList a) {
for (String s : a) {
System.out.println(s);
}
}
public static double mean(ArrayList a)
{
double sum = 0;
for (double i : a) {
sum += i;
}
return sum / a.size();
}
public static double median(ArrayList a)
{
Collections.sort(a);
if (a.size() / 2 * 2 == a.size()) {
return (a.get(a.size() / 2) + a.get(a.size() / 2 - 1)) / 2;
} else {
return a.get(a.size() / 2);
}
}
}
And may be you tried some methods which are not here it seems. Anyways, if you still need any
refinements, just get back to me.