Results 1 to 3 of 3

Thread: String to Float

  1. #1
    durahman is offline Member
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0

    Default String to Float

    I had been browsing the forum to get ideas from members.

    I was trying to do some validations on input entered by users through a form. Validation involve say Input fields must not empty and one must be an Integer and one must be a float.

    Field must not empty works fine.
    One Input must be an Integer works fine.

    Problem when come to float it seem my coding does not working the way I want it.

    Here is part of the validation code:

    Java Code:
    public boolean validate() {
    
    		boolean allOk=true;
    		if (noreg_majikan.equals("")) {
    			errors.put("noreg_majikan"," Perlu diisi.");
    			noreg_majikan="";
    			allOk=false;
    		}
    
    		if (bil_kosong.equals("")) {
    			errors.put("bil_kosong"," Perlu diisi.");
    			bil_kosong="";
    			allOk=false;
    		}else {
    			try {
    			      int x = Integer.parseInt(bil_kosong);
    			     }
    		     	      catch (NumberFormatException e) {
    				errors.put("bil_kosong","Perlu dalam bentuk angka");
    				bil_kosong="";
    				allOk=false;
    			   }	
    		      }
    
    		if (gaji.equals("")) {
    			errors.put("gaji"," Perlu diisi.");
    			gaji="";
    			allOk=false;
    		}else {
    			try {
    			       float y = Float.parseFloat(gaji);
    			    }
    		     	      catch (NumberFormatException e) {
    				errors.put("gaji","Perlu dalam bentuk angka");
    				gaji="";
    				allOk=false;
    		      	    }	
    		     }
    
    		return allOk;
    
    	}
    For the float part if the field not entered (empty) it give the correct message. If I enter a String it give the correct message.

    When I entered 9 ( or any integer) it works. But when I entered say 99.50 it give error as if I had entered a String.

    I just want to know why?

    Thanx

  2. #2
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    Did you try to enter 99,50 instead of 99.50?

    I don't know, just guessing

    Why don't you use Scanner? It should be much faster with it

  3. #3
    durahman is offline Member
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0

    Default

    When I tried it again it works.

    99,50 it gives a String error.

    Since I am new to java, I will explore the use of scanner.

    Thanks Hey.

Similar Threads

  1. Float to String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 05:08 PM
  2. convert string to float
    By miss_dot in forum NetBeans
    Replies: 1
    Last Post: 11-14-2007, 11:26 PM
  3. Error with convert to Float
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:18 AM
  4. Help with valid declarations of a float
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 10:28 AM
  5. Class float
    By Peter in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •