0

So my task is to read a file line by line and store the integers into an array. Then to add the integers in spots 1-5, 2-6, 3-7 etc. and store those into a new array.

In array 1 there is 4 more values than array 2. I need to compare these Arrays and see if array1 is 0.999 bigger than array2.

If it is indeed larger, I need to print out the LOCATION of the number in the array 1.

Right now my problem is my code is outputting that every number is larger than the corresponding number in array 2.

Code:

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class Asgn7
{
    public static void main(String[] args) throws FileNotFoundException
    {   
        Scanner file = new Scanner(new File("asgn7data.txt"));
        double[] array = new double[file.nextInt()];
        double[] newArray = new double[array.length - 4];
        double tempVal = 0;
        int j = 0;
        int count = 0;

        while(file.hasNext())
        {
        for(int i = 0; i < array.length ; i++)
        {
            array[i] = file.nextInt();
        }

        for(j = 0; j < array.length - 4; j++)
        {
            for(int k = 0; k < 5; k++)
            {
                newArray[j] += array[j+k] / 5;  
             }

        }

        for(int i = 2; i < array.length; i++)
        {
            if(array[i] > (newArray[i-2] + 0.999));
            {
                count++;
                tempVal = count;
            }

             System.out.println(tempVal);
        }   
        }

    }
}

enter image description here

The values which should be compared are from 3-13.

Jon Roy
  • 53
  • 1
  • 11
  • Not sure about your logic. The numbers in `newArray` are not as you specified, but rather the average of the five numbers in sports 1-5, the average of 2-6 etc. In addition you are comparing all the numbers in `array` to one, specific element in `newArray` (element `j-2`, and `j` doesn't change. What exactly are you trying to achieve by that? – RealSkeptic Nov 22 '15 at 15:17
  • The average thing, is right, thats my bad. Should it be i-2? – Jon Roy Nov 22 '15 at 15:19
  • @RealSkeptic -> The i=2 is there so the array[i] starts at the third index, and and j-2 was there because when i put i-2 with the newArray[i-2] i get an out of bounds error – Jon Roy Nov 22 '15 at 15:37
  • So now the question is, what exactly are you trying to compare? Why the third index? Since the arrays are not the same length, of course you will get an out of bounds error. So edit your question and explain better what numbers should be checked to be within 0.999 of each other. – RealSkeptic Nov 22 '15 at 16:00
  • @RealSkeptic I just added a picture to the original post. The values which have two numbers should be compared to eachother. The ones shaded grey should be the ones that come back true. – Jon Roy Nov 22 '15 at 16:12
  • Please clarify you question. It's not clear, What you want to do and your problem. – sohan nohemy Nov 22 '15 at 16:16
  • @sohannohemy i have two arrays array[i] and newArray[i]. I need to compare the two arrays, in the picture the slots with two values in them, I need to compare those. If the first Array is more than 0.999 bigger than the second array's value, i need to print out the location of that value. So in the shaded areas the program would print, (4,7,9,11,12) – Jon Roy Nov 22 '15 at 16:17

3 Answers3

1

Judging by the picture, you are not placing the values in the correct index in the second array, or you are not matching the correct ones.

If you want it to look exactly like in the picture, the second array should be declared:

double[] newArray = new double[array.length - 2];

And the loop to fill it should be changed to:

    for(j = 2; j < array.length - 2; j++)
    {
        for(int k = -2; k <= 2; k++)
        {
            newArray[j] += array[j+k] / 5;  
        }

    }

This will put the averages in the third, fourth, fifth... elements in newArray. And now you can compare them directly:

    for(int i = 2; i < array.length - 2; i++)
    {
        if(array[i] > (newArray[i] + 0.999))
        {
            count++;
            tempVal = count;
        }

        System.out.println(tempVal);
    }

If you want to save the two unused spaces, as you originally did, rather than responding exactly to the picture, then you should calculate the values as you originally did. But remember to compare each element to the one two places before it and stop 2 places before the end.

Instead of

for(int i = 2; i < array.length; i++)

use

for(int i = 2; i < array.length - 2; i++)

To print the location, your construct with the count and tempVal is unnecessary. You just need to print i+1. Also note that you have a ; after your if. This means it's an empty if, and the block after it is always performed. Never have a ; after an if, for, while etc.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
0

Not clear with what you are asking for in your question but without questioning what's the logic, by just looking at your code:

    for(int i = 2; i < array.length; i++)
    {
        if(array[i] > (newArray[i-2] + 0.999));
        {
            count++;
            tempVal = count;
        }

         System.out.println(tempVal);
    }   
    }

if you relocate the system.out line as follows, I think you will get what you expect as follows:

    for(int i = 2; i < array.length - 2; i++)
    {
        if(array[i] > (newArray[i-2] + 0.999));
        {
            System.out.println(tempVal);
            // count++;
            // tempVal = count;
        }


    }   
    }

PS: Please note that I have also changed the boundary for the loop to stop iteration on 13th member of the array, instead of 15.

Alp
  • 3,027
  • 1
  • 13
  • 28
  • That prints out (4,6,4,7,11,8,10,6,10,13,6) which is the numbers it should be comparing, But it doesnt seem to recognize when the newArray[1-2] is bigger than it. – Jon Roy Nov 22 '15 at 16:36
0

Are you sure you're parsing the numbers correctly? See Java: Reading integers from a file into an array Why don't you print them out after parsing for verification?

btw, this will overflow the index of the 2nd array (since it is created using new double[array.length - 4]):

for(int i = 2; i < array.length; i++)

so does your code run?

Community
  • 1
  • 1
George Birbilis
  • 2,782
  • 2
  • 33
  • 35
  • It is printing the numbers correctly, yes and it does run – Jon Roy Nov 22 '15 at 16:33
  • it doesn't sound logical to me - it should throw an exception since the index on the 2nd array will get out of range: newArray[i-2] when i=array.length-1 (see last for loop) will be newArray[array.length-1-2] aka newArray[array.length-3] but you defined newArray to have array.length-4 elements, so the max index in it should be array.length-5 which is lower than array.length-3 by two positions – George Birbilis Nov 22 '15 at 21:01