java 缩进_解析Java中的缩进文本树

我有一个缩进文件,需要使用java进行解析,我需要某种方式将其放在Section类中,如下所示

root

root1

text1

text1.1

text1.2

text2

text2.1

text2.2

root2

text1

text1.1

text1.2

text2

text2.1

text2.2.2

我有上课的地方像缩进的东西

public class Section

{

private List children;

private String text;

private int depth;

public Section(String t)

{

text =t;

}

public List getChildren()

{

if (children == null)

{

children = new ArrayList();

}

return children;

}

public void setChildren(List newChildren)

{

if (newChildren == null) {

children = newChildren;

} else {

if (children == null) {

children = new ArrayList();

}

for (Section child : newChildren) {

this.addChild(child);

}

}

}

public void addChild(Section child)

{

if (children == null) {

children = new ArrayList();

}

if (child != null) {

children.add(child);

}

}

public String getText()

{

return text;

}

public void setText(String newText)

{

text =newText;

}

public String getDepth()

{

return depth;

}

public void setDepth(int newDepth)

{

depth = newDepth;

}

}

我需要一些方法来解析文件并将其放在预期的结果中,这是我们的Section对象,如下所示

Section=

Text="Root"

Children

Child1: Text= "root1"

Child1: "text1"

Child1="Text 1.1"

Child2="Text 1.2"

Child2: "text2"

Child1="Text 2.1"

Child2="Text 2.2"

Children

Child2: Text= "root2"

Child1: "text1"

Child1="Text 1.1"

Child2="Text 1.2"

Child2: "text2"

Child1="Text 2.1"

Child2="Text 2.2"

Here is some code that I have started

int indentCount=0;

while(String text = reader.readline()

{

indentCount=countLeadingSpaces(String word);

//TODO create the section here

}

public static int countLeadingSpaces(String word)

{

int length=word.length();

int count=0;

for(int i=0;i

{

char first = word.charAt(i);

if(Character.isWhitespace(first))

{

count++;

}

else

{

return count;

}

}

return count;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值