public class CMYKtoRGB {
public static void main(String[] args) {
String cyan = (args[0]); // Here would be the value 0.0
String magenta = (args[1]);
String yellow =(args[2]);
String black =(args[3]);
System.out.println (cyan); // There is more code but this is the important.
/*The errors are:
Exception in thread "main" java.lang.NumberFormatException: For input string: "0.0"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:665)
at java.base/java.lang.Integer.parseInt(Integer.java:781)*/
I tried to use double or other formats but I can´t do it. This is for a homework but I dont know how to do it. I am really stressed with this.
CodePudding user response:
You are trying to use the method Integer::parseInt
. This method takes in a String
and returns an int
. An int
stands for the mathematical concept of an Integer. In short, an Integer is a WHOLE NUMBER, and therefore, NO DECIMAL POINT.
So, if you try to give a decimal point to a function that is trying to create a whole number, it will throw the exception that it gave you.
Did you instead mean Double::parseDouble
? This method would allow you to use a decimal point because it takes in a String
and returns a double
. A double
stands for the mathematical concept of a Floating point number, which is the long way of saying decimal numbers for computers.
As we can see, it works.
final String input = "0.1";
final double output = Double.parseDouble(input);
System.out.println(output);
0.1
CodePudding user response:
I recommend using new BigDecimal()
instead of Double
or Float
since you should get used to it anyway for precision decimal numbers in Java. Double and float are more for "quick and dirty" approximate arithmetic with decimal numbers.