How can I convert a binary string to decimal using Java?

  • Context: Comp Sci 
  • Thread starter Thread starter major_maths
  • Start date Start date
  • Tags Tags
    Issues Java
Click For Summary
SUMMARY

This discussion focuses on converting a binary string to a decimal integer in Java using the Integer.parseInt method. The user encountered issues with tokenizing the input string using StringTokenizer, which worked with a predefined variable but failed with user input. A more effective approach involves iterating through the binary string from back to front, calculating the decimal value based on the binary digits' positions, as demonstrated with the example of converting "110101" to 53.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of binary number representation
  • Familiarity with String manipulation in Java
  • Knowledge of using Scanner and StringTokenizer classes
NEXT STEPS
  • Learn how to implement a loop to process strings in Java
  • Research the use of Integer.parseInt with different bases
  • Explore Java's StringBuilder for efficient string manipulation
  • Study error handling in user input for Java applications
USEFUL FOR

Java developers, computer science students, and anyone interested in binary-to-decimal conversion techniques in programming.

major_maths
Messages
30
Reaction score
0
I'm trying to make a program that prompts a user to enter a binary number and then converts the string into integers by using Integer.parseInt. Right now though, I'm having trouble with tokenizing the string. My code works when I use the predefined variable temp but not when I enter the same thing into the String variable inputLine.

Code:
import java.util.*;

public class testBitConverter
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Please enter a string.");
		String inputLine = keyboard.next(); 
		
                String temp = "1 1 0 0 0 1 1 1";

		StringTokenizer tester = new StringTokenizer(inputLine);
		String first = tester.nextToken();
		String second = tester.nextToken();
		String third = tester.nextToken();
		String fourth = tester.nextToken();
		String fifth = tester.nextToken();
		String sixth = tester.nextToken();
		String seventh = tester.nextToken();
		String eighth = tester.nextToken();
		
		System.out.println("This is the output: ");
		System.out.println("first token: "+first);
		System.out.println("second token: "+second);
		System.out.println("third token: "+third);
		System.out.println("fourth token: "+fourth);
		System.out.println("fifth token: "+fifth);
		System.out.println("sixth token: "+sixth);
		System.out.println("seventh token: "+seventh);
		System.out.println("eighth token: "+eighth);
	}
}
 
Physics news on Phys.org
This doesn't seem like the right approach to me. Your input should be a string that consists of at most 32 0's and 1's. After the string is entered, your program should loop through the string from the back to the front, determining whether the current entry in the string is a '0' or a '1'.

For example, if the entered string is "110101" the resulting decimal number will be the sum of
1 * 20 = 1
0 * 21 = 0
1 * 22 = 4
0 * 23 = 0
1 * 24 = 16
1 * 25 = 32

These add up to 53.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 10 ·
Replies
10
Views
12K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K