I\'m trying to read a file into my code but the return statement (in bold) keeps
ID: 3603167 • Letter: I
Question
I'm trying to read a file into my code but the return statement (in bold) keeps getting the error: cannot return double [] as double. Why is that?
public class ReadInData {
public static void main (String [] args){
String user = " ";
while (true) {
System.out.println("Enter the option average, hottest, coldest:");
Scanner input = new Scanner (System.in);
user = input.nextLine();
double x = theArray();
if (user.equals("average")){
findAverage(x);
break;
}
else if (user.equals("hottest")){
findHottestYearIndex(x);
break;
}
else if (user.equals("coldest")){
findColdestYearIndex(x);
break;
}
else if (user.equals("quit")){
System.out.println("Program done.");
break;
}
else {
System.out.println("Invalid input.");
continue;
}
}
}
public static double theArray(){
double [] y = new double [200];
try {
File weather = new File("~/Users/MyName/Desktop/heatwave.txt");
Scanner input = new Scanner(weather);
for(int i=0;i<y.length;i++){
y[i] = input.nextDouble();
}
}
catch(FileNotFoundException ex) {
System.out.println("File not found");
}
return y;
}
Explanation / Answer
Note:
1)As i dont have the input file..just i wrote overall code.One thing is that do u know the input file has 200 entries.because U created an double type array size 200 and your are using for loop which iterates for 200 times. If 200 entries is not there in the file you will get NO SUCH ELEMENT EXCEPTION.
2)You are expecting HOttest and Coldest year index ,but we have to pass the year array so that based on the index we can dispay hottest year and coldest year.
If u have any doubt further regarding this code u can ask me i will help you.
________________
ReadInData.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadInData {
public static void main (String [] args){
String user = " ";
while (true) {
System.out.println("Enter the option average, hottest, coldest:");
Scanner input = new Scanner (System.in);
user = input.nextLine();
double[] x = theArray();
if (user.equals("average")){
findAverage(x);
break;
}
else if (user.equals("hottest")){
findHottestYearIndex(x);
break;
}
else if (user.equals("coldest")){
findColdestYearIndex(x);
break;
}
else if (user.equals("quit")){
System.out.println("Program done.");
break;
}
else {
System.out.println("Invalid input.");
continue;
}
}
}
private static void findColdestYearIndex(double[] x) {
double min=x[0];
int index = 0;
for(int i=0;i<x.length;i++)
{
if(min<x[i])
{
min=x[i];
index=i;
}
}
System.out.println("Coldest Year Index :"+index);
}
private static void findHottestYearIndex(double[] x) {
double max=x[0];
int index = 0;
for(int i=0;i<x.length;i++)
{
if(max<x[i])
{
max=x[i];
index=i;
}
}
System.out.println("Hottest Year index:"+index);
}
private static void findAverage(double[] x) {
double tot=0,avg=0;
for(int i=0;i<x.length;i++)
{
tot+=x[i];
}
System.out.printf("Average :%.2f",(tot/x.length));
}
public static double[] theArray(){
double [] y = new double [200];
try {
File weather = new File("heatwave.txt");
Scanner input = new Scanner(weather);
for(int i=0;i<y.length;i++){
y[i] = input.nextDouble();
}
}
catch(FileNotFoundException ex) {
System.out.println("File not found");
}
return y;
}
}
_______________