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

Recommended Answers

All 34 Replies

You're reading a file by making a Scanner on that file. The Scanner's constructor takes a String.

What if you made a method

readFile(String filePathName){}

which accepted the name of the file as a parameter and used that to construct a new Scanner. That should eliminate your extra file-reading methods.

-------------
(afterthought)
You'd also have to capture the array that you're returning from that method, instead of discarding it as you're doing now. So instead of having the method assign to a globally declared array, you build a locally declared array and return that. Your method call then looks like

array3 = readFile(pathToFileThree);

where "pathToFileThree" is a String with the appropriate path/name in it.

I don't understand the parameter in the method.
Since it is one method to interact with all three files......how would you define that in the main method? would I not use the scanners in the main method at all? And also, do I not need to declare the arrays in my main if I am doing it in the method? And also ugh sorry but if I am making the arrays in the method how do I get it so that I have an array for each file?

I don't understand the parameter in the method.
Since it is one method to interact with all three files......how would you define that in the main method? would I not use the scanners in the main method at all?

Nope. You open a Scanner on a file, and you read the file. What do you do with the Scanner after that? Nothing. There's no reason for the object to persist - all it lives for is to open up a file and hand you the contents, then it goes peacefully to the Garbage Collector. So there's no need for it to exist outside of the local context in which it does its work. So you'll declare the Scanner within the readFile() method, and the file that it's going to look at will be whatever it is you tell it to look at. Right now, you're hard-coding a location for each file.

Try this:

Instead of

Scanner firstFile = new Scanner(new File("/Volumes/SCII CE USB/temps1.txt"));

have

String firstFileName = "/Volumes/SCII CE USB/temps1.txt";
    Scanner firstFile = new Scanner(new File(firstFileName));

That's a small change - what have I done? Instead of a literal value, I'm using a reference to a String. Okay, so the constructor for File takes that reference, and it goes and finds a file, and returns a File on it, which Scanner is able to use as a data source.
So now we know that reference to a String is as good as the String itself.

When you do a method call,

double foo[] = readFile(firstFileName);

That's passing the String reference to the method readFile()

What's in readFile? Well, it'll be

public double[] readFile(String fileName)
{
    Scanner scan = new Scanner(new File(fileName));
... 
}

So if firstFileName is the same in both cases, the Scanner will find the same file in both cases. And if later on you have

double bar[] = readFile(secondFileName);
double frotz[] = readFile(thirdFileName);

Then you'll have read the files described in secondFileName and thirdFileName as well, and assigned the resulting arrays to bar[] and frotz[].
Make more sense now?

I think you had another question, but I have to go stir some soup. Back later.

Well I am still not getting it right but thanks to you I realized my issue was with the fact that I forgot that the actual and formal parameter names DO NOT need to be the same and that is one of the reasons I was getting so confused but here is my fixed but not working code so far lol

import java.io.*;
import java.util.*;

public class arrayFilesTest
{
  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
  {
    String firstFile = "/Volumes/SCII CE USB/temps1.txt";
    String secondFile = "/Volumes/SCII CE USB/temps2.txt";
    String thirdFile = "/Volumes/SCII CE USB/temps3.txt";
        
    double num;
    int index = 0;
    int count = 0;
    
    double[] arrayOne = readFile(firstFile,index);
    double[] arrayNTwo = readFile(secondFile,index);
    double[] arrayThree = readFile(thirdFile,index);

    printArray(arrayOne);
    
  }
  
  public static void printArray(double[] arrayOne)
  {
    for (int i = 0; i < arrayOne.length; i++)
    {
    System.out.print("The contents of array one are: " + arrayOne[i]);
    System.out.println();
    }
  }
  public static double[] readFile(String fileName, int index) throws FileNotFoundException
  {
    Scanner readIt = new Scanner(new File(fileName));
    while (readIt.hasNextDouble())
    {
      double[] tempArray = new double[index];
      tempArray[index] = readIt.nextDouble();
      index++;
    }
    return tempArray;
  }
}

I am hoping this is slightly better than before =)

Much better. Doesn't it feel better? When you look at this, you should be thinking "Ah, that's much better".

Now I see that youre passing in the array index as a parameter to readFile. Since this will always be zero to begin with, it makes sense to declare that as a local variable. Other than that, your readFile looks pretty good.

You'll notice that your maxTemps constants are no longer needed. This is good, it means you're working closer to the data. You're no longer having to tell the machine how many data points to accept, you're letting it go find all of the data that there is and bring it back.

PrintArray looks pretty good as well - you could go ahead and use it to print arrayNTwo and arrayThree without any trouble. Your local declaration of arrayOne will shadow the global arrayOne, so any reference to arrayOne[n] in the method printArray will be a reference to the arrayOne that you've used as a parameter, not the arrayOne that you declared in the main() method. This is an important thing to know about, so make sure you see how that works.

You should be able to calculate your averages and extremes now without much trouble.

I seem to be having trouble with my readFile method though.....it is having an issue with the return statement and I am unsure as to why.
says cannot find symbol: variable tempArray

Ok i took the declaring the array outside of the while loop before it in the method but it won't print now .....gives me an out of bounds error

What does the stack trace look like?

what's a stack trace O_O
but the error message is

java.lang.ArrayIndexOutOfBoundsException: 0
	at arrayFilesTest.readFile(arrayFilesTest.java:39)
	at arrayFilesTest.main(arrayFilesTest.java:16)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
>

Oh and I tried to treat the print method like the file method and I boned that as well

import java.io.*;
import java.util.*;

public class arrayFilesTest
{
  public static void main(String[] args) throws FileNotFoundException
  {
    String firstFile = "/Volumes/SCII CE USB/temps1.txt";
    String secondFile = "/Volumes/SCII CE USB/temps2.txt";
    String thirdFile = "/Volumes/SCII CE USB/temps3.txt";
        
    double num;
    int index = 0;
    int count = 0;
    
    double[] arrayOne = readFile(firstFile,index);
    double[] arrayNTwo = readFile(secondFile,index);
    double[] arrayThree = readFile(thirdFile,index);

    printArray(arrayOne);
    printArray(arrayTwo);
    printArray(arrayThree);
  }
  
  public static void printArray(double[] arrayOne)
  {
    for (int i = 0; i < arrayOne.length; i++)
    {
    System.out.print("The contents of array one are: " + arrayOne[i]);
    System.out.println();
    }
  }
  public static double[] readFile(String fileName, int index) throws FileNotFoundException
  {
    Scanner readIt = new Scanner(new File(fileName));
    double[] tempArray = new double[index];
    while (readIt.hasNextDouble())
    {
      tempArray[index] = readIt.nextDouble();
      index++;
    }
    return tempArray;
  }
}

I guess you can't do the same thing with a void method that you can with a value returning method? I don't know how I screwed this up again.

That's a stack trace. I see - you're declaring the array to be an array of length index. Index at that point is sero, so you've got an array of length zero.

Well I can't use a constant since the file method would be using files with different lengths so how would I go about this? I can't leave it blank can I?

One thing to do would be to just pick a number that you're sure will be large enough to hold any data you'll be reading, and use that. That's probably the best thing at this stage.
You can put in a sentinel value after you've read all the data. Write something like "DONE" in the cell after your last data, and when you're reading you'll know where to stop.


If you want to learn about a handy built-in structure, read up on java.util.ArrayList - that would be what I'd probably use here. But for your current needs, just assign yourself some headroom.

Won't I get an error for choosing a number that is too large?

And also when I do the averages and figure out the maximum number and the minimum number won't this mess it up?

Won't I get an error for choosing a number that is too large?

No, you'll just have an array with empty space at the end.

And also when I do the averages and figure out the maximum number and the minimum number won't this mess it up?

Mm, yes, you'll probably have to change the way you handle reading the array. Read until you hit the sentinel value, then quit, rather than reading to the length of the array.
(Oh, of course you can't put "DONE" in the array, because it's doubles, you'll have figured that out already. You'll have to pick a value that's out of range for your data set, but in the range of a double.)

Here is the complete program as it is.....I get the wrong output because of the index being different for each file though.

import java.io.*;
import java.util.*;

public class arrayFilesTest
{
  public static void main(String[] args) throws FileNotFoundException
  {
    String firstFile = "/Volumes/SCII CE USB/temps1.txt";
    String secondFile = "/Volumes/SCII CE USB/temps2.txt";
    String thirdFile = "/Volumes/SCII CE USB/temps3.txt";
        
    double num;
    int index = 0;
    int count = 0;
    
    double[] arrayOne = readFile(firstFile,index);
    double[] arrayTwo = readFile(secondFile,index);
    double[] arrayThree = readFile(thirdFile,index);
    
    System.out.println("The contents of array one are: ");
    printArray(arrayOne);
    System.out.println("The average of array one is: " + arrayAverage(arrayOne));
    countDays(arrayOne);
    System.out.println("The greatest number is: " + greatest(arrayOne));
    System.out.println("The lowest number is: " + lowest(arrayOne));
    System.out.println();
    System.out.println("The contents of array two are: ");
    printArray(arrayTwo);
    System.out.println("The average of array two is: " + arrayAverage(arrayTwo));
    countDays(arrayTwo);
    System.out.println("The greatest number is: " + greatest(arrayTwo));
    System.out.println("The lowest number is: " + lowest(arrayTwo));
    System.out.println();
    System.out.println("The contents of array three are: ");
    printArray(arrayThree);
    System.out.println("The average of array three is: " + arrayAverage(arrayThree));
    countDays(arrayThree);
    System.out.println("The greatest number is: " + greatest(arrayThree));
    System.out.println("The lowest number is: " + lowest(arrayThree));
  }
  
  public static void printArray(double[] arrayOne)
  {
    for (int i = 0; i < arrayOne.length; i++)
    {
    System.out.print(arrayOne[i]);
    System.out.println();
    }
  }
  public static double[] readFile(String fileName, int index) throws FileNotFoundException
  {
    Scanner readIt = new Scanner(new File(fileName));
    double[] tempArray = new double[14];
    while (readIt.hasNextDouble())
    {
      tempArray[index] = readIt.nextDouble();
      index++;
    }
    return tempArray;
  }
  public static void countDays(double[] arrayOne)
  {
    int aboveCount = 0;
    int belowCount = 0;
    int sumAbove = 0;
    int sumBelow = 0;
      
    for (int i = 0; i < arrayOne.length; i++)
    {
      if (arrayOne[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[] arrayOne)
  {
    int maxIndex = 0;
    for (int index = 1; index < arrayOne.length; index++)
      if (arrayOne[maxIndex] < arrayOne[index])
      maxIndex = index;

    return arrayOne[maxIndex];
  }
  public static double lowest(double[] arrayOne)
  {
    int minIndex = 0;
    for (int index = 1; index < arrayOne.length; index++)
    if (arrayOne[minIndex] > arrayOne[index])
      minIndex = index;

    return arrayOne[minIndex];
  }
  public static double arrayAverage(double[] arrayOne)
  {
    double sum = 0;
    for (int i = 0; i < arrayOne.length; i++)
      sum += arrayOne[i];
    return sum/arrayOne.length;
  }
}

Are you sure I can't use the constants at all? Maybe in the declaring of the arrays in the main or something?
Because I don't know how I would go about setting it up as a loop inside of the file method =(

If you want, you can put the constants in, sure. That should get it working, anyway. But why not try writing a sentinel value? You can set it in the field declarations:
final double SENTINEL = 100000; //temperatures over 99999 are not expected, I presume?

When you're writing to the array, after you've got the last value you're going to put in the array, set the following slot to SENTINEL. When you're reading, read until the value==SENTINEL.

The advantage of this is that if I were to sneak into your file system and replace your data files with different ones, you'd be fine. If you've hard-coded your file lengths, you're going to have some trouble. Flexibility, we like.

Well the files have pre-determined temperatures in there....first file I believe has 10 numbers......second one has I think 4 or 5 numbers and the third file has 13 numbers.
But I learned about sentinel values briefly during the whole learning while loops and such.......still not sure how to do that when dealing with files and also if I set my constants back to the way I had them before where there was a constant for each array how would I do that? I am not using the method to put together each array so I can't put constants in the readFile method at all so I would assume they would HAVE to be in the main method but with declaring the array in main is for example double[] arrayOne = readFile(firstFile,index); there is no way to place a constant in there it seems to me

there is no way to place a constant in there it seems to me

No, you'd have to pass it in as a parameter.
readFile(String fileName, int fileSize)

and then use fileSize for the length of your array.

Ah I think I might understand.....another bout of me being retarded concerning parameters lol.....ugh it's been a rough time in learning java so far

Keep making mistakes, they're your best teachers.

As the midget in Poltergeist said.......this house is now clean

w00t!

import java.io.*;
import java.util.*;

public class arrayFilesTest
{
  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
  {
    String firstFile = "/Volumes/SCII CE USB/temps1.txt";
    String secondFile = "/Volumes/SCII CE USB/temps2.txt";
    String thirdFile = "/Volumes/SCII CE USB/temps3.txt";
        
    double num;
    int index = 0;
    int count = 0;
    
    double[] arrayOne = readFile(firstFile,index,MAX_TEMPS);
    double[] arrayTwo = readFile(secondFile,index,MAX_TEMPS2);
    double[] arrayThree = readFile(thirdFile,index,MAX_TEMPS3);
    
    System.out.println("The contents of array one are: ");
    printArray(arrayOne);
    System.out.println("The average of array one is: " + arrayAverage(arrayOne));
    countDays(arrayOne);
    System.out.println("The greatest number is: " + greatest(arrayOne));
    System.out.println("The lowest number is: " + lowest(arrayOne));
    System.out.println();
    System.out.println("The contents of array two are: ");
    printArray(arrayTwo);
    System.out.println("The average of array two is: " + arrayAverage(arrayTwo));
    countDays(arrayTwo);
    System.out.println("The greatest number is: " + greatest(arrayTwo));
    System.out.println("The lowest number is: " + lowest(arrayTwo));
    System.out.println();
    System.out.println("The contents of array three are: ");
    printArray(arrayThree);
    System.out.println("The average of array three is: " + arrayAverage(arrayThree));
    countDays(arrayThree);
    System.out.println("The greatest number is: " + greatest(arrayThree));
    System.out.println("The lowest number is: " + lowest(arrayThree));
  }
  
  public static void printArray(double[] arrayOne)
  {
    for (int i = 0; i < arrayOne.length; i++)
    {
    System.out.print(arrayOne[i]);
    System.out.println();
    }
  }
  public static double[] readFile(String fileName, int index, int fileSize) throws FileNotFoundException
  {
    Scanner readIt = new Scanner(new File(fileName));
    double[] tempArray = new double[fileSize];
    while (readIt.hasNextDouble())
    {
      tempArray[index] = readIt.nextDouble();
      index++;
    }
    return tempArray;
  }
  public static void countDays(double[] arrayOne)
  {
    int aboveCount = 0;
    int belowCount = 0;
    int sumAbove = 0;
    int sumBelow = 0;
      
    for (int i = 0; i < arrayOne.length; i++)
    {
      if (arrayOne[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[] arrayOne)
  {
    int maxIndex = 0;
    for (int index = 1; index < arrayOne.length; index++)
      if (arrayOne[maxIndex] < arrayOne[index])
      maxIndex = index;

    return arrayOne[maxIndex];
  }
  public static double lowest(double[] arrayOne)
  {
    int minIndex = 0;
    for (int index = 1; index < arrayOne.length; index++)
    if (arrayOne[minIndex] > arrayOne[index])
      minIndex = index;

    return arrayOne[minIndex];
  }
  public static double arrayAverage(double[] arrayOne)
  {
    double sum = 0;
    for (int i = 0; i < arrayOne.length; i++)
      sum += arrayOne[i];
    return sum/arrayOne.length;
  }
}

This is the final code so far except for ONE LAST PART lol
I need to make a method or search function that returns the corresponding index for any value I search for. Oh no....here goes another hour of toiling on the ol' laptop lol

Ok here is the updated code with my search function that doesn't work bleh but I feel that I am pretty close

import java.io.*;
import java.util.*;

public class arrayFilesTest
{
  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
  {
    String firstFile = "/Volumes/SCII CE USB/temps1.txt";
    String secondFile = "/Volumes/SCII CE USB/temps2.txt";
    String thirdFile = "/Volumes/SCII CE USB/temps3.txt";
        
    double num;
    int index = 0;
    int count = 0;
    
    double[] arrayOne = readFile(firstFile,index,MAX_TEMPS);
    double[] arrayTwo = readFile(secondFile,index,MAX_TEMPS2);
    double[] arrayThree = readFile(thirdFile,index,MAX_TEMPS3);
    Scanner input = new Scanner(System.in);

    System.out.println("The contents of array one are: ");
    printArray(arrayOne);
    System.out.println("The average of array one is: " + arrayAverage(arrayOne));
    countDays(arrayOne);
    System.out.println("The greatest number is: " + greatest(arrayOne));
    System.out.println("The lowest number is: " + lowest(arrayOne));
    System.out.println("Enter Search Number: ");
    int searchItem = input.nextInt();
    System.out.println(find(arrayOne,searchItem));
    System.out.println();
    System.out.println("The contents of array two are: ");
    printArray(arrayTwo);
    System.out.println("The average of array two is: " + arrayAverage(arrayTwo));
    countDays(arrayTwo);
    System.out.println("The greatest number is: " + greatest(arrayTwo));
    System.out.println("The lowest number is: " + lowest(arrayTwo));
    System.out.println();
    System.out.println("The contents of array three are: ");
    printArray(arrayThree);
    System.out.println("The average of array three is: " + arrayAverage(arrayThree));
    countDays(arrayThree);
    System.out.println("The greatest number is: " + greatest(arrayThree));
    System.out.println("The lowest number is: " + lowest(arrayThree));
  }
  
  public static void printArray(double[] arrayOne)
  {
    for (int i = 0; i < arrayOne.length; i++)
    {
    System.out.print(arrayOne[i]);
    System.out.println();
    }
  }
  public static double[] readFile(String fileName, int index, int fileSize) throws FileNotFoundException
  {
    Scanner readIt = new Scanner(new File(fileName));
    double[] tempArray = new double[fileSize];
    while (readIt.hasNextDouble())
    {
      tempArray[index] = readIt.nextDouble();
      index++;
    }
    return tempArray;
  }
  public static void countDays(double[] arrayOne)
  {
    int aboveCount = 0;
    int belowCount = 0;
    int sumAbove = 0;
    int sumBelow = 0;
      
    for (int i = 0; i < arrayOne.length; i++)
    {
      if (arrayOne[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[] arrayOne)
  {
    int maxIndex = 0;
    for (int index = 1; index < arrayOne.length; index++)
      if (arrayOne[maxIndex] < arrayOne[index])
      maxIndex = index;

    return arrayOne[maxIndex];
  }
  public static double lowest(double[] arrayOne)
  {
    int minIndex = 0;
    for (int index = 1; index < arrayOne.length; index++)
    if (arrayOne[minIndex] > arrayOne[index])
      minIndex = index;

    return arrayOne[minIndex];
  }
  public static double arrayAverage(double[] arrayOne)
  {
    double sum = 0;
    for (int i = 0; i < arrayOne.length; i++)
      sum += arrayOne[i];
    return sum/arrayOne.length;
  }
  public static double find(double[] arrayOne, int searchItem)
  {
    for (int i = 0; i < arrayOne.length; i--)
    { 
      if (arrayOne[i] == searchItem)
        return i;
    }
    return -1;
  }
}

Try making searchItem a double.

Still didn't work for some reason........this search that I am doing is by index and then prints the number in that index spot or am I searching for the actual number in the file? either way it isn't working out lol

Um, just out of curiousity, why are you decrementing the index in your search function? I'd think that you'd be seeing an indexOutOfBounds - in any case, you wouldn't be seeing the contents of the array.

It was one of the ways we learned how to do this in the class i am taking, however we see all of this on power points and not actual examples during the class...so.....i guess to me it all seems theory lol

If you start at zero, and decrement from there, you're going to be looking at index values 0, -1, -2....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.