Java Program for Valid Binary Number Conversion and Checking | Help Needed

In summary, the conversation discusses the need to write a Java program that takes a string input from the user, checks if it is a valid binary number, and then converts it to decimal. The person is having trouble checking if the input is a binary number and asks for help. Solutions are suggested, such as using a for loop and checking for the characters '0' and '1', and ensuring there is a termination condition in the loop.
  • #1
JasonJo
429
2
hey I need to write a Java program to ask the user for a string input, that should be a binary number. i am to check if the string input is a valid binary number and then convert it into decimal. i know how to program the converting into decimal, but i am having a nightmare as to checking whether or not it is a binary number.

i used the following if condition in my program (psuedo code is used to simpifly it)

num1 = the string to be input by the user
i = 0;
z = length of the string
while (i < z)
if (character i of the string is not equal to 0 and it is not equal to 1) //character i would be the 0th character, using 0 indexing
print the number input is not a valid binary number
else
i = i + 1 //search the entire string

anyone see why i can't seem to properly check to see if its a binary number?

thanks a lot
 
Physics news on Phys.org
  • #2
check brackets and if your if condition is indeed the one you put there.

OR you can revers the if and else.

plus you don't have a termination condition. to exit while loop
 
Last edited:
  • #3
Try:

Code:
String input = <input from user>...

for (int i = 0; i < input.length(); i++) {
    if ((input.charAt(i) != '0') && (input.charAt(i) != '1')) {
        // error and quit...
    }
}

// success
 

1. How can I get help with my CompSci homework?

There are a few ways you can get help with your CompSci homework. You can reach out to your classmates or professor for assistance, join a study group, or seek help from online resources such as forums, tutorials, or tutoring services.

2. What are some good online resources for CompSci homework help?

There are many online resources available for CompSci homework help, such as Stack Overflow, Codecademy, and Khan Academy. These websites offer tutorials, forums, and other resources to help you with your homework.

3. How can I improve my coding skills for my CompSci homework?

Practice is key to improving your coding skills. You can also try working on small coding projects outside of your homework assignments, attending coding workshops or bootcamps, and regularly reviewing and understanding your code.

4. What should I do if I am struggling with a specific concept in my CompSci homework?

If you are struggling with a specific concept in your CompSci homework, try breaking it down into smaller parts and tackling them one by one. You can also seek help from your professor or classmates, or consult online resources for additional explanations and examples.

5. How can I manage my time effectively for my CompSci homework?

To manage your time effectively for CompSci homework, it is important to prioritize and plan ahead. Break down your assignments into smaller tasks and schedule specific times to work on them. Make sure to also take breaks and avoid procrastination to stay on track with your homework.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
17K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
12K
Back
Top