Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Validating user input if it is in the correct format.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Validating user input if it is in the correct format.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Validating user input if it is in the correct format.

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Validating user input if it is in the correct format.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Validating user input if it is in the correct format.

    can you explain it in english please? if you know what i mean..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How can I have the user input the name of an object?
    By kkid in forum Object Oriented Programming
    Replies: 2
    Last Post: November 12th, 2012, 06:03 PM
  2. GUI program won't take 'char' input code, can't seem to find the correct one...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2012, 07:12 AM
  3. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  4. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 07:14 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM