0

this is my code, I use lang class. But I want to write a similar program that doesn't contain lang class. Please help me.

import java.lang.*;
import java.io.*;
public class DecimalToBinary {
public static void main(String[] args) throws IOException{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter a Binary number");
    String str=br.readLine();
    long num=Long.parseLong(str);
    long rem;
    while(num>0){
        rem=num%10;
        num=num/10;
        if(rem!=0&&rem!=1){
            System.out.println("This is not a binary number,Please enter again");

        }
    }
    int i=Integer.parseInt(str,2);
    System.out.println("Decimal:"+i);


}

}

how to do the same conversion without using lang class? Please help me!!

4
  • 2
    lang is not a class, it's package, that you should not import - jvm automatically imports it
    – bsiamionau
    Mar 2, 2013 at 17:36
  • If your teacher considers that you should investigate, maybe you should do it. You can, for example, calculate the number using the binary value, on your own. Pretty sure that's what your teacher asked. Mar 2, 2013 at 17:38
  • Do you mean the "java.lang" class (which as @zvzdhk notes is actually a package) or the "java.lang.Long" class? It's hard to avoid the java.lang package because String is there. But you can definitely write this code (with some work) without using Long.
    – user949300
    Mar 2, 2013 at 17:55
  • Do you know in JDK7, a new feature is of binary integer assignment: int i = 0b1101; Mar 3, 2013 at 13:51

3 Answers 3

1

One way to think about this is to split the problem into two.

  1. Testing that the String that was entered represents a binary number. This is pretty straightforward, right? Think about how you might test the characters in the String.
  2. Converting to decimal. This is a bit harder. But, once you are sure that the String is legal binary, the code will be shorter and simpler and easier for you to write and understand.

p.s. the "class" tag you added to your question refers to a Java/C# "class", not school work. I think that "homework" is the correct tag.

0

You do not need to import java.lang.*
Here is the edited code :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DecimalToBinary {

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter a Binary number");
    String str = br.readLine();
    long num = Long.parseLong(str);
    long rem;
    while (num > 0) {
        rem = num % 10;
        num = num / 10;
        if (rem != 0 && rem != 1) {
            System.out.println("This is not a binary number,Please enter again");

        }
    }
    int i = Integer.parseInt(str, 2);
    System.out.println("Decimal:" + i);


}

}

0

I recommend you to walk through the characters of the input from index N - 1 to 0 and check if the current character is valid, if not then stop by throwing an exception. I it is valid, you calculate something like "2 pow (the current position of the binary digit e.g. (N - 1 - i))" and add that to your result. Since this is probably a homework i can only give you these incomplete hints. You should think about it, the rest of the solving is up to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.