无法解析^字符 [英] Unable to parse ^ character
问题描述
好的,现在我能够解析空间,这是我以前的问题。现在我的解析器已经准备就绪但有一个我无法弄清楚的缺陷。
Okay, Now I am able to parse space which was my previous problem. Now my parser is almost ready but has a defect which I am unable to figure out.
我能够在段之后检索数据(参见代码)和管道之间的数据。
我无法获得的是升级并检索管道之间包含的数据,并由^分隔。
I am able to retrieve Data after segments(see code) and data in between pipes. What I am not able to get to is a level up and retrieve data which is contained between pipes and seperated by ^.
例如。
Input String is A|1|2|3^4|
Expected Output
element1 A
element2 1
element3 2
element4.1 3
element4.2 4
但是,我当前的输出将是
However, My current output is coming to be
element1 A
element2 1
element3 2
element4 3^4
我收到异常
1 [Ljava.lang.String; @ 1786e64带有各种ID @
I am recieving exception 1 [Ljava.lang.String;@1786e64 with various ids after @
下面给出的注释代码会产生问题。
The commented code given below is creating problem.
代码:
Scanner scanner = new Scanner(System.in);
str=scanner.nextLine();
System.out.println(str);
segments="(A)|(B)|(C)|(D)";
pipe="[\\s+\\|+\\+]";
carat="[\\^+]";
for(i=0;i<token_type1.length;i++)
{
token_type2=token_type1[i].toString().split(pipe);
for(j=0;j<token_type2.length;j++)
{
/*
token_type3=token_type2.toString().split(carat);
for(k=0;k<token_type3.length;k++)
System.out.println("\t"+(k+1)+" "+token_type3[k]);
*/
System.out.println((j+1)+"\t"+token_type2[j]);
}
System.out.println();
}
请告知。
推荐答案
这会将您的输入转换为您想要的输出(将类保存在两个不同的文件中)
This will transform your input to your desired output (save the classes in two different files)
Parser.java
Parser.java
public class Parser {
public static final String ELEMENT_DELIM_REGEX = "\\|";
public static void main(String[] args) {
String input = "A|1|2|3^4|";
String[] tokens = input.split(ELEMENT_DELIM_REGEX);
Element[] elements = new Element[tokens.length];
for (int i = 0; i < tokens.length; i++) {
elements[i] = new Element(i + 1, tokens[i]);
}
for (Element element : elements) {
System.out.println(element);
}
}
}
和
Element.java
Element.java
public class Element {
public static final String SUB_ELEMENT_DELIM_REGEX = "\\^";
private int number;
private String[] content;
public Element(int number, String content) {
this.number = number;
this.content = content.split(SUB_ELEMENT_DELIM_REGEX);
}
@Override
public String toString() {
if (content.length == 1) {
return "Element " + number + "\t" + content[0];
}
StringBuilder str = new StringBuilder();
for (int i = 0; i < content.length; i++) {
str.append("Element " + number + "." + (i+1) + "\t" + content[i] + "\n");
}
// Delete the last \n
str.replace(str.length() - 1, str.length(), "");
return str.toString();
}
}
这篇关于无法解析^字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!