-2

I'm working on a lesson to create a vacation plan. When I write code like

double money1 =  money / days;
money1 =  money*100;
money1 = (int) money1/ 100.0;

IntelliJ underlines money / days and notifies me that it's redundant.

enter image description here

Why does it happen? How can I make it not redundant?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Lokian
  • 93
  • 11
  • What error are you getting? What is your expected output? – achAmháin Dec 18 '17 at 00:05
  • THe output should be : How many days are you going to spend traveling? 14 How much money, in USD, are you planning to spend on your trip? 2300 What is the three letter currency symbol for your travel destination?MXC How many MXC are there in 1 USD? 19.8 If you are traveling for 14 that is the same as 336 hours or 20160 minutes If you are going to spend $2300 USD that means per day you can spend up to 164.28 USD (/sorry, I don;t know how to correcly format the answer) – Lokian Dec 18 '17 at 00:07
  • 1
    Yet another `"I've searched everywhere..."` type question. Please avoid such worthless statements and instead **show** exactly what you **have** found. Those statements don't help clarify your problem, but your showing what you've found and how it doesn't help you **does**. – Hovercraft Full Of Eels Dec 18 '17 at 00:08
  • 1
    So your logic is wrong and you want someone to fix it? – achAmháin Dec 18 '17 at 00:09
  • Hey guys, I'm new here, It's the first time I make a question here. Sorry for something. – Lokian Dec 18 '17 at 00:12
  • Yes, you should probably go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to help you improve your current and future questions, which can help you get better answers. Better to do this *before* asking your first question, but better late than never. – Hovercraft Full Of Eels Dec 18 '17 at 00:13
  • I hope It's better now. I was just excited, sorry. @HovercraftFullOfEels – Lokian Dec 18 '17 at 02:41
  • At `double money1 = money / days;` your IDE says `money / days;` is redundant because immediately after it you execute `money1 = money * 100;` which replaces previously calculated value with new value. Since you never used previous value calculating it doesn't make much sense. So `double money1 = money / days;` is effectively the same as `double money1;` – Pshemo Dec 19 '17 at 18:30
  • I've modified it so: `double money1 = (money / days) * 100;` , `money1= (int) money1;` , `money1/= 100.0;` . – Lokian Dec 20 '17 at 06:39

1 Answers1

0

You're immediately reassigning the variable, so your calculation is lost.

Observe:

double money1 =  money / days;
money1 =  money*100; // <- Quotient is discarded and overwritten
money1 = (int) money1/ 100.0; <- Prior product is discarded and overwritten

This is what IntelliJ is warning you about; this smells very much like a bug. I can't reliably tell you which of these statements to change since they do very different things, but this is something you should look to debug in your logical flow.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • I've found the mistake, I used _int_ instead of _double_ when asking for the input, so: replaced: `int money = input.nextInt();` for: `double money =input.nextDouble();`Also, fixed the part of the code that you said to: `double money1 = (money / days) * 100;` `money1= (int) money1;` `money1/= 100.0;` – Lokian Dec 20 '17 at 06:42