Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java - Converting to Decimal from Binary - Stuck on a Small Error
New Posts  All Forums:Forum Nav:

Java - Converting to Decimal from Binary - Stuck on a Small Error

post #1 of 2
Thread Starter 
I am stuck on a small issue. I wrote a method for converting to decimal from binary, but I can't test to see if it works yet because I get an error when calling on the method and it is the only error. I identified the error as a comment in the code on line 12. Why is the method not working when I call it?

Here is the problem if that helps:

Code:
import java.util.Scanner;

public class BinaryConversion {
        
        public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                String str;
                
                System.out.println("Enter a binary number: ");
                str = input.nextLine();
                
                System.out.println("Conversion to decimal: " + binaryToDecimal(power)); // power cannot be resolved
        }

        public static int binaryToDecimal(String binaryString) {
                double power;
                
                power = 0;
                
                for(int i = 0; i < binaryString.length(); i++) {
                        if (binaryString.charAt(i) == '1') {
                                power = power + Math.pow(2, binaryString.length() - 1 - i); 
                        }
                }
                
                return (int)power;
        }
        
}
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
My System
(15 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k ASUS P8P67 Deluxe EVGA GTX 570 SC G.SKILL Ripjaws X 8GB 
Hard DriveHard DriveCoolingOS
Crucial M4 Samsung Spinpoint F3 Prolimatech Megahalems Windows 7 Professional 
MonitorKeyboardPowerCase
Samsung P2450H 24" Ducky Shine Blue LED w/ Cherry MX Reds Corsair HX850 FT02B-W 
Mouse
Razer Death Adder 
  hide details  
Reply
post #2 of 2
Quote:
Originally Posted by Heat Miser View Post

I am stuck on a small issue. I wrote a method for converting to decimal from binary, but I can't test to see if it works yet because I get an error when calling on the method and it is the only error. I identified the error as a comment in the code on line 12. Why is the method not working when I call it?

The problem is that the variable "power" does not exist in the main scope. It only exists in the function-scope.
The input from the console is stored in the "str" variable, so you need to pass that to the function.

Changing the line where the function is called to the following makes it work.
Code:
System.out.println("Conversion to decimal: " + binaryToDecimal(str));
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java - Converting to Decimal from Binary - Stuck on a Small Error