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 4 of 4

Thread: Beginner needs help with simple java assignment.

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Beginner needs help with simple java assignment.

    I'm trying to implement a program that reads binary numbers from the user and returns the equivalent in the normal (decimal) numeral system.

    This is what I've come up with so far:

     import java.util.Scanner;
    public class BinaryConverter {
     
    		public static void main(String[] args) {
    		int binary;
    		Scanner scan = new Scanner (System.in);
    		System.out.print("Enter a binary number (0 to quit): ");
    		binary = scan.nextInt();	
    		String binaryString = Integer.toString(binary);
    		while (binary != 0)
    			System.out.println("The binary number " +binaryString +" corresponds to " +toDecimal(binaryString)+" in the decimal numeral system.");
     
     
    }
     
    public static double toDecimal(String binaryString)
    {
    	double decValue = 0;
    	int i = 0;
    	for(i=0; i < binaryString.length(); i++)
    		if (binaryString.charAt(binaryString.length() - i) == 1){
    			decValue = Math.pow(2, binaryString.length()- i );
    		}
    return decValue;
     
    }

    My program fails to carry out this task.
    What can I do to ensure that the variable decValue summarizes the result from "decValue = Math.pow(2, binaryString.length()- i " each and every time the for-loop is runned through and invokes that code?

    I also get a string index out of bound error, but I can't see where the program errs.

    Any help is appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Beginner needs help with simple java assignment.

    You reset the decValue every time though the loop, rather than incrementing it:

    decValue [COLOR="Red"]+=[/COLOR] Math.pow(2, binaryString.length()- i );

    Also, inspect your loops closely. You are getting exceptions because your loop is written in such a way as to try and access an array element that is not within its bounds.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner needs help with simple java assignment.

    Ok, I've made som necessary changes to the program.

    Code:

     import java.util.Scanner;
    public class BinaryConverter {
     
    		public static void main(String[] args) {
    		int binary = 2;
    		Scanner scan = new Scanner (System.in);
    		String binaryString = Integer.toString(binary);
    		while (binary != 0){
    			System.out.print("Enter a binary number (0 to quit): ");
    			binary = scan.nextInt();
    			System.out.println("The binary number " +binary +" corresponds to: " +toDecimal(binaryString));
    		}
     
    }
     
    		public static double toDecimal(String binaryString) {
    		    double decValue = 0;
    		    for (int i=0; i < binaryString.length(); i++)
    		        if (binaryString.charAt(i) == '1')
    		            decValue += Math.pow(2, binaryString.length() - 1 - i);
    		    return decValue;
    		}  
     
     
    }

    I have two problems, firstly, the program will just return 0.0 from any input. Secondly, the program doesn't quit when the user enters zero. The latter is a minor problem, but advice on both problems are highly appreciated.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Beginner needs help with simple java assignment.

    Instead of using
    binary = scan.nextInt()
    , use
    binaryString = scan.next()
    . This will get you the necessary binaryString to convert to decimal. It's also unnecessary to have the binary variable. toDecimal is already returning the correct decimal value, so print this out:

    System.out.println("The binary number " + binaryString + " corresponds to: " + toDecimal(binaryString));

    To fix the check in the while loop, all you need to change it to is this:
    while (toDecimal(binaryString) != 0)

    I would also change the return type of toDecimal to a either int or long because there's no need to handle binary decimal numbers (floating point number conversion is a whole new can of worms ). This isn't really necessary, but something that I would recommend (choose your variable types to fit the purpose).

Similar Threads

  1. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM
  2. I need extensive help with my java assignment
    By MoshMoakes in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 12th, 2009, 07:44 PM
  3. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM
  4. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM
  5. [SOLVED] Problem with Grading calculator in java program
    By Peetah05 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: March 28th, 2009, 04:25 PM