File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Decimal to binary converter help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Decimal to binary converter help" Watch "Decimal to binary converter help" New topic
Author

Decimal to binary converter help

Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Hey guys, trying to write a program that takes a user inputted number and converts it to a binary number.

Here's what I have:



Bugs on:

Line 13

Line 17

Line 23
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 190

You are attempting to access 'binary' as an array but it wasn't defined that way.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

How can I go about fixing that?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Your printBinaryFormat method is expecting an int parameter to be passed to it. I suspect that that is the int you are reading from the scanner.
So I would say that line 7 and 10 should actually be in your main method and binary should be passed as a parameter to printBinaryFormat.

You will then need to declare an int array in your printBinaryFormat method, but you'll need to work out what size it needs to be somehow.
You might be better off just appending the digits to a StringBuilder - that way you don't need to know how long the binary value is going to be.


Joanne
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

I'm not exactly sure how to go about doing that. A friend of mine asked me to change this code sequence so that it would accept user input rather than the stated number. Kind of new at this

Here is the original from:

http://www.java2novice.com/java-interview-programs/decimal-to-binary/

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Which bit don't you understand ?
You already know how to get an int value from the Scanner object - I'm just saying you're doing it in the wrong place. If you do it in the main method you can then pass the value to the printBinaryFormat method.
In the original code (line 19), 25 is being passed to the method, so you just need to replace the 25 with the value you got from the Scanner.
If you leave the rest of the code as it was originally it will work.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Like this?

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:Like this?

Why have you put the [] after the method name ?
I said to replace the 25 with the value returned from the Scanner.
You can either do this directly by replacing it with console.nextInt() or you can move line 7 of your original post into the main method and replace the 25 with the binary variable.

The printBinaryFormat method should not change at all from the code that you copied.

If none of this is making sense then you might want to take a look at this tutorial section on method parameters.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Do you mind showing me? I feeling like I'm just gutting this and digging a deeper hole.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:Do you mind showing me? I feeling like I'm just gutting this and digging a deeper hole.

Which is why you need to read the tutorial I linked to.
There are examples in there of how to pass variable values to a method - if you can make use of those examples to work out how to fix your problem, you will have learnt a lot more than you will by me just giving you the answer.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

See that's the problem: I did read through it, and if anything, it made me feel more overwhelmed. It's hard for me to connect the tutorial to what I'm doing because for one, I barely understand what I'm doing and the tutorial's context is separate from my issue, so I can't bridge the two mentally. I'm just struggling to follow this concept :/
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16

This is one of the examples from the tutorial. It passes an int variable to a method that is declared to take a single int parameter - identical to what you are trying to do.
Ignore lines 11 - 13 and the contents of the called method for the moment - the only parts you are interested in are the passing of the parameter.

On line 3 it assigns a value to a variable - that is what you do on line 10 of your original post. You just need to move that line into the main method.
On line 9 it passes the variable as a parameter to the method.

I'm not deliberately being awkward - assigning values to variables and passing values to methods is something you will do a lot of when writing code, so it is better that you understand it now rather than just be given an answer and then have to ask the same question again the next time you come across something similar.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Can you show me how to write those lines of code then? I swapped the term number with binary and only got more bugs. I can't put these ideas into code, I keep looking at it feeling fried.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:Can you show me how to write those lines of code then?

I think you know the answer to that question.

The advice I have given you so far is
1. Return the content of the printBinaryFormat method to exactly how it was on the website you copied it from.
2. Line 10 from your original post needs to be moved into the main method.
3. Pass the binary variable to the printBinaryFormat method.

For the moment just do steps 1 and 2. It's just cutting and pasting. When you've done that post your new code.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Only bugs on lines 13 and 17.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:Only bugs on lines 13 and 17.

Is your printBinaryFormat method exactly the same as the original ?
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

When I change the printbinary at the very bottom back to 25, it gets messed up because I need that value to be input by the user, so I put it as "binary", since binary was defined by the user
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:When I change the printbinary at the very bottom back to 25, it gets messed up because I need that value to be input by the user, so I put it as "binary", since binary was defined by the user

I was talking about the code in the method, not the call to the method.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Here is original:



And here is what I have so far:

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
So back to my earlier question
Joanne Neal wrote:Is your printBinaryFormat method exactly the same as the original ?
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Yes

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:Yes


That's the method signature. I am talking about the whole method (lines 5 - 15 in the original code and lines 9 - 19 in your code)
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

So like this?



It's clean of bugs but won't run
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:So like this?

Assuming it works then the only other change I would suggest is to make your console variable a non-static variable local to the main method. Although it won't be a problem in a simple program like this, you need to understand the difference between static and non-static variables, so you might want to study that at some point.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:It's clean of bugs but won't run

What do you mean by 'won't run' ?
What command line are you using and what happens when you run it ?
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

How would I correct that? Please it's 0422 right now and I have to get some sleep at some point. I just need this done.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:How would I correct that? Please it's 0422 right now and I have to get some sleep at some point. I just need this done.

Assuming that's referring to my comments about your console variable in my first post, then, as I said, you don't need to for a simple program like this.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Im su\using Eclipse IDE. There are no bugs. i hit run and nothing happens.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Dan Good wrote:Im su\using Eclipse IDE. There are no bugs. i hit run and nothing happens.

Do you type anything ? Remember that the whole point of this exercise was to change the program to accept user input, so you need to actually give the program some input.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

The console for me to type in does not pop up. Literally nothing happens. First time I have ever seen this. Doesnt say error or anything. Does it work for you?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
If the Console doesn't show automatically, you need to click on the Window | Show View | Console menu item to open it.
Dan Good
Ranch Hand

Joined: Feb 02, 2014
Posts: 49

Thank god. it works now. I both hate you and love you for sticking this out with me for the past 5 hours. You just got this Marine his next rank for bootcamp in 20 days. I needed to pass this last class so I could have enough credits to rank up for the summer. PFC. Thank you!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

Dan Good wrote:Thank god. it works now. I both hate you and love you for sticking this out with me for the past 5 hours. You just got this Marine his next rank for bootcamp in 20 days. I needed to pass this last class so I could have enough credits to rank up for the summer. PFC.

Could I make a suggestion then? If you're a Marine, then you'll know that successful operations are almost always the result of good planning.

Programs don't just write themselves, they take thought - and lots of it.

So my advice: Next time, give yourself plenty of time; and if you DO run into problems, don't leave it to the last minute to ask for help.
Remember, we're all volunteers here.

You may also find the StopCoding (←click→) and WhatNotHow pages worth reading.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Decimal to binary converter help