Knowledge Reigns Supreme
Jon Avadis wrote:Im a beginner myself but i guess its not good practise to use the condition in while to assign your "binary" String.
Joanne
Campbell Ritchie wrote:I think you are making life difficult for yourself by writing too much code. You should write, compile, run and test, maybe 5 lines. Then add another 5 lines and repeat the exercise.
What is probably happening is that all your == "exit" tests are returning what they are expected to return (ie false throughout), and you are then passing the text "exit" to the Integer.parseInt method.
I would suggest you look through the String class for methods which might make equality testing easier.
Joanne NealI wrote:It is a bit difficult to understand the first time you see it, but it is commonly used if you are only testing for one value.
Jay Brass wrote:A better solution
1. try {
2. while(!(binary = br.readLine()).equalsIgnoreCase("exit") ) {
3. System.out.println("You have input: "+binary);
4.
5. scan.useDelimiter("\n");
6.
7. try {
8. dec = Integer.parseInt(binary, 2);
9. }
10. catch(NumberFormatException nfe){
11. System.out.println("Decimal value is not a number: ");
12. }
. . . will always return true because the "exit" object entered from the keyboard is guaranteed to be a different object from that in the String pool. That is not caused by evaluating one condition, but by inappropriate use of the != and == operators.Sudhir Srinivasan wrote: . . . However, as per my original code, evaluating one condition (instead of three)
. . .
Campbell Ritchie wrote:. . . will always return true because the "exit" object entered from the keyboard is guaranteed to be a different object from that in the String pool.
Campbell Ritchie wrote:
That is not caused by evaluating one condition, but by inappropriate use of the != and == operators.
I was hoping you would find equalsIgnoreCase(). Always use an equals method for comparing reference types for identity, never == or !=. The exception to that rule is for members of enums, which are true singletons, where == and != are actually appropriate.
Campbell Ritchie wrote:As I said, when the class is loaded, the String object "exit" is created, and put into the String pool. Whenever there is another String which is identical to "exit" as a compile-time constant (I hope this is the correct link), the same String "exit" is found and the same object is used. This confuses beginners
Campbell Ritchie wrote:There is a far better explanation of that elsewhere on the Ranch: search for "Strings, literally", and you will find a JavaRanch Journal article about this very topic.
Campbell Ritchie wrote:
You can find how to convert between binary and decimal numbers here.
Campbell Ritchie wrote:Pencil and paper. Convert this binary number to decimal
001011010101001010110101010101001110100101010
I don't know the right answer; I simply tapped the keys several times with my eyes shut. Now, how do you do it?@***@ Start with a decimal value of 0. Look at the leftmost binary digit. Add it to your decimal value. Double the decimal value. Delete the leftmost digit from the binary number. If any binary digits remain, go back to @***@
Campbell Ritchie wrote:
001011010101001010110101010101001110100101010 0
01011010101001010110101010101001110100101010 0
1011010101001010110101010101001110100101010 0
011010101001010110101010101001110100101010 1
11010101001010110101010101001110100101010 2
1010101001010110101010101001110100101010 5
010101001010110101010101001110100101010 11
10101001010110101010101001110100101010 22
0101001010110101010101001110100101010 45
101001010110101010101001110100101010 90
01001010110101010101001110100101010 181
1001010110101010101001110100101010 362
001010110101010101001110100101010 725
01010110101010101001110100101010 1450
1010110101010101001110100101010 2900
010110101010101001110100101010 5801
10110101010101001110100101010 11802
0110101010101001110100101010 23605
110101010101001110100101010 47210
10101010101001110100101010 94421
0101010101001110100101010 188843
101010101001110100101010 377686
etc etc and the calculator says it should be 6229156601130.
Try a smaller number first.
Campbell Ritchie wrote:
You can get a char[] from a String "0101010110011010110101010" with one of its methods. You can do arithmetic on chars, eg c - '0'. So you can easily get 1 and 0 out of that String.
Campbell Ritchie wrote:
Your method with the pow() method call looks inordinately complicated.
Poop goes in a willow feeder. Wipe with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|