• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program to convert binary to decimal  RSS feed

 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on creating the Binary to Decimal program
I cannot figure out what is wrong with this part of my code.
Why does it not take you into the loop.


 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your for loop condition. The loop only runs when that is true. What values for input.length will allow that condition to be true the first time the loop runs (when i is 0) ?
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now it returns Not Binary for the binary numbers as well.
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I should also say I am very new to java, And a little confused when it comes to loops.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (a < 0 || a > 1)


Again you're getting burned by the conditional. Any value of "a" that's more than 1 is going to return True. Are you sure that greater than and less than is what you want to check for?

And don't worry, we were all new once.

And welcome to The Ranch!
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if I change it to not equal it still comes out as not binary when it is binary....
Sad thing is I still have to come up with the algorithm... :S I have 3 days to get this done. I am hoping to tackle this part tonight.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're new, I think it would help you to know that the value of char '1' is different from int 1. Likewise char '0' is not the same as int 0.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code compiles because char a is promoted to int but the values being compared are not what you think they are. You have to make the values that you're comparing compatible with each other so there's no misunderstanding between you and the computer. You think you're telling the computer one thing but the computer is interpreting your instructions differently.

Funny story that's kind of related: A guy who was trying to learn to speak Chinese decided to try out what he learned so far at a Chinese restaurant. He wanted a bowl of soup so first, he asked the waitress "How much for one bowl?" The waitress slapped him and stormed off. On seeing the bewildered look on his face, the Chinese gentleman at the next table told him, "She was insulted because you asked her 'How much for one night?'" Turns out the word for "bowl" and "night" were the same except for the tone.
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So should I change the way I declared the variables, from char to int?
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also thanks everyone so far for your help, I feel a tad bit dense today! I can't seem to figure this one out.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a simpler thing you can do: change the kind of literal you're using. Try printing out the values with System.out.println();

Edit: Sorry, you have to force promotion to int for the chars to see what Java was doing with your code. Casting to int does that. I hope this doesn't confuse you even more. Anyway, the (int) on lines 2 and 4 force Java to promote the char literal to an int.
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
??
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khaly Silverman wrote: ??
Did you try it to see if it works? Is it right to use "or" here?
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't, I get a weird error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:646)
at question5.main(question5.java:14)
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must seem like a lost cause, I swear I was doing so awesome with java until this...
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I find really helpful in finding problems with code is to read it OUT LOUD. Yeah, people may look at you funny, talking to yourself/the computer but it's funny how hearing what you think you're telling the computer to do can make it clearer that you're not really telling it the right thing.
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I now used the and && operator and ...
It always returns binary ....
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khaly Silverman wrote:It doesn't, I get a weird error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:646)
at question5.main(question5.java:14)


Note that the error message says: "String index out of range: -1" where -1 is the value of the index that's out of range. Ask yourself "How did the index get to be -1?"
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel like I am getting closer
Enter a Binary number.
10203
Binary
Binary
NOT BINARY
Binary
NOT BINARY


 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you want to check each character in the string and display a message for each one or did you want to take the string as a whole and display one message for the entire string? As it is now, your program is doing the former. If it's the latter you wanted to do, then you have to rethink what you do inside the loop and what you want to keep outside the loop. Remember, instructions inside the loop get repeated over and over.
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should I be looking at doing if I want to ask for another binary number if the number isn't binary?
Oh and I want it to say "this number isn't binary, Please enter a new number."
I'm slowly getting there.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khaly Silverman wrote:What should I be looking at doing if I want to ask for another binary number if the number isn't binary?

you should be looking at the various Control Flow Statements available, understand how each one works and pick one that's appropriate for what you want to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!