# Thread: Error in calculation somewhere

1. Member
Join Date
Oct 2011
Location
Tromsø
Posts
54
Rep Power
0

## Error in calculation somewhere

this ain't one school project or anything, it's for myselfh and my clan members in TribalWars.net

A program to calculate how many noblemen I can produce here is how it workks

Java Code:
```Price for next nobleman:	75 x 28000, 30000, 25000	2.100.000 2.250.000 1.875.000
Stored resources:	1128 x 31.584.000 33.840.000 28.200.000```
the noblemen after this will have the multiplier by 76 and it's multiplied by this
Java Code:
```		int priceWood = 28000;
int priceClay = 30000;
int priceIron = 25000;```

Java Code:
```import java.util.Scanner;
import java.util.stream.Stream;

class NobleMan {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int priceWood = 28000;
int priceClay = 30000;
int priceIron = 25000;

int wood, clay, iron = 0;
int gotWood, gotClay, gotIron = 0;

int nobleman = 0;

System.out.println("Type in what the next nobleman will cost you");

System.out.println("Wood Price");
wood = in.nextInt();
System.out.println("Clay Price");
clay = in.nextInt();
System.out.println("Iron Price");
iron = in.nextInt();

System.out.println("Type in what you got of the packages");
System.out.println("What you got in Wood");
gotWood = in.nextInt();
System.out.println("What you got in Clay");
gotClay = in.nextInt();
System.out.println("What you got in Iron");
gotIron = in.nextInt();

while (gotWood > wood && gotClay > clay && gotIron > iron){

nobleman++;

gotWood -=priceWood;
gotClay -=priceClay;
gotIron -=priceIron;

wood +=priceWood;
clay +=priceClay;
iron +=priceIron;

} // end while

System.out.print("With theese resources you can make "+nobleman+" Noblemen" );
in.close();

}

}```
when i'm running the code i'm getting that i can make over 500 noblemen and that is wrong.
So the program compiles but the calculation is wrong.

2. ## Re: Error in calculation somewhere

Can you post the program's output that shows the problem?
Add some comments showing what is wrong and explain what the output should be.

3. Member
Join Date
Oct 2011
Location
Tromsø
Posts
54
Rep Power
0

## Re: Error in calculation somewhere

Java Code:
```Type in what the next nobleman will cost you
Wood Price
28000
Clay Price
30000
Iron Price
25000
Type in what you got of the packages
What you got in Wood
28000
What you got in Clay
30000
What you got in Iron
25000
With theese resources you can make 1 Noblemen```
If it were just one, then thatone is working.
Java Code:
```Type in what the next nobleman will cost you
Wood Price
2100000
Clay Price
2250000
Iron Price
1875000
Type in what you got of the packages
What you got in Wood
31584000
What you got in Clay
33840000
What you got in Iron
28200000
With theese resources you can make 527 Noblemen```
Thisone just is wrong.

4. ## Re: Error in calculation somewhere

Thisone just is wrong.
What should the output be?

A hint for easier testing and debugging:
Use the Scanner class's constructor that takes a String for its input:
Java Code:
`   Scanner in = new Scanner("2100000 2250000 1875000 31584000 33840000 28200000"); //System.in);`
That will make sure everyone that tests the code is using the exact same input AND will keep them from having to type in all those numbers.
Last edited by Norm; 06-20-2014 at 10:34 PM.

5. Member
Join Date
Oct 2011
Location
Tromsø
Posts
54
Rep Power
0

## Re: Error in calculation somewhere

i don't know the exact number, but under 20.

6. ## Re: Error in calculation somewhere

A hint for easier testing and debugging:
Use the Scanner class's constructor that takes a String for its input:
Java Code:
`   Scanner in = new Scanner("2100000 2250000 1875000 31584000 33840000 28200000"); //System.in);`
That will make sure everyone that tests the code is using the exact same input AND will keep them from having to type in all those numbers.

7. Member
Join Date
Oct 2011
Location
Tromsø
Posts
54
Rep Power
0

## Re: Error in calculation somewhere

Here is an screenshot just taken from my account
Gyazo - 37871646a4441475a57d0732cdfa33af.png
When I've made one nobleman then the multiplier will rise by 1. it's not static numbers either. constantly rising. hte only thing that is static is this

Java Code:
```        int priceWood = 28000;
int priceClay = 30000;
int priceIron = 25000;```
but this is also what is beeing multiplied all the time. this is the cost of the first nobleman, the seccond is the double of this, and then so on.

8. ## Re: Error in calculation somewhere

Is the input correct? Different input will give different values.
It seems that there is a very large number of different inputs for the program.

How are you trying to debug the code?
i don't know the exact number
But if you don't know what the output is supposed to be, you will never know if the code is working correctly. You need to create a set of inputs and manually create the output for each set of inputs before testing the code to see if it produces the same output as you have computed.

Another hint for testing: Remove the ending 3 zeros (000) from the numbers so it is easier to see the values. Numbers ending in 5 and 6 zeros are hard to keep track of.
Last edited by Norm; 06-20-2014 at 10:59 PM.

9. Member
Join Date
Oct 2011
Location
Tromsø
Posts
54
Rep Power
0

## Re: Error in calculation somewhere

the high numbers shall be there, part of the game. but thanks for the help so far, no need to reply anymore since you are working on the input instead of the algorithm. for the next person want to have a look on this here is a few screenshots that might be to help how the calculation goes.
Gyazo - 7763c61cd5515d9a03ce218fb5a96c9f.png
Gyazo - 8961ed379b4515d47b458cc2b21ca81b.png
Gyazo - d76bd045cae0d240937ac19b1e137830.png
Gyazo - e39bfeb5ffd5145d5b1aef396ff2cf95.png
Gyazo - b142238e1effceafb91d5f2581e90b7a.png

is showing that the number of nobleman should not be an high number.
as you see the "high numbers" is rapidly decreasing by only making 4 noblemen there.

to reserve myselfh, if you feel to going on the input, explain why atleast there is something wrong there.

update, by making these 4 noblemen when i'm now running the program i'm down to 388 that i can make.
Last edited by Juukamen; 06-20-2014 at 11:22 PM.

10. ## Re: Error in calculation somewhere

You don't want to do what I have suggested for finding what is wrong with your program so I'm giving up.
Ok, you can do it your way.

Good luck.

Sorry, I don't look at screen shots on other sites.
Last edited by Norm; 06-20-2014 at 11:39 PM.

11. Member
Join Date
Oct 2011
Location
Tromsø
Posts
54
Rep Power
0

## Re: Error in calculation somewhere

Problem solved =)
Java Code:
```import java.util.Scanner;
import java.util.stream.Stream;

class NobleMan {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int priceWood = 28000;
int priceClay = 30000;
int priceIron = 25000;

int wood, clay, iron = 0;
int gotWood, gotClay, gotIron = 0;

int nobleman = 0;

System.out.println("Type in what the next nobleman will cost you");

System.out.println("Wood Price");
wood = in.nextInt();
System.out.println("Clay Price");
clay = in.nextInt();
System.out.println("Iron Price");
iron = in.nextInt();

System.out.println("Type in what you got of the packages");
System.out.println("What you got in Wood");
gotWood = in.nextInt();
System.out.println("What you got in Clay");
gotClay = in.nextInt();
System.out.println("What you got in Iron");
gotIron = in.nextInt();

while (gotWood >= wood && gotClay >= clay && gotIron >= iron){

nobleman++;

gotWood -=wood; // theese 3 lines were the "mistake" =)
gotClay -=clay;
gotIron -=iron;

wood +=priceWood;
clay +=priceClay;
iron +=priceIron;

} // end while

System.out.print("With theese resources you can make "+nobleman+" Noblemen" );
in.close();

}

}```

Here is the output, high numbers and it's correct this time also.
Java Code:
```Type in what the next nobleman will cost you
Wood Price
2212000
Clay Price
2370000
Iron Price
1975000
Type in what you got of the packages
What you got in Wood
26796000
What you got in Clay
28710000
What you got in Iron
23925000
With theese resources you can make 11 Noblemen```
As my gut said, no wrong with the input, but the calculation. Why fight the "customer" pal ?

12. ## Re: Error in calculation somewhere

What did you change in the program so that it generates the results you want?

13. Member
Join Date
Oct 2011
Location
Tromsø
Posts
54
Rep Power
0

## Re: Error in calculation somewhere

Old Code
Java Code:
```            gotWood -=priceWood;
gotClay -=priceClay;
gotIron -=priceIron;```
New Code
Java Code:
```            gotWood -=wood; // theese 3 lines were the "mistake" =)
gotClay -=clay;
gotIron -=iron;```
I should subtract what is costed to make the last nobleman, instead of what what it cost's to store one single package.

14. ## Re: Error in calculation somewhere

Thanks for the explanation.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•