Can anyone tell me why this code will compile but won\'t run? I have the program
ID: 3812629 • Letter: C
Question
Can anyone tell me why this code will compile but won't run? I have the programa nd files save under the same folder. Not sure what's going on
import java.util.*;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class DataAnalysis
{
static String fName; //Read file name
static BufferedReader buffRead=null;
private static int numDaysInMonth(String month) //read month and to determine # of days
{int count=0;
try
{
String ln2=buffRead.readLine();
ln2=buffRead.readLine();
count=1;
while(buffRead.readLine()!=null)
{
count++;
}}
catch(Exception a)
{
System.out.println(a);}
return count;
}
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in) ;
System.out.println("Please enter name of file:");
BufferedReader br1=null;
fName=scan.next();
try{
// new file input reader
buffRead = new BufferedReader(new FileReader(fName));
br1 = new BufferedReader(new FileReader(fName));
// scanner will read until end of the file
String ln1=br1.readLine();
int days=numDaysInMonth(ln1);
System.out.println("Data will be collected for "+days+" # of days");
int array[]=new int[days];
int i=0;
String ln=br1.readLine();
while((ln)!=null)
{
array[i++]=Integer.parseInt(ln);
ln=br1.readLine();
}
Double total=0.0;
for(int j=0;j {
total=total+array[j] ;
}
Double avg=total/array.length; //Find average of array in doubles
System.out.println("Average temp for "+ln1+" was: "+avg) ;
for(int j=0;j {
if(array[j]>avg)
{
System.out.println(array[j]+"+") ;
}
else
System.out.println(array[j]) ;
}
}
catch(Exception e1){
// if error occurs
e1.printStackTrace();
}
}
}
Explanation / Answer
Hi
I have fixed the compilation errors and highlighted the code changes below
DataAnalysis.java
import java.util.*;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class DataAnalysis
{
static String fName; //Read file name
static BufferedReader buffRead=null;
private static int numDaysInMonth(String month) //read month and to determine # of days
{int count=0;
try
{
String ln2=buffRead.readLine();
ln2=buffRead.readLine();
count=1;
while(buffRead.readLine()!=null)
{
count++;
}}
catch(Exception a)
{
System.out.println(a);}
return count;
}
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in) ;
System.out.println("Please enter name of file:");
BufferedReader br1=null;
fName=scan.next();
try{
// new file input reader
buffRead = new BufferedReader(new FileReader(fName));
br1 = new BufferedReader(new FileReader(fName));
// scanner will read until end of the file
String ln1=br1.readLine();
int days=numDaysInMonth(ln1);
System.out.println("Data will be collected for "+days+" # of days");
int array[]=new int[days];
int i=0;
String ln=br1.readLine();
while((ln)!=null)
{
array[i++]=Integer.parseInt(ln);
ln=br1.readLine();
}
Double total=0.0;
for(int j=0;j <array.length; j++) {
total=total+array[j] ;
}
Double avg=total/(double)array.length; //Find average of array in doubles
System.out.println("Average temp for "+ln1+" was: "+avg) ;
for(int j=0;j <array.length; j++) {
if(array[j]>avg)
{
System.out.println(array[j]+"+") ;
}
else
System.out.println(array[j]) ;
}
}
catch(Exception e1){
// if error occurs
e1.printStackTrace();
}
}
}