for (int i = 0; i < 18; i++) {
line = file.readLine();
String[] word = line.split(";");
appartment[i] = new Appartment();
appartment[i].floor= Integer.parseInt(word[0]);
appartment[i].name = word[1];
appartment[i].money= Double.parseDouble(word[2]);
appartment[i].owner= word[3];
}
Could someone tell my why this is not working? Im reading from a file. I am trying to convert money from string to double, but it says
possible loss of precision.
required: int
found: double
I need doubles so the owners account also can go negative.
- It seems that appartment
is an Array of type Appartment
, where the Appartement's object field named money
is in int
type.
- But you are assingnig it the value as double
type, so you will need an explicit cast from double to int,
Eg:
appartment[i].money= (int) Double.parseDouble(word[2]);
Apparently appartment[i].money= Double.parseDouble(word[2]);
is a problem here. If the type of money is int
, either you should cast the value of word[2] to int
like appartment[i].money = (int(Double.parseDouble(word[2]));
or you should parse as an integer something like appartment[i].money= Integer.parseInt(word[2]);
用这个,
appartment[i].money = Integer.parseInt(word[2]);
您可能在辩解中认为金钱是整数,请尝试将其更改为两倍...
The best practice when representing money in Java is to use BigDecimal. See: http://www.javapractices.com/topic/TopicAction.do?Id=13 for more details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.