package com.codegym.task.task07.task0721;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Min and max in arrays
Create an array of integers (int[]) with 20 elements.
2. Read 20 integers from the keyboard and add them to the array.
3. Find the maximum and minimum integers, and then display them separated by a space.
4. Use a for loop.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] n=new int[20];
for(int i=0;i<20;i++)
{
n[i]=Integer.parseInt(br.readLine());
}
int maximum=0;
int minimum=0;
for(int i=0;i<20;i++){
if(n[i]>n[i+1]){
maximum=n[i];
}
if(n[i]<n[i+1])
minimum=n[i];
}
//write your code here
System.out.print(maximum + " " + minimum);
}
}
Also while I am trying to run without execution I am getting following :
java.lang.NumberFormatException:
Solution.java, method main, line: 22
Error converting a string to a number (invalid format).
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at com.javarush.task.task07.task0721.Solution.main(Solution.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.javarush.task.common.model.execution.ExecuteService.callMainMethod(ExecuteService.java:78)
at com.javarush.task.common.model.execution.ExecuteService.execute(ExecuteService.java:102)
at com.javarush.task.app.execution.ExecutionService.execute(ExecutionService.java:107)
at com.javarush.task.app.execution.ExecutionService.main(ExecutionService.java:244)
at com.javarush.task.app.MainApplication.main(MainApplication.java:49)
package com.codegym.task.task07.task0721;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Min and max in arrays
Create an array of integers (int[]) with 20 elements.
2. Read 20 integers from the keyboard and add them to the array.
3. Find the maximum and minimum integers, and then display them separated by a space.
4. Use a for loop.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] n=new int[20];
for(int i=0;i<20;i++)
{
n[i]=Integer.parseInt(br.readLine());
}
int maximum=0;
int minimum=0;
for(int i=0;i<20;i++){
if(n[i]>n[i+1]){
maximum=n[i];
}
if(n[i]<n[i+1])
minimum=n[i];
}
//write your code here
System.out.print(maximum + " " + minimum);
}
}