Java 解析文本文件中的一行并访问括号内的值
因此,我正在解析文本文件中的一行,并得到如下字符串:Java 解析文本文件中的一行并访问括号内的值,java,parsing,colors,text-files,background-color,Java,Parsing,Colors,Text Files,Background Color,因此,我正在解析文本文件中的一行,并得到如下字符串: color(black) 或 所以我写了一个代码: private Sprite makeSpriteToBackGround(String line) { if (line.contains("color")) { ColorsParser colorsParser = new ColorsParser(); Color color = colorsPar
color(black)
或
所以我写了一个代码:
private Sprite makeSpriteToBackGround(String line) {
if (line.contains("color")) {
ColorsParser colorsParser = new ColorsParser();
Color color = colorsParser.colorFromString(line.substring(line.indexOf("RGB")));
return new Sprite() {
@Override
public void drawOn(DrawSurface d) {
d.setColor(color);
d.fillRectangle(0, 0, 800, 600);
}
@Override
public void timePassed() {
}
};
}
因此,我的目标是检查该行是否包含“RGB”。
如果有-它将通过我的colorFromString解析器函数进行解析,如果没有,它将作为颜色进行部署
您建议我如何解析
color(black)
部分
因为RGB部件工作得很好。您可能需要先检查是否存在
RGB
,然后返回到查找表中查找已知值
Map<String, Color> colors = new HashMap<String, Color>() {{
put("black", Color.BLACK);
put("red", Color.RED);
put("green", Color.GREEN);
put("blue", Color.BLUE);
}};
Color color = null;
int rgbIndex = line.indexOf("RGB");
if (rgbIndex > -1) {
color = colorsParser.colorFromString(line.substring(rgbIndex));
} else {
// There are better ways to extract text between brackets, see https://stackoverflow.com/questions/24256478/pattern-to-extract-text-between-parenthesis/24256532
String colorName = line.substring(line.indexOf("(") + 1, line.indexOf(")"));
color = colors.get(colorName);
}
// TODO: Make sure you check if `color` is null here.
Map colors=newhashmap(){{
放置(“黑色”,颜色。黑色);
放置(“红色”,颜色为红色);
放置(“绿色”,颜色为绿色);
放置(“蓝色”,颜色。蓝色);
}};
颜色=空;
int rgbIndex=line.indexOf(“RGB”);
如果(rgbIndex>-1){
color=colorsParser.colorFromString(line.substring(rgbIndex));
}否则{
//有更好的方法可以提取括号之间的文本,请参见https://stackoverflow.com/questions/24256478/pattern-to-extract-text-between-parenthesis/24256532
字符串colorName=line.substring(line.indexOf(“”+1,line.indexOf(“”));
color=colors.get(colorName);
}
//TODO:请确保检查此处的'color'是否为空。
您可能希望首先检查是否存在RGB
,然后回退到查找表中查找已知值
Map<String, Color> colors = new HashMap<String, Color>() {{
put("black", Color.BLACK);
put("red", Color.RED);
put("green", Color.GREEN);
put("blue", Color.BLUE);
}};
Color color = null;
int rgbIndex = line.indexOf("RGB");
if (rgbIndex > -1) {
color = colorsParser.colorFromString(line.substring(rgbIndex));
} else {
// There are better ways to extract text between brackets, see https://stackoverflow.com/questions/24256478/pattern-to-extract-text-between-parenthesis/24256532
String colorName = line.substring(line.indexOf("(") + 1, line.indexOf(")"));
color = colors.get(colorName);
}
// TODO: Make sure you check if `color` is null here.
Map colors=newhashmap(){{
放置(“黑色”,颜色。黑色);
放置(“红色”,颜色为红色);
放置(“绿色”,颜色为绿色);
放置(“蓝色”,颜色。蓝色);
}};
颜色=空;
int rgbIndex=line.indexOf(“RGB”);
如果(rgbIndex>-1){
color=colorsParser.colorFromString(line.substring(rgbIndex));
}否则{
//有更好的方法可以提取括号之间的文本,请参见https://stackoverflow.com/questions/24256478/pattern-to-extract-text-between-parenthesis/24256532
字符串colorName=line.substring(line.indexOf(“”+1,line.indexOf(“”));
color=colors.get(colorName);
}
//TODO:请确保检查此处的'color'是否为空。
公共类TestColor{
公共静态void main(字符串[]args){
字符串fullColorStr=“颜色(黄色)”;
//前提条件
if(fullColorStr==null | | fullColorStr.trim().isEmpty()){
抛出新的IllegalArgumentException(“颜色不能为空”);
}
字符串colorStr=fullColorStr.substring(fullColorStr.indexOf(“”+1,fullColorStr.indexOf(“”));
颜色=getColorFromString(colorStr);
系统输出打印(彩色);
}
私有静态颜色getColorFromString(String colorStr){
//前提条件
if(colorStr==null | | colorStr.trim().isEmpty()){
返回null;
}
颜色=空;
字符串lowerColorStr=colorStr.toLowerCase();
试一试{
Field colorField=Color.class.getField(lowerColorStr);
//妄想症检查开始
类类型=colorField.getType();
if(type!=Color.class){
抛出新的IllegalStateException(“不应该遇到这个:“+colorStr”);
}//结束
color=(color)colorField.get(color.class);
}捕获(NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e){
抛出新的IllegalArgumentException(“不是有效的颜色:“+colorStr”);
}
返回颜色;
}
}
公共类TestColor{
公共静态void main(字符串[]args){
字符串fullColorStr=“颜色(黄色)”;
//前提条件
if(fullColorStr==null | | fullColorStr.trim().isEmpty()){
抛出新的IllegalArgumentException(“颜色不能为空”);
}
字符串colorStr=fullColorStr.substring(fullColorStr.indexOf(“”+1,fullColorStr.indexOf(“”));
颜色=getColorFromString(colorStr);
系统输出打印(彩色);
}
私有静态颜色getColorFromString(String colorStr){
//前提条件
if(colorStr==null | | colorStr.trim().isEmpty()){
返回null;
}
颜色=空;
字符串lowerColorStr=colorStr.toLowerCase();
试一试{
Field colorField=Color.class.getField(lowerColorStr);
//妄想症检查开始
类类型=colorField.getType();
if(type!=Color.class){
抛出新的IllegalStateException(“不应该遇到这个:“+colorStr”);
}//结束
color=(color)colorField.get(color.class);
}捕获(NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e){
抛出新的IllegalArgumentException(“不是有效的颜色:“+colorStr”);
}
返回颜色;
}
}
我想知道为什么其他评论者都没有提到这一点,但有一种既定的方法可以解决这类问题:正则表达式
您描述了两种模式:
- 命名模式:
()color\(\w+)
- RGB图案:
()color\(RGB\(\d+)、(\d+)、(\d+)\)
包装颜色;
导入java.awt.Color;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类颜色分析器{
私有最终模式名为Pattern=Pattern.compile(“color\\(\\w+\\)”);
私有最终图案rgbPattern=Pattern.compile(“颜色\(RGB\\(\\d+\,(\\d+\,(\\d+)\,(\\d+)\)\”;
公共颜色解析(字符串行){
颜色结果=解析RGB(行);
如果(结果==null){
结果=解析命名(行);
}
返回结果;
}
专用颜色解析RGB(字符串行){
最终匹配器匹配器=rgbPat
public class TestColor {
public static void main(String[] args) {
String fullColorStr = "color(yellow)";
//Precondition
if (fullColorStr == null || fullColorStr.trim().isEmpty()) {
throw new IllegalArgumentException("Color cannot be empty");
}
String colorStr = fullColorStr.substring(fullColorStr.indexOf("(")+1,fullColorStr.indexOf(")"));
Color color = getColorFromString(colorStr);
System.out.print(color);
}
private static Color getColorFromString(String colorStr) {
//Precondition
if (colorStr == null || colorStr.trim().isEmpty()) {
return null;
}
Color color = null;
String lowerColorStr = colorStr.toLowerCase();
try {
Field colorField = Color.class.getField(lowerColorStr);
//Paranoid check start
Class<?> type = colorField.getType();
if (type != Color.class) {
throw new IllegalStateException("Shoudln't have encountered this : " + colorStr);
} //End
color = (Color) colorField.get(Color.class);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new IllegalArgumentException("Not a valid color : " + colorStr);
}
return color;
}
}