+ 2

How to fix error "void type not allowed here" in java

import java.util.Scanner; //введите код сюда public class Converter{ public static void toBinary(int x) { String binary = ""; while (x > 2 ){ binary = (x%2) + binary; x /= 2; } } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinary(x)); } }

22nd Jul 2022, 5:44 PM
Mihail Jimi
Mihail Jimi - avatar
4 Antworten
+ 3
Your toBinary() method is void type but you expecting to print returned value in System.out.println( Convertor.toBinary(x)) ; // since called method is void, does not return anything... Either return value or just call method without print statement...
22nd Jul 2022, 6:04 PM
Jayakrishna 🇮🇳
+ 2
thx
22nd Jul 2022, 6:15 PM
Mihail Jimi
Mihail Jimi - avatar
+ 1
edit: you're welcome.
22nd Jul 2022, 7:34 PM
Jayakrishna 🇮🇳
+ 1
Make your toBinary method return type string and return binary string.
22nd Jul 2022, 7:38 PM
Jayakrishna 🇮🇳