0

Why 1st one is not compile but 2nd one is working ?

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner kb=new Scanner(System.in); double dollar=71.58*new Scanner(System.in).nextDouble(); double pesos=3.85*new Scanner(System in).nextDouble (); System.out.println(dollar<pesos ? "dollar" : "Pesos"); } } //_----------------------------------- import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner kb=new Scanner(System.in); double dollar=71.58*kb.nextDouble(); double pesos=3.85*kb.nextDouble (); System.out.println(dollar<pesos ? "dollar" : "Pesos"); } }

19th Feb 2020, 6:56 PM
Shubham
Shubham - avatar
5 odpowiedzi
0
I think it has something to do with the fact that the code playground does not allow multiple scanners. It is more a sololearn issue. Both codes should work using another ide.
19th Feb 2020, 7:12 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
You forget dot after System in line #7
19th Feb 2020, 7:19 PM
andriy kan
andriy kan - avatar
+ 1
@Shubham Ok Then fix your first part code in the post, since without a dot after System (on line # 7) it will not compile in any way. The code (if dot exists) should compile as it is valid. But it can not work, because you create Scanner with System.in several times. First Scanner may not be destroyed, so it will not close its handle ( System.in). In that case the next Scanner will not get that handle at the moment of creation
20th Feb 2020, 4:31 AM
andriy kan
andriy kan - avatar
0
Though both should work fine but I don't see the need of the reference variable 'kb' of type Scanner in the 1st code.
19th Feb 2020, 7:19 PM
Avinesh
Avinesh - avatar
0
I didn't forget when I wrote so I used to write System.in
20th Feb 2020, 3:02 AM
Shubham
Shubham - avatar