+ 1

Can someone please tell me what's wrong with my code?

I'm trying to make a more advanced time converter in java that converts years into months, days, hours, minutes, and seconds. This is my code: import java.util.Scanner; public class Program { public static void main(String[] args) { String m="Months:"; String w="Weeks:"; String d="Days:"; String h="Hours:"; String min="Minutes:"; String s="Seconds:"; Scanner years = new Scanner(System.in); System.out.println(m); System.out.println(years.nextInt()*12); System.out.println(w); System.out.println(years.nextInt()*52); System.out.println(d); System.out.println(years.nextInt()*365); System.out.println(h); System.out.println(years.nextInt()*365*24); System.out.println(min); System.out.println(years.nextInt()*365*24*60); System.out.println(s); System.out.println(years.nextInt()*365*24*60*60); } }

22nd Mar 2021, 3:54 PM
Ava
Ava - avatar
3 Respuestas
+ 4
Ava , save your input as variable. Otherwise you should enter 6 values in your code which can be different. If I understand the purpose you ask the user to enter the years and then convert it. Look at the code. Hope it helps you. https://code.sololearn.com/c0V0jY0v4IGo/?ref=app
22nd Mar 2021, 4:15 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Thank you so much! I tried to use variables but I guess I did it wrong and it didn't work so thanks!
22nd Mar 2021, 4:22 PM
Ava
Ava - avatar
+ 1
Try to instance your scanner object separetly ex: Scanner scanner = new Scanner(System.in); Then u can take the user input like this : Int years = scanner.nextInt(); To print result of convert to months for example : System.out.println(years * 12); And so on ...
22nd Mar 2021, 11:41 PM
‎Abdul Jalil
‎Abdul Jalil - avatar