0

I finally got it in order for smooth compilation, but nothing gets converted

import java.util.Scanner;

    public class nizadi_Lab8
   {
   
   
       public static void main(String[] args)
      {
         if(args.length > 0)
         {
         
            try
            {
               Integer.parseInt(args[0]);
            }
                catch(NumberFormatException f)
               {
                  System.out.println("wrong format");
               }
         }
         
         else
         {
            System.out.println("no arg");
         }
      }
   
       public int  printBin(int n)
      {
      args[0] = n;
      
         if(n < 1)
         {

         }
         else
         {
            System.out.println(printBin(n/2));
         
      
         
      
      System.out.println(printBin(n%2));
      }
      return n%2;
      }  
      
   
   }

Edited by gibbsfan19: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by stevelg
0

This code won't compile at all, firstly try to make it compile, then think of how you define recursion:

  1. base case
  2. recursive case, which decrements value, so recursion terminates at some point
0

Your primary problem here is that you do not call the 'printBin' method from the main class so this procedure is never called! You also use the call Integer.parseint() but do not allocate the result to any variable. These could be overcome using

printBin(Integer.parseInt();

but as this returns an integer it will still need assigning to something or passing to another method that uses the result. Alternatively, you could make your recursive method return void ancd just use to print the results.

Further, this procedure will not print the desired results, although if you can get any output you may be able to sort the rest out yourself.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.