0

I have a file formated .TXT like this :

[Quest]
Name: The Adventure
Creator: Darius
Reward: 0g

[@New Monster]
MonsterID: 100
MonsterHP: 23987
MonsterLv: 11
MonsterDmg: 133
MonsterType: Magic

[@New Monster]
MonsterID: 101
MonsterHP: 21236
MonsterLv: 8
MonsterDmg: 95
MonsterType: Physical

[@New Item]
ItemID: 222
ItemDamage: 88
ItemType: Robe

How can i parse this file so i can get the monsterid,monsterhp etc and itemId,itemdamage etc. There is two [@New Monsters]

  • Is there a reason beyond learning why you are re-inventing file formats? If not, using a standard format makes it easier to edit, read, and use programmatically. – Ryan Leach Jun 26 '17 at 04:46
  • Do you save the text in the file by yourself? If yes, you can write a class as data model for Monster, Quest, Item and serialize/deserialize it as JSON in your txt file. For example how easy it is to get a JSON string from an object look here: https://stackoverflow.com/a/16607559/5056173 – Sean Stayns Jun 26 '17 at 04:47
  • 1
    What is your code so far? – Ori Marko Jun 26 '17 at 04:47
  • @RyanTheLeach I wanted to know how to parse this code. – Darius Ellert Klaus Jun 26 '17 at 04:51
  • @SeanStayn i made this file so people can create custom quest in my game, people create the file, so my program parse it as a quest in my game. – Darius Ellert Klaus Jun 26 '17 at 04:51
  • @user7294900 i only can parse the first tag "[Quest]" about the other tag, i cant solve it now. – Darius Ellert Klaus Jun 26 '17 at 04:51
  • @DariusEllertKlaus "i only can parse the first tag "[Quest]" about the other tag, i cant solve it now." That's fine, show us what you have done, it shows you have put some effort in and can provide insight into what you mis-understand. – Ryan Leach Jun 26 '17 at 04:56
  • "i made this file so people can create custom quest in my game" - so why not use JSON or XML, which will be more familiar to other people, and allow you to use existing parsers? – Jon Skeet Jun 26 '17 at 04:59
  • @JonSkeet i dont want to use json or xml, i still learning this thing, as i said about, i wanted to know how to parse this code. – Darius Ellert Klaus Jun 26 '17 at 05:06
  • @RyanTheLeach my code is very bad, its even cant be called parse :( – Darius Ellert Klaus Jun 26 '17 at 05:06
  • 2
    You can use `java.util.Scanner` to parse file manually. When you read line `[@New Monster]` create object of class `Monster` and populate it with following properties – Jay Smith Jun 26 '17 at 05:12

0 Answers0