• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Making a binary to decimal converter

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that worked, how would I get it to "initialize" x though?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what test should I use to determine if the loop should end?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes probably... But "do it again" - till how many times you will do it again... and how you determine that?
 
Chris Haris
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay... Happy digging and read the tutorials I have posted earlier...
 
Chris Haris
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would it be d.length() ?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes... try writing a for loop and print necessary things you need to check with the help of System.out.println().
 
Chris Haris
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We've got a winner! Thanks a lot for the help!
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran the code and got below output..



Hint - What is the purpose of creating the backwards String?
 
Chris Haris
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
>




 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Names like d and e are not helpful. An identifier should allow the reader to know what it means.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we suspend disbelief and pretenddoesn't work, here's how I'd tackle the problem:
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does line 9 do?
 
Campbell Ritchie
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i see now, it displays incorrect if without that statement.. really appreciated tha..
 
kevin baslot
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so btw, what is that process called?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic