Need Java Help

h_abeysinghe

Well-known member
  • Sep 4, 2008
    12,096
    435
    83
    32
    toronto,canada
    machnla mata podi udawak one Java walin(dan Java mathaka nae ban :baffled:)
    anyway meka awith BaseConversion ekak :D

    Binary to Decimal and Decimal to Binary Convert karana :D
    mama danata haduwe Decimal to Binary witharai, dan mata one Binary to Decimal convert karna..... eth mathaka nae ban karana widiya :eek:

    menna code eka
    Code:
    import java.util.*;
    public class BaseConversions
    {
      public static void main (String[] args)
      {
        Scanner input=new Scanner(System.in);
        boolean conv = true;
        String value,binary,decimal1;
        int chosen,decimal,binary1;
        
        do {
          if (conv == true) 
          {
            System.out.println("******************************** ");
            System.out.println("Choose What you want to Convert ");
            System.out.println("1.Decimal to Binary(Eg:- 23232 -> 10101)");
            System.out.println("2.Binary To Decimal (Eg:- 10101 ->23232 ");
            chosen = input.nextInt();
            switch (chosen)
            {
              case 1:
                System.out.println("1.Decimal to Binary(Eg:- 23232 -> 10101)");
                System.out.println("Enter positive integer");
                decimal = input.nextInt();
                if (decimal <= 0)
                  System.out.println("Entered integer is nonpositive.");
                else {
                  binary = "";
                  
                  while (decimal!= 0) 
                  {
                    if (binary.length() % 5 == 0)
                    {
                      binary = " " + binary;
                    }
                    binary = (decimal % 2) + binary;
                    
                    decimal /= 2;
                    
                  }
                  
                  System.out.println("Binary: " + binary);
                }
                  break;
                  
             case 2:
                    System.out.println("2.Binary To Decimal (Eg:- 10101 ->23)");
                    System.out.println("Enter the Binary value you want to convert");
                
               
                    break;
                
                }
                
    
            }
            System.out.println("Do you want to Try again (Y/N): ");
            if (input.next().equalsIgnoreCase("N") )
              conv = false;
            System.out.println("Thank you for using this Program ");
          
       
        }
        while (conv == true);
      }
        
    }
    okata puluwan nam thawa conversions uth dapan.(EG:- Binary to hexa... etc..)

    :yes::yes:
     

    Pink panther

    Well-known member
  • Nov 8, 2011
    23,234
    3,133
    113
    35
    ~ ση тнє ƒℓσω ~
    fsvT.jpeg
     
    • Like
    Reactions: h_abeysinghe

    dmudhitha

    Active member
  • Jan 9, 2009
    658
    49
    28
    Binery to Decomel Converter

    import java.util.Scanner;

    public class BtOD {
    public static void main(String[] args)
    {


    Scanner in=new Scanner(System.in);
    System.out.print("Enter ur Binary No: ");
    String str = in.nextLine();
    int i= Integer.parseInt(str,2);
    System.out.println("Decimal Value:="+ i);
    }
    }
     
    • Like
    Reactions: h_abeysinghe