English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

if i have this code so i want to check if the user enters a valid binary no or any other no , so how?

import javax.swing.*;

public class lily {

static public void main (String args [] ){

String word;

String second;

word = JOptionPane.showInputDialog(null, "enter your first number ");

double star = Double.parseDouble(word);

double finish =Integer.parseInt(word,2) ;

second = JOptionPane.showInputDialog(null, "enter your second number ");

double sun = Double.parseDouble(second);

double wild =Integer.parseInt(second,2) ;

double planet=finish+wild;

word=Integer.toBinaryString((int)Math.round(planet));

JOptionPane.showMessageDialog(null, "the result of the two numbers" + word);

JOptionPane.showMessageDialog(null, "the result of the two numbers" + planet);

System.exit(0);



}
}

2007-10-18 23:46:47 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

The only way is to check the individual digits and make sure that they do not exceed the maximum value.
For binary that would be 1.
But you cannot validate one input against all number types. You must know what number type the user is going to input.

101 is valid decimal input as well as binary.
For decimal you can only ensure that the input is numeric.
This can be done when using the parse method
http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
Take parseInt() for example. This throws NumberFormatException. Use try and catch and print out an appropriate message.

2007-10-19 00:17:19 · answer #1 · answered by AnalProgrammer 7 · 0 0

once you talk approximately 0 's and a million 's, in all probability you're questioning approximately what it relatively is universal as device language precise? The language that the workstation relay talks and each application gets switched over to.nicely ,, there are approximately 2 greater tiers or layers under your 0's and a million's. the subsequent point under is universal as "microcode" or "microprogram" . it relatively is the code that lives contained in the processor and is the single that drives each and every of the operations of the CPU. under which you have the fairly circuit that has aspects all made up of excellent judgment gates. some processors have not got( or have little or no) the microcode and bypass directly from the actual circuit to 0's and a million's. on the circuit point you have aspects like shifters, mutex, decoders, encoders, and so on. those men do very worry-loose and easy issues. they're created from good judgment gates that carry out mathematic operations like OR , AND, XOR, no longer. The gates are created from transistors after which you get into an different field ( physics ;). an entire degree is mandatory to be attentive to the way all that works at the same time.

2016-10-07 05:19:02 · answer #2 · answered by ? 4 · 0 0

fedest.com, questions and answers