Reputation: 17
Im trying to make a binary string into a decimal. It will terminate if -1 is entered. I am stuck with using an array. It was suggested to use: public static int binaryToDecimal (String binaryString) . But Im not sure how to do that. This is what I have:
import java.util.Scanner;
public class BinaryConversion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inString;
int decimal;
System.out.println("Enter a binary number: ");
inString = input.nextLine();
while (inString != "-1") {
int i;
int binaryLength;
binaryLength = inString.length();
for (i = 0, decimal = 0; i < binaryLength; i++) {
decimal = decimal * 2 + (inString[i] - 0);
System.out.print(decimal);
}
System.out.println("Enter a binary number: ");
inString = input.nextLine();
}
System.out.println("All set !");
}
}
It says there is a compilation problem with the array. Thank you!
Upvotes: 0
Views: 3645
Reputation: 1
byte[] binary = {1,1,0,1};
int decimal = 0;
for(int i=binary.length-1, j=0; i>=0; i--, j++){
decimal += binary[i]*Math.pow(2, j);
}
Upvotes: 0
Reputation: 11890
You can simplify the code by using Integer.parseInt()
:
public static void main(String[] args) {
final Scanner input = new Scanner(System.in);
String inString;
while (true) {
System.out.println("Enter a binary number: ");
inString = input.nextLine();
if (inString.equals("-1"))
break;
System.out.println(Integer.parseInt(inString, 2));
}
System.out.println("All set !");
}
Upvotes: 1
Reputation: 1611
Try this one:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inString;
int decimal;
System.out.println("Enter a binary number: ");
inString = input.nextLine();
//Character.getNumericValue(inString.charAt(i))
while (inString != "-1") {
int i;
int binaryLength;
binaryLength = inString.length();
for (i = 0, decimal = 0; i < binaryLength; i++)
{
decimal = decimal * 2 + (Character.getNumericValue(inString.charAt(i)) - 0);
System.out.print(decimal);
}
System.out.println("Enter a binary number: ");
inString = input.nextLine();
}
System.out.println("All set !");
}
}
As suggested, you have to use Character.getNumericValue
Upvotes: 1
Reputation: 23029
You had few logical and few syntax errors.
This is working code :
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inString;
int decimal;
System.out.println("Enter a binary number: ");
inString = input.nextLine();
while (!"-1".equals(inString)) {
int i;
int binaryLength;
binaryLength = inString.length();
for (i = binaryLength-1, decimal = 0; i >= 0; i--) {
if (inString.charAt(i) == '1') {
decimal += Math.pow(2, binaryLength-i-1);
}
}
System.out.println(decimal);
System.out.println("Enter a binary number: ");
inString = input.nextLine();
}
System.out.println("All set !");
}
Note that comparing String cannot be done with ==
, you have to use equals
or compareTo
methods.
Upvotes: 0
Reputation: 180
inString is a String, not an array. So, you can't use inString[i]. To get the character at a given position in the string, use inString.charAt(i), which returns a char.
Then, you'll also have to convert that char into an int. You can do this with Character.getNumericValue(char).
So in summary, instead of
inString[i]
you need to use
Character.getNumericValue(inString.charAt(i))
Upvotes: 1