![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | ||
|
Newbie
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0
![]() |
Decimal to binary help
I'm trying to code a program in java which converts decimal to binary but i keep getting this error upon compile:
Quote:
Quote:
Thanks
__________________
biggest NOOB ever :P |
||
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 204
Rep Power: 2
![]() |
Re: Decimal to binary help
Change your to this and try it out.
import java.util.Scanner; //this program uses class scanner
public class Q1
{
public static String binToDec(int b)
{
return Integer.toBinaryString(b);
}
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the decimal number you want to convert to binary");
int b = input.nextInt();
System.out.println(binToDec(b));
}
}Don't worry I'm not telling you off, just giving a few pointers.
__________________
SYNTAX ERROR ... |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 204
Rep Power: 2
![]() |
Re: Decimal to binary help
Also, your scanner had to be nextInt to get an integer from the user. You had a string wanting to be an integer and then a string a again.
__________________
SYNTAX ERROR ... |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0
![]() |
Re: Decimal to binary help
Thanks for the tips. It works!
If i wanted to go about making this a recursive solution, how would i do it?
__________________
biggest NOOB ever :P |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 204
Rep Power: 2
![]() |
Re: Decimal to binary help
You would have to make a class of it. For example:
public class Binary
{
public static String binToDec(int b)
{
return Integer.toBinaryString(b);
}
}
__________________
SYNTAX ERROR ... |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 204
Rep Power: 2
![]() |
Re: Decimal to binary help
Sorry, I can't go into too much detail, too sleepy...
![]()
__________________
SYNTAX ERROR ... |
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0
![]() |
Re: Decimal to binary help
This is what i have done so far...not working of course
![]() Quote:
i get a compile error missing return statment line 9
__________________
biggest NOOB ever :P Last edited by gam3r; Nov 8th, 2007 at 8:16 AM. |
|
|
|
|
|
|
#8 |
|
Sexy Programmer
|
Re: Decimal to binary help
You should use the return statement to return a string value from your method.
Let me know if this works. I didn't compile or test this code. public static String decToBin(int decimal, String str)
{
str += decimal % 2;
decimal /= 2;
if (decimal == 0)
return new StringBuffer(str).reverse().toString();
else
return decToBin(decimal, str);
}
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0
![]() |
Re: Decimal to binary help
Hey! Thanks for the reply. I tried using your code but it won't compile...just get "else without if on line9"..
The way you have done it isn't recursive though is it? i know my coding is horribly wrong for the conversion..
__________________
biggest NOOB ever :P |
|
|
|
|
|
#10 | |
|
Sexy Programmer
|
Re: Decimal to binary help
Quote:
As you can see from my snippet of code, I did indeed have an if statement. How did you insert my code in yours? Please just don't copy and paste. Understand and learn from what others are writing. It is frustating to hold your hand every step of the way.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| linked list & binary tree questions | n00b | C++ | 14 | Nov 4th, 2006 2:24 AM |
| Java code to convert binary to decimal and hexadecimal to binary | prince_haldir | Java | 1 | Mar 7th, 2006 1:51 AM |
| Binary | Ubert | Other Programming Languages | 8 | Sep 23rd, 2005 10:09 AM |
| binary and ascii | Nellie | C++ | 4 | Jun 8th, 2005 1:39 PM |
| hex and decimal RGB transforming please help | cloud- | Visual Basic | 5 | Jan 17th, 2005 3:17 PM |