Whats wrong with my code? It runs well in NetBeans but here it gives me a lot of errors. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Whats wrong with my code? It runs well in NetBeans but here it gives me a lot of errors.

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner x = new Scanner(System.in); int y = x.nextInt(); int a=0; Scanner z = new Scanner(System.in); for(int i=1;i<=y;i++) { int b = z.nextInt(); if(b%2==0){ a+=b; } } System.out.print(a); } }

11th Mar 2020, 8:27 PM
Nicolae Dubenco
Nicolae Dubenco - avatar
1 Answer
+ 3
There is no need to take 2 Scanner objects. You can use fisrt object throughout the function... Edit: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner x = new Scanner(System.in); int y = x.nextInt(); int a=0; // Scanner z = new Scanner(System.in); for(int i=1;i<=y;i++) { int b = x.nextInt(); //z changed x if(b%2==0){ a+=b; } } System.out.print(a); } }
11th Mar 2020, 8:30 PM
Jayakrishna 🇮🇳