Професионална програма
Loading...
+ Нов въпрос
vbmarinova avatar vbmarinova 0 Точки

Pirates JAVA fundamentals

Здравейте!

Някой може ли да помогне с кода на задачата?

Дава ми грешка Null Pointer Exception.

Pastebin:https://pastebin.com/SB9iHrR1

Условие:

Description

Until the "Sail" command is given you will be receiving:

  • Cities that you and your crew have targeted, with their population and gold, separated by "||".
  • If you receive a city which has been already received, you have to increase the population and gold with the given values.

After the "Sail" command, you will start receiving lines of text representing events until the "End" command is given. 

Events will be in the following format:

  • "Plunder=>{town}=>{people}=>{gold}"
    • You have successfully attacked and plundered the town, killing the given number of people and stealing the respective amount of gold. 
    • For every town you attack print this message: "{town} plundered! {gold} gold stolen, {people} citizens killed."
    • If any of those two values (population or gold) reaches zero, the town is disbanded.
      • You need to remove it from your collection of targeted cities and print the following message: "{town} has been wiped off the map!"
    • There will be no case of receiving more people or gold than there is in the city.
  • "Prosper=>{town}=>{gold}"
    • There has been a dramatic economic growth in the given city, increasing its treasury by the given amount of gold.
    • The gold amount can be a negative number, so be careful. If a negative amount of gold is given print: "Gold added cannot be a negative number!" and ignore the command.
    • If the given gold is a valid amount, increase the town's gold reserves by the respective amount and print the following message: "{gold added} gold added to the city treasury. {town} now has {total gold} gold."

Input

  • On the first lines, until the "Sail" command, you will be receiving strings representing the cities with their gold and population, separated by "||"
  • On the next lines, until the "End" command, you will be receiving strings representing the actions described above, separated by "=>"

Output

  • After receiving the "End" command if there are any existing settlements on your list of targets, you need to print all of them, sorted by their gold in descending order, then by their name in ascending order, in the following format:

Ahoy, Captain! There are {count} wealthy settlements to go to:

{town1} -> Population: {people} citizens, Gold: {gold} kg

   …

{town…n} -> Population: {people} citizens, Gold: {gold} kg

  • If there are no settlements left to plunder, print:

"Ahoy, Captain! All targets have been plundered and destroyed!"

Constraints

  • The initial population and gold of the settlements will be valid, 32-bit integers,
    will never be negative or exceed the respective limits.
  • The town names in the events will always be valid towns that should be on your list.

 

Тагове:
0
Fundamentals Module
svephoto avatar svephoto 402 Точки

Здравей, vbmarinova,

 

Най-напред ти предлагам да зададеш и в двата мап-а стойността да приема Integer - Long не ти е нужен. Махаш тези два реда (16-ти и 17-ти) в първия while цикъл, понеже ти хвърля java.lang.NullPointerException точно на 16-ти ред:

 

long newPopulation = populationMap.get(city) + population;
int newGold = gold + goldMap.get(city);

 

При първото влизане и двата мап-а все още са празни и няма как да извикаш get метода - нямаш нищо, което да get-неш като ключ, който да ти върне стойност.

Обърни внимание точно как пълниш мап-овете в if-else проверката, защото и при if, и при else в момента правиш едно и също. Задай на 36-ти ред тип int на мястото на Long. Виж точно как изпълняваш нещата според условието при команда Plunder и дали всичко работи коректно - с точния изход, отпечатан на конзолата когато трябва.

След тези корекции и правилно разписани действия/команди, би трябвало да си вече ОК за 100/100, но ако отново имаш затруднения и/или проблеми, пиши пак. Ще си дебъгнеш и ще видиш къде, какво и как се случва.

 

Успех!

0
01/08/2020 15:24:43
vbmarinova avatar vbmarinova 0 Точки

Много ти благодаря!

0
svephoto avatar svephoto 402 Точки

Моля!

0