Validating user input if it is in the correct format.
Hi, my prof. asks us to write a conversion program in from binary to decimal, decimal to binary, etc.. It should have a menu system and a method should validate if the user input is number and is in correct format depending on the conversion. I have no problem making the code for the conversions, my ONLY problem is the validation method. kindly guide me on what is wrong with my code and what I should do to correct it. thanks!
The validation method should validate if the user inputted the correct format depending on the conversion used.
Code :
import java.io.*;
import java.lang.*;
public class scope {
static String num;
static int choice;
public static void main(String[] args) throws NumberFormatException, IOException{
welcomeScreen();
getUserInput();
}
public static void welcomeScreen(){
System.out.println(" ******************************************");
System.out.println(" * Welcome to the Number System Converter *");
System.out.println(" ******************************************");
System.out.println();
}
public static void getUserInput() throws NumberFormatException, IOException{
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("Please choose from ne of the following:");
System.out.println();
System.out.println("[1] Decimal to Binary");
System.out.println("[2] Binary to Decimal");
System.out.println("[3] Decimal to Hexadecimal");
System.out.println("[4] Decimal to Octal");
System.out.println("[5] Exit");
System.out.println();
choice = Integer.parseInt(bReader.readLine());
switch(choice){
case 1: System.out.println(validateNumber(num, choice)+ decimalToBinary(num)); break;
case 2: System.out.println(); break;
case 3: System.out.println(); break;
case 4: System.out.println(); break;
}
}while(choice != 5);
}
public static boolean validateNumber(String num, int choice) throws IOException{
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a number.");
num = bReader.readLine();
if(choice == 2){
for(int i=0; i<num.length(); i++){
char a=num.charAt(i);
if(a== 0 || a== 1){
return true;
}
else{
return false;
}
}
}
else if(choice == 1||choice == 3||choice == 4){
try{
int n = Integer.parseInt(num);
System.out.println(n);
} catch (NumberFormatException nfe){
System.out.println("Please only enter numbers!");
}
}
return false;
}
public static String decimalToBinary(String num){
int n = Integer.parseInt(num);
String toBinary = Integer.toBinaryString(n);
System.out.println("The answer is: " + toBinary);
return toBinary;
}
}
this is the output when I run the code.
Code :
******************************************
* Welcome to the Number System Converter *
******************************************
Please choose from ne of the following:
[1] Decimal to Binary
[2] Binary to Decimal
[3] Decimal to Hexadecimal
[4] Decimal to Octal
[5] Exit
//I chose 1 to convert from decimal to binary conversion
1
//here is the problem, when i enter a number, it tells me the problem below.
Please enter a number.
1
1
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at scope.decimalToBinary(scope.java:101)
at scope.getUserInput(scope.java:47)
at scope.main(scope.java:12)
Re: Validating user input if it is in the correct format.
Quote:
problem is the validation method
Can you explain what happens when the code is execute? What does it do wrong?
Can you copy the console window from when the code is executed and paste it here?
Add some comments to it describing what the program is doing wrong and explain what the code should do.
Re: Validating user input if it is in the correct format.
Quote:
Originally Posted by
Norm
Can you explain what happens when the code is execute? What does it do wrong?
Can you copy the console window from when the code is executed and paste it here?
Add some comments to it describing what the program is doing wrong and explain what the code should do.
edited it. the output if i run the code is there. i added comments in between the codes to let you know what i did.
Re: Validating user input if it is in the correct format.
Quote:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at scope.decimalToBinary(scope.java:101)
at scope.getUserInput(scope.java:47)
at scope.main(scope.java:12)
The code is passing a null value to the parseInt() method called on line 101. Look at line 101 and find the variable with the null value and then backtrack in the code to see why that variable does not have a valid non-null value.
Re: Validating user input if it is in the correct format.
can you explain it in english please? if you know what i mean.. :)
Re: Validating user input if it is in the correct format.
Find the source code on line 101, it's in the decimalToBinary() method
There is a call to parseInt() on that line.
The arg passed to the parseInt() method has a null value, which causes the exception.
Look at the code in the program to see why the variable that is the arg passed to parseInt() has a null value.
Where does that variable get assigned a valid, non-null value? If it doesn't ever get assigned a valid value, then you need to change the code so that it does get assigned a valid value.