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

  • Thread starter Thread starter JasonJo
  • Start date Start date
  • Tags Tags
    compsci Homework
AI Thread Summary
The discussion revolves around creating a Java program that checks if a user-input string is a valid binary number and converts it to decimal. The original poster struggles with the logic for validating the binary number, using a while loop and an if condition to check each character. Respondents suggest checking the brackets and reversing the if-else logic for clarity. They also recommend using a for loop with a termination condition to streamline the validation process. The focus is on ensuring the program correctly identifies valid binary inputs before conversion.
JasonJo
Messages
425
Reaction score
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
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:
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
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'Calculation of Tensile Forces in Piston-Type Water-Lifting Devices at Elevated Locations'
Figure 1 Overall Structure Diagram Figure 2: Top view of the piston when it is cylindrical A circular opening is created at a height of 5 meters above the water surface. Inside this opening is a sleeve-type piston with a cross-sectional area of 1 square meter. The piston is pulled to the right at a constant speed. The pulling force is(Figure 2): F = ρshg = 1000 × 1 × 5 × 10 = 50,000 N. Figure 3: Modifying the structure to incorporate a fixed internal piston When I modify the piston...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...

Similar threads

Replies
12
Views
2K
Replies
7
Views
3K
Replies
2
Views
12K
Replies
16
Views
2K
Replies
4
Views
2K
Replies
34
Views
21K
Back
Top