i need help i dont know what to do, heres the psudocode and java code

Function (BinaryNumber is String) returns Integer
 Var DecimalNumber is Integer initialized as 0
 Var Power is Integer initialized as 0
 Var BinaryDigit is Integer
 For (I (start value is length of BinaryNumber) to (end value is 1))
  BinaryDigit = StringToInteger (BinaryNumber (I))
- BinaryNumber (I) represents the Ith character in the string – this is similar to the substring function
  DecimalNumber = DecimalNumber + (BinaryDigit * 2 ** Power)
- ** is the “power of” operator
  Power = Power + 1
 End for
 Return DecimalNumber
End Function

The "BinaryToDecimalGood" class.

import java.awt.*;
import hsa.Console;
public class BinaryToDecimalGood
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();
        String BinaryNumber;//return as intiger
        int DecimalNumber = 0;
        int BinaryDigit;
        int Power = 0;

        c.println("What is the binary number you wish to convert:");
        c.println();
        for (int i = binary2.length;i <=1;i ++)
        {
        BinaryDigit = StringToInteger (BinaryNumber (i));
        DecimalNumber = DecimalNumber + (BinaryDigit * 2 ** Power);//** is the "power of" operator
        Power != Power + 1;
        }//end loop
        Return DecimalNumber;

    } // main method
} // BinaryToDecimalGood class

if any1 can help it will be greatly appreated , please keep in mind im in gr 11 and if possible could you keep it a simple as possible thanks

Recommended Answers

All 2 Replies

I haven't even looked at that code, because you haven't really asked a question.

What part of this are you having problems with?

Are you getting errors while compiling? If so, post them.

Are you getting errors while running? If so post them.

Are you seeing results other than what you expect? If so, post what you expect, and what you are getting.

Is there some part of the assignment that you do not understand? If so, ask the teacher, that is what he is there for.

Now, after you have read these questions, and decided you want to post here again, then please, repost the code as well, and this time use code tags. (Use the "Go Advanced" button below, then the "Code" button that you find there.)

[B]import[/B] java.io.BufferedReader;
[B]import[/B] java.io.InputStreamReader;
 
[B]public [/B][B]class[/B] BinaryToDecimal {
 
[B]public[/B] [B]static[/B] [B]void[/B] main(String[] args) {
 
Double DecimalNumber = 0.0;
[B]int[/B] BinaryDigit;
[B]int[/B] Power = 0;
[B]try[/B] {
System.[I]out[/I].println("What is the binary number you wish to convert:");
InputStreamReader in = [B]new[/B] InputStreamReader(System.[I]in[/I]);
BufferedReader br = [B]new[/B] BufferedReader(in);
 
String binary2 = br.readLine();
[B]for[/B] ([B]int[/B] i = binary2.length(); i >= 1; i--) {
String BinaryD = binary2.substring(i - 1, i);
BinaryDigit = Integer.[I]parseInt[/I](BinaryD);
System.[I]out[/I].println("digit to convert : " + BinaryDigit);
DecimalNumber = DecimalNumber
+ (BinaryDigit * (Math.[I]pow[/I](2, Power)));
Power = Power + 1;
}
System.[I]out[/I].println(" The Decimal Number is : " DecimalNumber);
} [B]catch[/B] (Exception e) {
e.printStackTrace();
}
}
}

I modified your code.It takes input as binary number and output equivalent decimal number.

This program can be done through shift operators also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.