jyothi
jyothi

Reputation: 1

My Java code is showing a compiler error when I create method

package aj;

import java.util.Scanner;

public class ConvertingNumber {

    public static void main(String[] args) {

        Scanner a = new Scanner(System.in);
        System.out.println("Enter the Number");
        int num = a.nextInt();
        System.out.println("Enter the base for the given number");
        int base = a.nextInt();
        converting(num,base);

        public static int converting(int num , int base) {

            String sum="";

            while(num > 0) {
                int rem = 0;
                rem = num % base;
                num = num / base;
                sum = rem + sum;
            }

            System.out.println(sum);
        }   
    }
}

for my above java code,am getting compier error saying:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method converting(int, int) is undefined for the type ConvertingNumber
void is an invalid type for the variable converting
Syntax error on token "(", ; expected
Duplicate local variable num
Syntax error on token ",", ; expected
Duplicate local variable base
Syntax error on token ")", ; expected
at aj.ConvertingNumber.main(ConvertingNumber.java:12)

Please anyone help me out in solving this. Thanks in advance.

Upvotes: 0

Views: 604

Answers (2)

chalitha geekiyanage
chalitha geekiyanage

Reputation: 6812

converting method should be out side from the main method

package aj;

import java.util.Scanner;

public class ConvertingNumber {
    public static void main(String[] args) {
        Scanner a=new Scanner(System.in);
        System.out.println("Enter  the Number");
        int num=a.nextInt();
        System.out.println("enter the base for the given number");
        int base=a.nextInt();
        converting(num,base);
    }

    //This method should be out side the main method
    public static void converting(int num , int base) {
        String sum="";
        while(num>0) {
            int rem=0;
            rem=num%base;
            num=num/base;
            sum=rem+sum;
        }
        System.out.println(sum);

    }   

}

Upvotes: 3

Anshul
Anshul

Reputation: 103

I have Corrected Your code..

package aj;

import java.util.Scanner;

public class ConvertingNumber  {
public static void main(String[] args) {
    Scanner a=new Scanner(System.in);
    System.out.println("Enter  the Number");
    int num=a.nextInt();
    System.out.println("enter the base for the given number");
    int base=a.nextInt();
    ConvertingNumber .converting(num,base); 
           //converting(num,base);

}

    public static int converting(int num , int base)
    {
        int sum=0;
        while(num>0)
        {
            int rem=0;
            rem=num%base;
            num=num/base;
            sum=rem+sum;
        }
        //System.out.println(sum);
        return sum;
        } 

}

Upvotes: 0

Related Questions