Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why wont this method work?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why wont this method work?

    I'm trying to create a method that takes a binary number as a string parameter and then prints out the decimal equivalent. For some reason my code isn't working but I feel like I am extremely close!
    import java.util.Scanner;
     
    public class Question1 {
     
    public static void main(String[] args) {
     
    Scanner input = new Scanner(System.in);
     
    System.out.println(" Enter a binary number ");
     
    String n= input.nextLine();
     
    int hey = binaryToDecimal(n) ;
     
    System.out.println(hey);
     
    }
     
    public static int binaryToDecimal(String num) {
     
    char number ;
     
    int n;
     
    int x = 0;
     
    int m=0;
     
    for(int i=0;i<=num.length()-1;i++) { 
     
     
    number=num.charAt(i);
     
    n = Character.getNumericValue(number);
     
    m = num.length()-1;
     
     
     
    x +=n*Math.pow(2,m);
     
    m--;
     
     
     
     
     
     
    }
     
    return x;
     
     
     
     
     
     
    }
     
     
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Why wont this method work?

    The code is quite difficult to follow. Perhaps you could edit your post to remove redundant empty lines and correct the indentation.

    What is m supposed to represent in the binaryToDecimal() method? (In general it is a good idea to make variables as descriptive as possible) And what value should m have each time around the loop?

Similar Threads

  1. Replies: 1
    Last Post: February 27th, 2013, 02:32 AM
  2. Double.valueof wont work for formated string
    By sstavrou in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 8th, 2013, 01:35 PM
  3. File IO Compiles, but wont work?
    By StarKannon in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 2nd, 2011, 09:05 AM
  4. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 02:58 PM
  5. [SOLVED] Simple Grahpic porgram wont work.
    By kogo50 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2010, 12:46 PM