This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Making a binary to decimal converter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Making a binary to decimal converter" Watch "Making a binary to decimal converter" New topic
Author

Making a binary to decimal converter

Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
Hey, I'm trying to make a binary to decimal converter, using a loop, for example if I were to enter 101, it would give me a value of 5, so far I have:



I realize that's nowhere near complete, but whenever I try to compile it to see what errors it pops up/test what it does so far, I get this error:


1 error found:
File: C:\Users\Chris\Desktop\Java Programming Programs\java\BinaryCounter.java [line: 14]
Error: C:\Users\Chris\Desktop\Java Programming Programs\java\BinaryCounter.java:14: cannot find symbol
symbol : method hasNextInt()
location: class java.lang.String


How do i fix this/ any ideas on what I'm doing wrong in general/ ideas on how to make it do what I want? Any help's appreciated.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
The error says hasNextInt() method is not present in the class java.lang.String. Might be the you intend to call the Scanner:hasNextInt() method?
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
that worked, how would I get it to "initialize" x though?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Can you explain the program step by step... You read a line and store it in a String variable d. What you want to check with the hasNextInt() method?
What you want to initialize the variable x with?
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22


this is my final code, and this is the error I'm getting, how should I fix it?


java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(Unknown Source)
at BinaryCounter.main(BinaryCounter.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
x is the index reading, because the first digit is 2^0, and the second is 2^1, i use x as the exponent, and the index number, and hasNextInt is the test, so when it runs out of numbers, the test comes back false, and the loop ends.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Chris - Please start Language Basics when you have free time. Sorry to say but I think you need to learn bit of basic language constructs well.

Now back to the program... You first read a String entered by the user using the nextLine() method. This String is the one you try to use inside your while() loop.

But when you call the hasNextInt() method, the Scanner again reads one more input from the user and checks whether that input entered by user is a valid integer or not.

Again since you have used the hasNextInt() method in the while loop it results into a infinite loop... since the hasNextInt() method returns true always for the already entered user input but not prompt user for the next input...
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
so what test should I use to determine if the loop should end?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
First I suggest you accomplish the conversion for one number. Another thing is that when you attempt to take the first digit (I presume you are doing so) using charAt() function is not right.

Below addresses two things -
1. Run for only one value entered by user.
2. How to take the bits from the user entered input.

Next step might be for you is to get all the bits in the number entered by user.. Try to use many System.out.println() statements to check whether the value of a variable is what you think...

And please UseCodeTags henceforth to post code.



Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
wow, that really cleared a lot up, the only problem is i need it to be a loop of some description, so technically getting one variable is getting all of them, i was using x to change the number that it was pulling from the string, so if i entered 1101 he first one would grab a 1, the second would grab the zero, the third would grab a 1, and the fourth would grab a 1, so it would add ((1*2^0)+(0*2^1)+(1*2^2)+(1*2^3)), and it would all be contained in a loop, that keeps adding until it gets to the end of the string.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Ok - so you need to take all the characters of a String and store it in the variable x for each iteration. So what code have you got to achieve that?
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
wouldnt it be charAt(x), repeating the whole process to get the number value, then add 1 to x and do it again?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Yes probably... But "do it again" - till how many times you will do it again... and how you determine that?
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
I have no idea, I thought it was the hasNextInt but that's not right, and so far that's all I've dug up in the book
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Okay... Happy digging and read the tutorials I have posted earlier...
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
would it be d.length() ?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Yes... try writing a for loop and print necessary things you need to check with the help of System.out.println().
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
I figured it out, thanks a lot, I'll post the final code if you're interested, but I made a string that adds 1 for each time its ran, and the loop runs until its equal to the length of the string, might be the jerry rigged way to do it, but whatever works, right?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Post the code once you are done... constructing new Strings for every run is a jiggy way though... But lets first see if you have done it right
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
I'm getting close... I'm still trying to figure out why my first z spits out 2 and my second spits out 3, when i input 110 as my initial string... This is it so far though...
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Note that the operator '^' cannot be used to find power in Java - Operators



Also the way you try to convert from left to right should be inverted. (The value of y should read from the right most digit if you want to operate like below)

1101 -> 1 * (2 ^ 0) + 0 * (2 ^ 1) + 1 * (2 ^ 2) + 1 * (2 ^ 3) = 1 + 0 + 4 + 8 = 13
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
We've got a winner! Thanks a lot for the help!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I ran the code and got below output..



Hint - What is the purpose of creating the backwards String?
Chris Haris
Greenhorn

Joined: Nov 16, 2011
Posts: 22
I know, I just saw that the backwards part didn't work too well... I forgot to change string d to be the backwards string, which I now named e for simplicity


heres my test


> run BinaryCounter2
Enter a number in base 2 format.
[DrJava Input Box]
you entered 1101 backwards
y is 1
x is 0
z is 1.0
loop total is 1.0
loopval internal is 1
y is 1
x is 1
z is 2.0
loop total is 3.0
loopval internal is 2
y is 0
x is 2
z is 0.0
loop total is 3.0
loopval internal is 3
y is 1
x is 3
z is 8.0
loop total is 11.0
loopval internal is 4
loopval is 4
x is 4
loop absolute total is 11.0
origional input 1011
>



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Welcome to the Ranch
Your indentation is inconsistent.
You are using a complicated way of doubling. There is a much easier way of repeatedly doubling than using Math.pow(). And you aren’t getting the right answers. I tried your code with 10101010 and got 85.0. That ought to print an int, not a double, and 0b10101010 = 170.
You need to get an algorithm, and make it simple. You can probably end up with a method half as long.
Suggested algorithmI don’t think you need the backward String at all. You should also print the backward String, and see whether you have really reversed it. Do you know how to do arithmetic with chars? Remember a char is not a character, but a number.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Names like d and e are not helpful. An identifier should allow the reader to know what it means.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

If we suspend disbelief and pretenddoesn't work, here's how I'd tackle the problem:
kevin baslot
Greenhorn

Joined: Mar 21, 2013
Posts: 3
what does line 9 do?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Welcome to the Ranch

You have obviously been told that a char is a letter. But it isn’t. It is a number. You can therefore do arithmetic with it. Adding and subtracting usually give some sort of sensible result, but multiplication and division often give nonsense results.
kevin baslot
Greenhorn

Joined: Mar 21, 2013
Posts: 3
i see now, it displays incorrect if without that statement.. really appreciated tha..
kevin baslot
Greenhorn

Joined: Mar 21, 2013
Posts: 3
so btw, what is that process called?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making a binary to decimal converter
 
Similar Threads
Create a user class help
Binary to decimal converter
Scanner
illegal start of an expression
New class not working