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.
Related
I have the below requirement:
input String = "1.00000" should be converted to int ( because actually no fraction at all )
input String = "1" should be converted to int 1
however, input String ="1.0001" should be an Exception (because has fraction )
Previously I was assuming Integer.valueOf("1.00000") should return an integer.However it returns NumberFormatException.
any Java library that can solve this, or at least can check if 1.0000 is actually an integer so I can safely parse as Double and cast it to int?
BigDecimal class
Java itself has a library that does exactly this: BigDecimal
Example:
private static int parse(String input) throws ArithmeticException {
return new BigDecimal(input).intValueExact();
}
The intValueExact method throws an ArithmeticException if the BigDecimal object has a non-zero fractional part.
One way to tackle the problem is, you can use regex to first ensure that the input exactly matches the format of a number with fractional part being optional and if at all then all being zeroes and if yes, then go ahead and parse it else throw Exception or whatever you want to do. Check this Java code,
List<String> list = Arrays.asList("1.00000","1","1.0001");
Pattern intPat = Pattern.compile("(\\d+)(\\.0+)?");
list.forEach(x -> {
Matcher m = intPat.matcher(x);
if (m.matches()) {
int num = Integer.parseInt(m.group(1));
System.out.println(num);
} else {
System.out.println(x +" is not a pure Integer"); // Throw exception or whatever you like
}
});
Outputs,
1
1
1.0001 is not an Integer
Also, as long as the numbers you are working with are confined within integer range, its all good but to ensure the numbers don't cross integer limit, you may want to use a different quantifier.
Another alternate simpler solution you can go for is, parse the number as double and compare it with its int form and if both are equal then it is a pure integer else not. I'll prefer this than my first solution using regex. Java code,
List<String> list = Arrays.asList("1.00000","1","1.0001");
list.forEach(x -> {
double num = Double.parseDouble(x);
if (num == (int)num) {
System.out.println((int)num);
} else {
System.out.println(x + " is not a pure Integer");
}
Prints,
1
1
1.0001 is not a pure Integer
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));
}
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);
}
}
Right now, I am making a simple tic-tac-toe project. I would like to know what happens to the integer i when:
string s = "hello"; //or something else, non integer
int i = Integer.parseInt(s);
What will i be equal to?
Integer.parseInt("hello") statement will throw an exception: java.lang.NumberFormatException
If the given string does not contain a parseable integer, a
NumberFormatException will be thrown.
For more information, see: https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29
I'm struggling with the following code which supposed to compare numbers fractions and output some text to user informing him whether the fractions of numbers are the same, i.e.
User inputs two doubles: 3.14 and 4.14
Output: Fractions are the same
User input two double: 3.14 and 4.15
Output: Franctions are not the same
Somehow I've managed to compile the following code but once I tried to run it in Eclipse IDE I came accross the following notifications:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at comparison.main(comparison.java:12)
import java.util.Scanner;
class comparison
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double a,b;
a = scan.nextDouble();
b = scan.nextDouble();
compare(a,b);
}
public static void compare(double n, double m)
{
if(n- Math.floor(n) == m - Math.floor(m))
System.out.println("Fractions are the same");
else
System.out.println("Fractions are no the same");
}
}
I would love to obtain any proper explanation of my problems, I guess there are variety of different ways to solve this case in different way but can you please stick with my idea and help me out with this ?
Thanks in advance!
try
Double.toString (n).split ("\\.")[1].equals(Double.toString (m).split ("\\.")[1])
but to fix your problem change to
a = scan.nextDouble();
scan.nextLine (); // consume CR
b = scan.nextDouble();
The documentation for InputMismatchException states:
Thrown by a Scanner to indicate that the token retrieved does not
match the pattern for the expected type, or that the token is out of
range for the expected type.
So, basically, your scanner is trying to get a double, and you apparently entered something that either was not a double or was out of the range of a double.
The best way to handle this would probably be to get Strings from the scanner, and then check to see if they can be converted to doubles. Output an error message if not.
Edit - in agreement with #scary-wombat - use BigDecimal - it has a constructor that takes a String. Be sure to catch NumberFormatException, which will be thrown if what you enter cannot be converted.