Java parsing text

Source: Internet
Author: User

ImportJava.io.*;ImportJava.util.*; Public classFileManager { PublicList<dishes>parsefile (file file) {String str=NULL; List<Dishes> list =NewArraylist<dishes>(); Try{bufferedreader br=NewBufferedReader (Newfilereader (file));  while(str = br.readline ())! =NULL) {    if(Str.startswith ("Name:")) {       intindex = Str.indexof (":"); String Dishname= Str.substring (index+1, Str.length ()); STR=Br.readline (); Index= Str.indexof (":"); String categoriesstring= Str.substring (index+1, Str.length ()); String[] Catearr= Categoriesstring.split (","); Set<String> categories =NewHashset<string>();  for(intj = 0;j<catearr.length;j++) {Categories.add (catearr[j]); } Set<String> Ingredients =NewHashset<string>(); String SS=Br.readline ();  while(str = br.readline ())! =NULL&&! (str.length () = = 0) ) {string[] Strarr= Str.split ("");  for(inti = 0;i<strarr.length;i++) {Ingredients.add (strarr[i]); }} Dishes D=Newdishes ();       D.setname (Dishname);       D.setcategories (categories);              D.setingredients (ingredients);    List.add (d); }   }  } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }  returnlist;}  Public Static voidMain (string[] args) {//TODO auto-generated Method StubFileManager FM =NewFileManager (); List<Dishes> list =NewArraylist<dishes>(); File File=NewFile ("D:/dishes.txt"); List=fm.parsefile (file);  for(inti = 0; I<list.size (); i++) {System.out.println (List.get (i). GetName ());       System.out.println (List.get (i). GetCategories ());      System.out.println (List.get (i). Getingredients ()); } }}//An instance of a dish that corresponds to one of the dishes.classDishes {PrivateString name;PrivateSet<string> Categories;//Cuisine Category PrivateSet<string> ingredients;//Food Ingredients  PublicString GetName () {returnname;}  Public voidsetName (String name) { This. Name =name;}  PublicSet<string>getcategories () {returnCategories;}  Public voidSetcategories (set<string>categories) {Categories=categories;}  PublicSet<string>getingredients () {returnIngredients;}  Public voidSetingredients (set<string>ingredients) {   This. INGREDIENTS =ingredients;}}
 using Java to parse the following complex text, which is a txt text file of recipes, the following is only a list of two recipes, The remaining formats are the same. As long as the name,categories can be parsed separately, and the following ingredients, stored in an object can be. Please give the detailed code of the parsing step 

Heroes, please, then you must add the highest score.

Below is the text format of the recipe

Name:yu xiang rou si
categories:pork, Chinese

g pork g sugar
1 0 g chilly 1 g salt
G oil

in large bowl, blend oil and sugars on low until well mixed. ADD Chilly.
Beat in Salt ...

-----

Name:xi Hong Shi Chao ji Dan
Categories:tomato, Chinese

g Tomato g Suga R
5 G oil 2-g salt

Chop tomatoes, blend oil and sugars on low until well mixed.
Add some salt ...

-----
The ingredients are not certain, for example
2 sugar C Brown 2 ts cloves
2 c Sour milk 2 ts Nutmeg
4 c Flour 3 ea Eggs, LG
3/4 C Butter oz raisins
2 C Jam 1 c nutmeats
2 ts Baking soda 1 x Figs and dates as desired
This should also require parsing at least one array or list

Http://zhidao.baidu.com/link?url=eZtAHGCL3EM3OrpBdzKj6LAWnxZSQkG7PzUqZ7BT347wuyuI_VKCv34Mz7j6XsC0CVf17lo4OJ2oYvmV36rvaq

Java parsing text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.