• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Decimal to binary converter help

 
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are attempting to access 'binary' as an array but it wasn't defined that way.
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I go about fixing that?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like this?

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mind showing me? I feeling like I'm just gutting this and digging a deeper hole.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only bugs on lines 13 and 17.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan Good wrote:Only bugs on lines 13 and 17.


Is your printBinaryFormat method exactly the same as the original ?
 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is original:



And here is what I have so far:

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So back to my earlier question

Joanne Neal wrote:Is your printBinaryFormat method exactly the same as the original ?

 
Dan Good
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So like this?



It's clean of bugs but won't run
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im su\using Eclipse IDE. There are no bugs. i hit run and nothing happens.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are forgetting the compile‑time error in that link, are we? It also has some less than ideal style, variable names, for example.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be easier to use a StringBuilder instead of an int array for the binary variable. Then use to prepend the correct character, and finally just print binaty.toString() after the loop exits. This also makes it simpler if you want to use longs instead of ints, so you don't have to guess how big the array needs to be.
 
reply
    Bookmark Topic Watch Topic
  • New Topic