My code looks correct, compile and return correct result, but doesn't pass on server. What is wrong with it?

enter code here
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/* Maximum of an array of 20 numbers
1. In the method initializeArray():
    1.1. Create an array of 20 numbers
    1.2. Read from the keyboard 20 numbers and fill the array with them
2. The method max(int[] array) should find the maximum number of the elements of the        array
*/

public class Solution
 {
public static void main(String[] args) throws Exception
{
    int[] array = initializeArray();
    int max = max(array);
    System.out.println(max);
}
private static int[] initializeArray() throws IOException {
    //Initialize (create and fill) an array here
    BufferedReader reader  = new BufferedReader(new InputStreamReader(System.in));
    int[] list = new int[20];
    for (int i = 0; i < list.length; i++){
        list[i] = Integer.parseInt(reader.readLine());
    }
    return list;
}

private static int max(int[] array) {
    //Find the max value here
    int maximum = array[0];
    for (int i = 1; i < array.length; i++){
        if (array[i] > maximum){
            maximum = array[i];
        }
    }
    return maximum;
}

}

asked 27 Mar '16, 02:02

inzheneher's gravatar image

inzheneher
113
accept rate: 0%

edited 27 Mar '16, 02:06


It looks fine. My code passed the tests. Maybe try tailor-made for construction for nested iteration in max method:

for (int number : array)
link

answered 13 Apr '17, 09:00

noelo's gravatar image

noelo
102
accept rate: 0%

Error on line 34, you have lost one element of array with index 0. It should be for "int i = 0;" insted of for "int i = 1".

link

answered 26 Sep, 10:24

GreenMile's gravatar image

GreenMile
112
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×31
×8
×5

Asked: 27 Mar '16, 02:02

Seen: 1,270 times

Last updated: 26 Sep, 10:24