what's wrong with my code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what's wrong with my code ?

import java.util.scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner (System.in); System.out.println("donner votre age"); int age = sc.nextInt(); if (age < 15){ System.out.println("mineur"); } else { System.out.println("majeur"); } } }

5th Oct 2016, 5:34 PM
Yassine Iguider
Yassine Iguider - avatar
13 Answers
+ 3
The scanner on line 1 needs to be capitalized. I just ran the code after fixing that and it seems to be working fine. If you are not already I would suggest coding on a program that shows which line the error is on. I use eclipse and I find it very helpful.
6th Oct 2016, 3:53 AM
Parker L
Parker L - avatar
+ 1
Code is correct. I have tried. Only in first line a little wrong. import java.util.Scanner; needed.
6th Oct 2016, 10:38 AM
HawkEye
HawkEye - avatar
+ 1
The yellow is just telling you that the scanner is not closed. The same thing shows up before you use variable. Its just a warning.
6th Oct 2016, 11:43 PM
Parker L
Parker L - avatar
+ 1
thanks @parker L
7th Oct 2016, 12:39 AM
Yassine Iguider
Yassine Iguider - avatar
0
thanks but it didn't solve my problem .
5th Oct 2016, 7:02 PM
Yassine Iguider
Yassine Iguider - avatar
0
What does your compiler say the problem is?
5th Oct 2016, 7:30 PM
Ousmane Diaw
0
@Ousmane the compiler give this : donner votre age majeur
5th Oct 2016, 8:51 PM
Yassine Iguider
Yassine Iguider - avatar
0
i fixed the problem by deleting the : System.out.println("donner votre age"); but what if i want to keep that line of code too ? i think im missing something !
5th Oct 2016, 9:22 PM
Yassine Iguider
Yassine Iguider - avatar
0
it seem to be running perfectly in eclipse but one thing i noticed with sc in the first scanner line eclipse mention sc in yellow smthing wrong with it !
6th Oct 2016, 11:24 AM
Yassine Iguider
Yassine Iguider - avatar
0
Np Glad to help
7th Oct 2016, 12:49 AM
Parker L
Parker L - avatar
- 1
hi Yassie, use sc.close(); before if loop. This will close the scanner.
5th Oct 2016, 7:39 PM
devesh
devesh - avatar
- 2
There is a space before you passed in System.in. So replace it with this Scanner sc = new Scanner(System.in);
5th Oct 2016, 6:59 PM
Ousmane Diaw
- 2
i may be wrong, but try to return age after your else ex: return age; i think that might work, just try it and let me know if im right or not
5th Oct 2016, 7:33 PM
Aquarius
Aquarius - avatar