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.
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.
****************************************** * 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)