Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 8th, 2007, 6:16 AM   #1
gam3r
Newbie
 
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0 gam3r is on a distinguished road
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
__________________
biggest NOOB ever :P
gam3r is offline   Reply With Quote
Old Nov 8th, 2007, 6:40 AM   #2
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 199
Rep Power: 2 Grich is on a distinguished road
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.
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Old Nov 8th, 2007, 6:42 AM   #3
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 199
Rep Power: 2 Grich is on a distinguished road
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 ...
Grich is offline   Reply With Quote
Old Nov 8th, 2007, 6:47 AM   #4
gam3r
Newbie
 
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0 gam3r is on a distinguished road
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
gam3r is offline   Reply With Quote
Old Nov 8th, 2007, 7:04 AM   #5
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 199
Rep Power: 2 Grich is on a distinguished road
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.
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Old Nov 8th, 2007, 7:12 AM   #6
Grich
Hobbyist Programmer
 
Grich's Avatar
 
Join Date: Sep 2007
Location: Sydney - Australia
Posts: 199
Rep Power: 2 Grich is on a distinguished road
Re: Decimal to binary help

Sorry, I can't go into too much detail, too sleepy...
__________________
SYNTAX ERROR ...
Grich is offline   Reply With Quote
Old Nov 8th, 2007, 8:01 AM   #7
gam3r
Newbie
 
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0 gam3r is on a distinguished road
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
__________________
biggest NOOB ever :P

Last edited by gam3r; Nov 8th, 2007 at 8:16 AM.
gam3r is offline   Reply With Quote
Old Nov 8th, 2007, 9:08 AM   #8
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old Nov 8th, 2007, 10:01 AM   #9
gam3r
Newbie
 
Join Date: Jun 2005
Location: London, England
Posts: 24
Rep Power: 0 gam3r is on a distinguished road
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
gam3r is offline   Reply With Quote
Old Nov 8th, 2007, 10:05 AM   #10
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Re: Decimal to binary help

Quote:
Originally Posted by gam3r View Post
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.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:45 PM.

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