tags:

views:

44

answers:

2

Following is clearly mentioned in java docs about java.text.NumberFormat class :-

setParseIntegerOnly : only affects parsing, e.g. if true, "3456.78" -> 3456 (and leaves the parse position just after index 6) if false, "3456.78" -> 3456.78 (and leaves the parse position just after index 8) This is independent of formatting.

Here is an example :-

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class NFTest {
  public static void main(String args[]) throws Exception {
    NumberFormat numberFormat = NumberFormat.getInstance();

    //statement which is not going well
    numberFormat = NumberFormat.getNumberInstance(Locale.CHINESE);
    numberFormat.setParseIntegerOnly(true);


    String usersNumber = "1976.0826" ;

    try{
        Number num = numberFormat.parse(usersNumber) ;
        System.out.println(num);
    }
    catch(ParseException pe){
        System.out.println(pe);
    }
  }
}

o/p :-

1976

Now, here again the same example with a slight change :-

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class NFTest {
  public static void main(String args[]) throws Exception {
    NumberFormat numberFormat = NumberFormat.getInstance();

    numberFormat.setParseIntegerOnly(true);
    //result is getting effected, if i change the
    //position of following statement
    numberFormat = NumberFormat.getNumberInstance(Locale.CHINESE);


    String usersNumber = "1976.0826" ;

    try{
        Number num = numberFormat.parse(usersNumber) ;
        System.out.println(num);
    }
    catch(ParseException pe){
        System.out.println(pe);
    }
  }
}

o/p :-

1976.0826

Why the result is getting effected due to change in the position of statements???

+2  A: 

You're replacing the value of numberFormat with a reference to a completely different object... so the setParseIntegerOnly call has no effect whatsoever.

This code:

NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setParseIntegerOnly(true);
//result is getting effected, if i change the
//position of following statement
numberFormat = NumberFormat.getNumberInstance(Locale.CHINESE);

is equivalent (assuming getInstance and setParseIntegerOnly have no other side-effects) to:

NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.CHINESE);

To take it out of the context of parsing, suppose we had this simple class:

public class Person
{
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Your question is effectively asking the difference between:

Person person = new Person();
person = new Person();
person.setName("Will be seen");
System.out.println(person.getName());

and

Person person = new Person();
person.setName("Will be seen");
person = new Person();
System.out.println(person.getName());
Jon Skeet
thnx Mr. Skeet :)
rits
A: 

In your second code example you have this code:

NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setParseIntegerOnly(true);
numberFormat = NumberFormat.getNumberInstance(Locale.CHINESE);

This executes the following steps:

  • Declare a NumberFormat variable named numberFormat
  • Assign the NumberFormat instance returned by NumberFormat.getInstance() to numberFormat
  • Change the parseIntegerOnly property of that NumberFormat instance to true
  • Assign the NumberFormat instance returned by NumberFormat.getNumberInstance(Locale.CHINESE); to numberFormat

So you effectively set that property on a NumberFormat instance that you don't use later on.

Joachim Sauer