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

  • Comp Sci
  • Thread starter major_maths
  • Start date
  • Tags
    Issues Java
In summary, the conversation discusses the creation of a program that prompts the user to enter a binary number and then converts it into integers using Integer.parseInt. The issue being encountered is with tokenizing the string, as the code only works with a predefined variable and not when the same input is entered into a String variable. The suggested approach is to use a string with a maximum of 32 0's and 1's, and loop through the string from back to front to determine whether the current entry is a 0 or 1, and calculate the decimal number accordingly.
  • #1
major_maths
30
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
  • #2
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.
 

1. What is a Java Tokenizer?

A Java Tokenizer is a class in the Java programming language that breaks up a string of characters into smaller units known as tokens. These tokens can then be used in various ways, such as parsing or analyzing code.

2. How do I solve issues with my Java Tokenizer?

To solve issues with your Java Tokenizer, you must first identify the specific problem you are having. This could be a syntax error, incorrect use of methods, or other issues. Then, you can refer to the Java Tokenizer documentation or seek help from other programmers to find a solution.

3. What are some common errors when using a Java Tokenizer?

Some common errors when using a Java Tokenizer include NullPointerException, NoSuchElementException, and IllegalStateException. These errors can occur when the tokenizer is not properly initialized, or when it is used on an empty or incorrect input.

4. How can I improve the performance of my Java Tokenizer?

One way to improve the performance of your Java Tokenizer is to use the StringTokenizer class instead of the String class. This class is specifically designed for tokenizing strings and can be more efficient in certain cases.

5. Can I customize my Java Tokenizer to fit my specific needs?

Yes, you can customize your Java Tokenizer to fit your specific needs by using different methods and options available in the tokenizer class. For example, you can specify delimiters, ignore certain characters, or change the order in which tokens are returned.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
773
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
17K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
Back
Top