-3

.txt file have these values:

10

40

30

5

18

This code is meant to read the numbers in a .txt file, then saves in to an array. Then use a method to find the biggest number and return the value. I'm getting an error when running it. What did I do wrong?

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

public class MinOppgave4 {
public static void main(String[]args)throws Exception{

    String fileName = "MinOppgave4tekst.txt";
    File mineFile = new File(fileName);
    Scanner in = new Scanner(mineFile);
    int[] num = new int [5];

    for(int i=0; i<num.length; i++)
    {
        num[i]=in.nextInt();
        System.out.println(num[i]);
    }

    System.out.println("The biggest number is: "+GetTheBiggestNum(num));
}


public static int GetTheBiggestNum(int[] num)
{
    int biggestNum = 0;
    if ((num[0] > num[1]) && (num[0] > num[2]) && (num[0] > num[3]) && (num[0] > num[4]) && (num[0] > num[5]))
    {
        biggestNum = num[0];
    }
    else if ((num[1] > num[0]) && (num[1] > num[2]) && (num[1] > num[3]) && (num[1] > num[4]) && (num[1] > num[5]))
    {
        biggestNum = num[1];
    }
    else if ((num[2] > num[0]) && (num[2] > num[1]) && (num[2] > num[3]) && (num[2] > num[4]) && (num[2] > num[5]))
    {
        biggestNum = num[2];
    }
    else if ((num[3] > num[0]) && (num[3] > num[1]) && (num[3] > num[2]) && (num[3] > num[4]) && (num[3] > num[5]))
    {
        biggestNum = num[3];
    }
    else if ((num[4] > num[0]) && (num[4] > num[1]) && (num[4] > num[2]) && (num[4] > num[3]) && (num[4] > num[5]))
    {
        biggestNum = num[4];
    }
    else
    {
        biggestNum = num[5];
    }
    return biggestNum;
}

}
Candy
  • 13
  • 3
  • 1
    Why not just debug this simple program? It will take less time and you will learn much more – nogard Sep 23 '14 at 08:20
  • Have you read about the java `List` class, and `Collections.sort()` method? – vikingsteve Sep 23 '14 at 08:22
  • Apart from the *in-built* `Arrays.sort()[arr.length-1]`, there are other *easy* ways of finding the biggest element. Look them up on google. – TheLostMind Sep 23 '14 at 08:22
  • When you are trying to get maximum there are more efficent ways: http://stackoverflow.com/questions/1484347/java-max-min-value-in-an-array – Tomek Sep 23 '14 at 08:24
  • I have to do a return vale from a method. That's what the task says. – Candy Sep 23 '14 at 08:26

4 Answers4

1

Your code references num[5] , however you create an array of 5 values, which will be num[0] to num[4]. So you go out of bounds on your array.

One common way to find the biggest element is to sort your array, and then picking the first (or last) item in that sorted array.

Java also has way of doing this with Collections.max , you can simply do:

 int biggest = Collections.max(Arrays.asList(num))

However, if you want to do it yourself, a much better way than your current approach is to loop over your array and find the biggest value like so:

public static int GetTheBiggestNum(int[] num) {
   if (num.length == 0) {
      throw new IllegalArgumentException("Array cannot empty")
   }

   int biggest = num[0]; 
   for (int i = 1; i < num.length; i++) {
      if (num[i] > biggest)
         biggest = num[i];
   }
   return biggest;
}
nos
  • 223,662
  • 58
  • 417
  • 506
0

Change your loop to this. Highest will be the highest number.

int highest = 0;
for(int i=0; i<num.length; i++)
{
     num[i]=in.nextInt();
     System.out.println(num[i]);
     if(highest < num[i]){
        highest = num[i];
    }
}

If you change your loop to this, you don't even need that other method.

Vincent Beltman
  • 2,064
  • 13
  • 27
0

Your array's size => 5. Can access only 0 to 4 for the array with length 5.

But in your code, accessing num[5], it will throws ArrayIndexOutofBoundsException.

One thing is why not google for finding maximum number in a array. E.g this SO

Community
  • 1
  • 1
Wundwin Born
  • 3,467
  • 19
  • 37
0

There should be ArrayIndexOutOfBoundsException because you have 5 element in num array,and you are trying to access num[5] inside method GetTheBiggestNum ,which does not exist.You could have used other better ways to get max element from the integer array.

dReAmEr
  • 6,986
  • 7
  • 36
  • 63