Results 1 to 10 of 10

Thread: Help!!!!!

  1. #1
    ErikD99 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default Help!!!!!

    I am trying to make a program that takes a binary number that the user inputs, and converts it to an int. then it displays it to the user. this is the code i have so far.(it compiles but it doesnt calculate the answer. can anyone help me??

    Java Code:
    import java.util.Scanner;
    
    public class DanielsonErikA2Q2{
    
    	public static void main (String []args){
    
    		String binaryInput;
    		int binaryNumber;
    		int lastdigit;
    		int runningTotal =0;
    		int product;
    		int preproduct;
    		int calculation;
    		int result;
    		int number;
    
    		Scanner keyboard = new Scanner(System.in);
    
    		System.out.println("Please enter the binary number you would like to convert to a decimal.");
    
    		binaryNumber= keyboard.nextInt();
    
    		while(binaryNumber!=-1){
    
    		while(binaryNumber>0){
    
    			for(int i=1; i>=100; i++)
    			{
    							
    							result= (int)Math.pow(2, i);
    
    							number= binaryNumber%10;
    
    							preproduct=  number * result;
    
    							product = number * preproduct;
    
    							runningTotal = runningTotal + product;
    			}
    							System.out.println("The Number as a decimal is " + runningTotal);
    
    
    
    		}
    		}
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    it doesnt calculate the answer

    Do what *does* the program do when you run it?

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Is this for a class assignment? Because there is a much easier way to do this using the parseInt method. Here is an example that you could apply:

    Java Code:
    Integer.parseInt("1001",2);

    It uses a string for the binary number represented. So you could accomplish your task with about 4 lines of code.
    Last edited by joshdgreen; 10-20-2010 at 09:08 PM.
    Sincerely, Joshua Green
    Please REP if I help :)

  4. #4
    ErikD99 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    It asks for input, then when you put in an answer, it prints out a neverending looop of "the number as a decimal is 0." it doesnt seem to be doing the calculations.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    it prints out a neverending looop of "the number as a decimal is 0."

    OK! Now we have something...

    The inner while loop (that's the one that most closely contains the output message) begins:

    Java Code:
    while(binaryNumber>0){

    Now the thing is that binaryNumber never changes its value within that loop. SO the loop does whatever it does forever.

    You will have to change the logic within that loop.

    -----

    Following on from joshdgreen's point: I'm assuming this is an assignment. But do you have to read the user input as an int? Because, really it is not an int: it's a string. Numeric values (like int) don't have a base: numerals (String) do. Even if you don't use the library parse methods you may still find things easier if you deal with the user input as a String and deal with it character by character.

  6. #6
    ErikD99 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    So with the parse int method, could the program calculate any random length number that the user inputs?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Read the API docs for Integer and the BigInteger classes.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ErikD99 View Post
    So with the parse int method, could the program calculate any random length number that the user inputs?
    Nope as it has to be a "parsable" integer String, one that represents a valid Integer. If it's greater than Integer.MAX_VALUE or less than Integer.MIN_VALUE, it will throw a NumberFormatException as noted in the Integer API.

    Luck!

    [edit: oops, as PBrockway links to above]

  9. #9
    ErikD99 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    what would you change the logic of the second while loop then? :s

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by ErikD99 View Post
    what would you change the logic of the second while loop then? :s

    I suppose I would be asking myself what I expect or want the value of binaryNumber to be on the second, third, fourth etc time around the loop. Then after I had used it to calculate runningTotal I would update it so that it had the correct value for the next time around the loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •