Ok here is my problem thus far.
My program I have created is horrible lol...I am adult enough to admit it. My issue is the output I am getting that I will post after my code. It has weird characters at the top and I am not sure why. The second problem that I am having is the fact that I have made 3 separate methods to read 3 separate files. I would love to have a way to do this with just one method. IT would make things so much easier for me but I have no clue how to do this since the 3 files have 3 different names. Also, I need to be able to use the arrays for other methods I have that give me sums and averages and the like.
Well here is my first code that does ALL of my needed calculations but it doesn't involve files. Also, I need to keep my code as simple as possible.....not tokenizers or buffer readers or anything I have NOT learned. If I wanted that I could of copied code off the net but I will not do that...I want to know how to do this not just copy and paste code LOL
import java.util.*;
public class arrayOne
{
public static final int MAX_TEMPS = 10;
public static void main(String[] args)
{
double[] temperatures = new double[MAX_TEMPS];
System.out.println("Enter 10 Celsius Temperatures!");
Scanner input = new Scanner(System.in);
for (int index = 0; index < temperatures.length; index++)
temperatures[index] = input.nextDouble();
printArray(temperatures);
System.out.println("The average of the temperatures entered is: " + arrayAverage(temperatures));
countDays(temperatures);
System.out.println("The greatest temperature is: " + greatest(temperatures));
System.out.println("The lowest temperature is: " + lowest(temperatures));
}
public static void printArray(double[] temperatures)
{
for (int i = 0; i < temperatures.length; i++)
System.out.println(temperatures[i]);
}
public static double arrayAverage(double[] temperatures)
{
double sum = 0;
for (int i = 0; i < temperatures.length; i++)
sum += temperatures[i];
return sum/temperatures.length;
}
public static void countDays(double[] temperatures)
{
int aboveCount = 0;
int belowCount = 0;
int sumAbove = 0;
int sumBelow = 0;
for (int i = 0; i < temperatures.length; i++)
{
if (temperatures[i] > 32)
aboveCount++;
else
belowCount++;
}
sumAbove += aboveCount;
sumBelow += belowCount;
System.out.println("number of days above freezing: " + sumAbove);
System.out.println("number of days below freezing: " + sumBelow);
}
public static double greatest(double[] temperatures)
{
int maxIndex = 0;
for (int index = 1; index < temperatures.length; index++)
if (temperatures[maxIndex] < temperatures[index])
maxIndex = index;
return temperatures[maxIndex];
}
public static double lowest(double[] temperatures)
{
int minIndex = 0;
for (int index = 1; index < temperatures.length; index++)
if (temperatures[minIndex] > temperatures[index])
minIndex = index;
return temperatures[minIndex];
}
}
And here is the code when I attempted to switch it from regular input to accepting files as the input.
import java.io.*;
import java.util.*;
public class arrayFiles
{
public static final int MAX_TEMPS = 10;
public static final int MAX_TEMPS2 = 5;
public static final int MAX_TEMPS3 = 13;
public static void main(String[] args) throws FileNotFoundException
{
Scanner firstFile = new Scanner(new File("/Volumes/SCII CE USB/temps1.txt"));
Scanner secondFile = new Scanner(new File("/Volumes/SCII CE USB/temps2.txt"));
Scanner thirdFile = new Scanner(new File("/Volumes/SCII CE USB/temps3.txt"));
double num;
int index = 0;
double[] arrayOne = new double[MAX_TEMPS];
double[] arrayTwo = new double[MAX_TEMPS2];
double[] arrayThree = new double[MAX_TEMPS3];
double[] arrayNewOne = readFileOne(firstFile,arrayOne,index);
System.out.println(readFileOne(firstFile,arrayOne,index));
System.out.println(readFileTwo(secondFile,arrayTwo,index));
System.out.println(readFileThree(thirdFile,arrayThree,index));
printArray(arrayNewOne);
}
public static void printArray(double[] arrayNewOne)
{
for (int i = 0; i < arrayNewOne.length; i++)
{
System.out.print("The contents of array one are: " + arrayNewOne[i]);
System.out.println();
}
}
public static double[] readFileOne (Scanner firstFile, double[] arrayOne, int index)
{
while (firstFile.hasNextDouble())
{
arrayOne[index] = firstFile.nextDouble();
index++;
}
return arrayOne;
}
public static double[] readFileTwo (Scanner secondFile, double[] arrayTwo, int index)
{
while (secondFile.hasNextDouble())
{
arrayTwo[index] = secondFile.nextDouble();
index++;
}
return arrayTwo;
}
public static double[] readFileThree (Scanner thirdFile, double[] arrayThree, int index)
{
while (thirdFile.hasNextDouble())
{
arrayThree[index] = thirdFile.nextDouble();
index++;
}
return arrayThree;
}
}
here is my weird output.
> run arrayFiles
The contents of array one are: 40.3
The contents of array one are: 35.8
The contents of array one are: 29.6
The contents of array one are: 45.0
The contents of array one are: 17.8
The contents of array one are: 19.2
The contents of array one are: 38.6
The contents of array one are: 31.5
The contents of array one are: 27.8
The contents of array one are: 39.9
>
Also is there a way to fix my printArray method to be able to be used to print all of the arrays instead of creating 2 more separate methods for printing? And for some reason the strange characters in my output didn't happen this time but still my methods are most likely horribly flawed. Please help I am totally stressed LOL