-3

I'm making a program that reads some data from a text file and then takes that data and finds the minimum, maximum, and average of the numbers. For some reason I'm getting a lot of ridiculous errors I've never seen before. Here is my code:

import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;

public class Lab1 {
static int count = 0;
static int[] newData2 = new int[count];


// Method for reading the data and putting it into different arrays
static int[] readData() {
    File f = new File("data.txt");
    int[] newData = new int[100];
    try {
        Scanner s = new Scanner(f);
        while (s.hasNext()) {
            newData[count++] = s.nextInt();

        }

        for (int i = 0; i < newData2.length; i++) {
            newData[i] = newData2[i];
            return newData2;
        }

    } catch (Exception e) {
        System.out.println("Could not read the file.");
    }
}

static int min(int[] newData2) {
    int min = newData2[0];
    for (int i = 0; i < newData2.length; i++) {
        if (newData2[i] < min) {
            min = newData2[i];
        }

    }
    return min;

}

static int max(int[] newData2) {
    int max = newData2[0];
    for (int i = 0; i < newData2.length; i++) {
        if (newData2[i] > max) {
            max = newData2[i];
        }
    }
    return max;
}

static double average(int[] newData2) {
    double average = 0;
    int sum = 0;

    for (int i = 0; i < newData2.length; i++) {
        sum = newData2[i];
    }
    average = sum / newData2.length;
    return average;
}

/*
 * static int stddev(int[] newData2) { int[] avgDif = new
 * int[newData2.length]; for(int i = 0; i < newData2.length; i++) {
 * avgDif[i] = (int) (average(newData2) - newData2[i]); }
 * 
 * }
 */

void write(String newdata, int min, int max, double average, int stddev) {
    try {
        File file = new File("stats.txt");      
        file.createNewFile();
        FileWriter writer = new FileWriter("stats.txt");
        writer.write("Minimum: " + min + "Maximum: " + max + "Average: " + average);
        writer.close();
}catch(Exception e) {
    System.out.println("Unable to write to the file.");
}

public static void main(String[] args) {


}

}
}

I have an error in my readData method, and it tells me that:

This method must return a result type of int[].

I'm literally returning an int array so I don't understand what the problem here is.

Then in my main method it says void is an invalid type for the variable main.

Logan
  • 7
  • 6
  • 1
    "For some reason I'm getting a lot of ridiculous errors I've never seen before" neither have we.. You should add the errors messages you see to you question (with stacktraces) –  Feb 10 '15 at 17:07
  • Added, sorry about that. – Logan Feb 10 '15 at 17:09
  • As an example, if `new Scanner` throws an exception, the method returns nothing –  Feb 10 '15 at 17:10
  • Also you never enter the for loop `newData2.length`is always 0 –  Feb 10 '15 at 17:13
  • Welcome to SO! Please post what your errors are so we can help you. We'er happy to assist and answer specific questions, but we won't debug your program for you. Please take a look at the [allowed questions guide](http://stackoverflow.com/help/on-topic)! – JNYRanger Feb 10 '15 at 17:13
  • @RC How can I fix something like this? I fixed `newData2.length` and changed it to count so it won't be 0 anymore. – Logan Feb 10 '15 at 17:13

2 Answers2

0
static int[] readData() {
    File f = new File("data.txt");
    int[] newData = new int[100];
    try {
        Scanner s = new Scanner(f);
        while (s.hasNext()) {
            newData[count++] = s.nextInt();

        }

        for (int i = 0; i < newData2.length; i++) {
            newData[i] = newData2[i];
            return newData2;
        }

    } catch (Exception e) {
        System.out.println("Could not read the file.");
    }

    //TODO: return something here if there is some kind of error
}

Because of the try-catch block you need to account for every possibility that could occur. When you return the array when the program succeeds you are expecting a return, but when there is an exception the program still expects a return value, but you did not provide one.

  • "Because of the try-catch block" // For an upvote, do you think he need a return statement if the try catch is removed? –  Feb 10 '15 at 17:27
  • I fixed the rest of my program and put my `return newData2` where you suggested. Now the only thing wrong is the fact that it cannot read the file. There are 5 integers in my file, and the s.hasNext() should be picking up those numbers and putting them into the `newData[count++]` array. What is causing this to mess up? – Logan Feb 10 '15 at 17:28
  • @RC If I remove the try catch block the the file reader won't work. – Logan Feb 10 '15 at 17:30
  • @Anthony Rodriguez I did some printing and my `s.hasNext();` always returns the value 0, but in my data.txt file has this in it: `9 15 20 25 30` Why is my scanner unable to grab the correct values? Where is it even getting 0's? – Logan Feb 10 '15 at 17:36
0

Here are some pointers:

  1. each exit point of a method returning something must return something, if the line new Scanner(f); throws an exception, the first return is not reached, so you need a default one, like this:
private int[] readData() {
    File f = new File("data.txt");
    int count = 0;
    int[] newData = new int[100];
    try {
        Scanner s = new Scanner(f);
        while (s.hasNext()) {
            newData[count++] = s.nextInt(); // maybe you should handle the case where your input is too large for the array "newData"
        }
        return Arrays.copyOf(newData, count);
    } catch (Exception e) {
        System.out.println("Could not read the file.");
    }

    return null;
}
  1. To reduce the size of an array, you should use Arrays.copyOf (see below)
  2. You should avoid static fields (and in your case none are required)
  3. Your method min and max are assuming there are elements in the array (at least one), you should not do that (or test it with an if):
private int min(int[] data) {
    int min = Integer.MAX_VALUE; // handy constant :)
    for (int i = 0; i < data.length; i++) {
        if (data[i] < min) {
            min = data[i];
        }
    }
    return min;
}

private int max(int[] data) {
    int max = 0;
    for (int i = 0; i < data.length; i++) {
        if (data[i] > max) {
            max = data[i];
        }
    }
    return max;
}
  1. In your average method there are a few mistakes:
private double average(int[] data) {
    int sum = 0;
    for (int i = 0; i < data.length; i++) {
        sum += data[i]; // here if you want a sum it's += not =
    }
    return (1.0 * sum) / data.length; // you want a double division, local "average" was useless
}
  1. arrays are iterables so you can use "new style" for loops:
for (int value : newData) {
    // use value
}

Some reading:

Community
  • 1
  • 1
  • Thank you so much! I'm new to using methods so they've been really confusing me. This has helped me understand them quite a bit better. Many thanks! I got my program working now. – Logan Feb 10 '15 at 18:15