Redefine Scanner's not-a-number recognition? - java

In Java, the class java.util.Scanner provides a convenient way to parse long strings. In my particular case I have to parse a string with many double values, for which I use the nextDouble() method.
Sometimes, my input string contains nan instead of a valid float number. Unfortunately, Scanner only seems to recognize NaN for not-a-number.
Is there any way to teach it to also recognize nan? Maybe by setting a custom Locale with DecimalFormatSymbols.setNaN()?

How about something like this?
private static final Pattern nan =
Pattern.compile("nan", Pattern.CASE_INSENSITIVE);
public static boolean hasNextDouble(Scanner scanner) {
if(scanner == null)
return false;
return scanner.hasNext(nan) || scanner.hasNextDouble();
}
public static double nextDouble(Scanner scanner) {
if(scanner.hasNext(nan)) {
scanner.next();
return Double.NaN;
}
return scanner.nextDouble();
}

One option is setting a custom Locale. Another option is that internally the scanner uses a regular expression to retrieve a double-string and then uses Double.parseDouble to convert it to a double, so you could call Scanner#next(Pattern pattern) using the regular expression defined here except using "nan" instead of "NaN" and then call Double.parseDouble on the returned string.

Related

why do I get this numberformatexception? [duplicate]

Having this error
java.lang.NumberFormatException: For input string: "20,00"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.agitech.autofaces.el.FormatUtil.parseDouble(FormatUtil.java:122)
at com.agitech.erp.converter.DoubleConverter.getAsObject(DoubleConverter.java:27)
after reading decimal-separator-in-numberformat this, i try
public class FormatUtil {
public static char ApplicationDecimalSeparator = ',';
public static char SystemDecimalSeparator;
static {
DecimalFormatSymbols symbols= DecimalFormatSymbols.getInstance();
SystemDecimalSeparator = symbols.getDecimalSeparator();
symbols.setDecimalSeparator(ApplicationDecimalSeparator);
}
public final static Double parseDouble(String d){
if (d==null)
return 0.0;
return Double.parseDouble( fixDecimalSeparator(d) );
}
public final static String fixDecimalSeparator(String d){
if (SystemDecimalSeparator==ApplicationDecimalSeparator)
return d;
return d.replaceAll( ""+SystemDecimalSeparator, ""+ApplicationDecimalSeparator);
}
}
finally the SystemDecimalSeparator is already the ',' so why this exception ?
Its probably expecting 20.00 but how to get and fix this separtor ?
Indeed i test and its expectiong "20.00" instead and the default SystemDecimalSeparator is already the ','
You're calling Double.parseDouble, which always uses a dot as the decimal separator - it doesn't use the default locale at all.
Double.parseDouble is documented to behave like Double.valueOf(String), which has documentation including:
To interpret localized string representations of a floating-point value, use subclasses of NumberFormat.
This should work:
NumberFormat f = NumberFormat.getInstance(); // Gets a NumberFormat with the default locale, you can specify a Locale as first parameter (like Locale.FRENCH)
double myNumber = f.parse("20,0").doubleValue(); // myNumber now contains 20

check if currency has valid symbol

I have a string that represents a currency value like $200, €200, ¥200.
As per my task, I support the only the dollar, euro, and yen, but not other currency symbols.
I want to get the first character of string and check if that is a valid currency symbol, which means is that is either Euro or Dollar or Yen but not other currencies. How can do this in Java?
This is my sample code:
char c = s.charAt(0);
if(c == dollar || c == euro || c == yen) { // how can I write the symbols in my code?
valid = true;
} else {
valid = false;
}
It's easier than you might think:
if(c == '$' || c == '€' || c == '¥')
You say you don't want to use "special symbols" in your code... I don't understand why you wouldn't; but you can write the euro and Yen symbols as '\u20AC' and '\u00A5' respectively if you want (but note that the first thing the Java compiler does is to convert them back to € and ¥....)
A strong reason to use the unicode symbol rather than \uNNNN is readability: it would be easy to accidentally get the code wrong, and it wouldn't be easy to spot. And, presumably, you're going to write tests for the code - what do you use in the tests to make sure it's doing the right thing? If you use the \uNNNN form, you run the risk that you've got it wrong there as well; and if you use the symbol in the tests, then you may as well just use the symbol in the production code as well.
you can do this with unicode
import java.util.Arrays;
import java.util.List;
// Convert list of Characters to string in Java
class Main
{
public static void main(String[] args)
{ String s="$200";
char c = s.charAt(0);
List<Character> currency = Arrays.asList('\u0024', '\u20AC', '\u20AC');
if(currency.contains(c))
System.out.println("true");
}
}
How about some pattern matching?
Create separate class for string validation. Pass a Set of currency signs you want to match your string against. Compile pattern on class initialization (it's heavy operation so better do it once and store the pattern itself). In that case you'll get pattern like this ^[$€¥]\\d+$ (that means - beginning of the string - any character in square brackets (just one) - one or more digits - end of the string). Then just pass the String to isValid method and match against pattern. If you need to change available currencies just add or remove them from Set (you can even get them from separate .properties file). This approach makes your app flexible.
public class Main{
public static void main(String[] args) {
Set<String> currencySet = Set.of("$", "€", "¥");
CurrencyValidator currencyValidator = new CurrencyValidator(currencySet);
String value = "$200";
System.out.println(currencyValidator.isValid(value));
}
}
public class CurrencyValidator {
private final Set<String> currencySet;
private Pattern currencyPattern;
public CurrencyValidator(Set<String> currencySet) {
this.currencySet = currencySet;
init();
}
private void init() {
String currencyString = currencySet.stream()
.collect(Collectors.joining("", "^[", "]\\d+$"));
currencyPattern = Pattern.compile(currencyString);
}
public boolean isValid(String value) {
return currencyPattern.matcher(value).matches();
}
}

trouble formatting double in my getter method

my code:
private double retailPrice = 699;
DecimalFormat df = new DecimalFormat("#,###.00");
public double getRetailPrice()
{
return df.format(retailPrice);
}
I am trying to format this for a HW assignment. It's not really required, but I wanted to try this as a learning experience. The method should return a double, but when I try to use decimal formatter, it gives an error:
string cannot be converted into a double
but it's not a string...right?
Basically this ends up as part of a StringBuilder object that is written to a csv file, so it needs to be formatted before it is appended to the StringBuilder.
Do this
public String getRetailPrice()
{
return df.format(retailPrice);
}
You are mixing up two things.
A double number is just that: a number. It does not know about formatting. Formatting is when you turn numbers into strings.
Those are two different things, and there is simply no point in wanting to format a double value whilst keeping it a double. Your code is pointless, plain and simple.
Solution: either have the getter return the double value as it is. Or change its return type to string. And maybe rename it to "getPriceAsFormattedString" for example.
format method return type is String, so you have to parse again formatted value into double. like below
public double getRetailPrice() {
return Double.valueOf(df.format(retailPrice));
}

Double.parseDouble not accounting for locale [duplicate]

I want to convert some numbers which I got as strings into Doubles, but these numbers are not in US standard locale, but in a different one. How can I do that?
Try java.text.NumberFormat. From the Javadocs:
To format a number for a different Locale, specify it in the call to getInstance.
NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
You can also use a NumberFormat to parse numbers:
myNumber = nf.parse(myString);
parse() returns a Number; so to get a double, you must call myNumber.doubleValue():
double myNumber = nf.parse(myString).doubleValue();
Note that parse() will never return null, so this cannot cause a NullPointerException. Instead, parse throws a checked ParseException if it fails.
Edit: I originally said that there was another way to convert to double: cast the result to Double and use unboxing. I thought that since a general-purpose instance of NumberFormat was being used (per the Javadocs for getInstance), it would always return a Double. But DJClayworth points out that the Javadocs for parse(String, ParsePosition) (which is called by parse(String)) say that a Long is returned if possible. Therefore, casting the result to Double is unsafe and should not be tried!
Thanks, DJClayworth!
NumberFormat is the way to go, but you should be aware of its peculiarities which crop up when your data is less than 100% correct.
I found the following usefull:
http://www.ibm.com/developerworks/java/library/j-numberformat/index.html
If your input can be trusted then you don't have to worry about it.
Just learning java and programming. Had similar question. Found something like this in my textbook:
Scanner sc = new Scanner(string);
double number = sc.nextDouble();
The book says that a scanner automatically decodes what's in a String variabel and that the Scanner class automatically adapts to the language of the set Locale, system Locale being the default, but that's easy to set to something else.
I solved my problem this way. Maybe this could work for the above issue instead of parsing?
Addition: The reason I liked this method was the fact that when using swing dialouge boxes for input and then trying to convert the string to double with parse I got a NumberFormatException. It turned out that parse exclusively uses US-number formatting while Scanner can handle all formats. Scanner made the input work flawlessly even with the comma (,) decimal separator. Since the most voted up answer uses parse I really don't see how it would solve this particular problem. You would have to input your numbers in US format and then convert them to your locale format. That's rather inconvenient when ones numeric keybord is fitted with a comma.
Now you're all free to shred me to pieces ;)
You use a NumberFormat. Here is one example, which I think looks correct.
Use NumberFormat.getNumberInstance(Locale)
This should be no problem using java.text.DecimalFormat.
Do you know which locale it is? Then you can use
DecimalFormat format = DecimalFormat.getInstance(theLocale);
format.parse(yourString);
this will even work for scientific notations, strings with percentage signs or strings with currency symbols.
Here is how you use parseDouble to convert a String to a Double:
doubleExample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class doubleExample {
public static void main(String[] args) {
Double myDouble = new Double("0");
System.out.println("Please enter a number:");
try
{
//get the number from console
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
myDouble = Double.parseDouble(br.readLine());
}
//if invalid value was entered
catch(NumberFormatException ne)
{
System.out.println("Invalid value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
System.out.println("Double value is " + myDouble);
}
}

how can I parse "30.0" or "30.00" to Integer?

I using:
String str="300.0";
System.out.println(Integer.parseInt(str));
return an exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: "300.0"
How can I parse this String to int?
thanks for help :)
Here's how you do it:
String str = "300.0";
System.out.println((int) Double.parseDouble(str));
The reason you got a NumberFormatException is simply that the string ("300.00", which is a floating point number) could not be parsed as an integer.
It may be worth mentioning, that this solution prints 300 even for input "300.99". To get a proper rounding, you could do
System.out.println(Math.round(Double.parseDouble("300.99"))); // prints 301
I am amazed no one has mentioned BigDecimal.
It's really the best way to convert string of decimal's to int.
Josuha Bloch suggest using this method in one of his puzzlers.
Here is the example run on Ideone.com
class Test {
public static void main(String args[]) {
try {
java.math.BigDecimal v1 = new java.math.BigDecimal("30.0");
java.math.BigDecimal v2 = new java.math.BigDecimal("30.00");
System.out.println("V1: " + v1.intValue() + " V2: " + v2.intValue());
} catch(NumberFormatException npe) {
System.err.println("Wrong format on number");
}
}
}
You should parse it to double first and then cast it to int:
String str="300.0";
System.out.println((int)(Double.parseDouble(str)));
You need to catch NumberFormatExceptions though.
Edit: thanks to Joachim Sauer for the correction.
You can use the Double.parseDouble() method to first cast it to double and afterwards cast the double to int by putting (int) in front of it. You then get the following code:
String str="300.0";
System.out.println((int)Double.parseDouble(str));
Integer.parseInt() has to take a string that's an integer (i.e. no decimal points, even if the number is equivalent to an integer. The exception you're getting there is essentially saying "you've told me this number is an integer, but this string isn't in a valid format for an integer!"
If the number contains a decimal component, then you'll need to use Double.parseDouble(), which will return a double primitive. However, since you're not interested in the decimal component you can safely drop it by just casting the double to an int:
int num = (int)Double.parseDouble(str);
Note however that this will just drop the decimal component, it won't round the number up at all. So casting 1.2, 1.8 or 1.999999 to an int would all give you 1. If you want to round the number that comes back then use Math.round() instead of just casting to an int.

Categories

Resources