Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Decimal to binary help (http://www.programmingforums.org/showthread.php?t=14362)

gam3r Nov 8th, 2007 6:16 AM

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:

Q1.java:11: 'class' or 'interface' expected
public static void main(String args[])
My code is

Quote:

import java.util.Scanner; //this program uses class scanner

public class Q1
{
public static int binTodec(String b)
{
System.out.println(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");

String b = input.nextLine();

binToDec(b);

}
Can anyone help?

Thanks

Grich Nov 8th, 2007 6:40 AM

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));

}
}

Remember, your "public static int binToDec" is setting a value, returning a value and you have to return a string when dealing with the "toBinaryString" function so I changed it to "public static String binToDec". You were setting a procedure. A bracket was in the wrong place as well.
Don't worry I'm not telling you off, just giving a few pointers.

Grich Nov 8th, 2007 6:42 AM

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.

gam3r Nov 8th, 2007 6:47 AM

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?

Grich Nov 8th, 2007 7:04 AM

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);
}
}

Just import it and use it. Classes are very in depth, wikipedia has some good articles on the topic.

Grich Nov 8th, 2007 7:12 AM

Re: Decimal to binary help
 
Sorry, I can't go into too much detail, too sleepy...:yawn:

gam3r Nov 8th, 2007 8:01 AM

Re: Decimal to binary help
 
This is what i have done so far...not working of course :(

Quote:

public class exercise {

public static String decTobin (int n){
int r;
if (n>0){
r=n%2;
decTobin(r);

}
}
//public static int binTodec (String b){

// }
public static void main(String[] args) {

int d = Integer.parseInt(args[0]);
System.out.println("The binary representation of " + d + " is " + decTobin(d));
String b = args[1];
System.out.println("The decimal representation of " + b + " is " + binTodec(b));
}
}

i get a compile error missing return statment line 9

ReggaetonKing Nov 8th, 2007 9:08 AM

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);
}


gam3r Nov 8th, 2007 10:01 AM

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..

ReggaetonKing Nov 8th, 2007 10:05 AM

Re: Decimal to binary help
 
Quote:

Originally Posted by gam3r (Post 136473)
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?

If you ask that question, you don't know what recursion is.

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.


All times are GMT -5. The time now is 4:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC