Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印出在字符串中找到字符的行号_Java - Fatal编程技术网

Java 打印出在字符串中找到字符的行号

Java 打印出在字符串中找到字符的行号,java,Java,我正在编写一个程序来扫描给定字符串中的数学运算符,当程序找到匹配项时,我想返回运算符及其所在的行,下面是我的尝试 public class Testing { public enum TokenType { OP_MULTIPLY, OP_DIVIDE, OP_MOD, OP_ADD, OP_SUBTRACT, OP_LESS, OP_LESSEQUAL,

我正在编写一个程序来扫描给定字符串中的数学运算符,当程序找到匹配项时,我想返回运算符及其所在的行,下面是我的尝试

public class Testing {

    public enum TokenType {

        OP_MULTIPLY,
        OP_DIVIDE,
        OP_MOD,
        OP_ADD,
        OP_SUBTRACT,
        OP_LESS,
        OP_LESSEQUAL,
        OP_GREATER,
        OP_GREATEREQUAL,
        OP_EQUAL,
        OP_NOTEQUAL,
        OP_NOT,
        OP_ASSIGN,
        OP_AND,
        OP_OR,
        OP_DOT

    }

    public static String inputString = "public class HelloWorld {\n"
            + " public static void-*//-- main(String[] args) {\n" + " // to print out+8+*+ hello world\n"
            + " System.out.println(\"Hello World!\");\n" + " }\n" + "}\n";

    public static void lineNumber(String lineNumber) {
        int count = 1;

        String[] lines = inputString.split("\\r?\\n");
        for (String line : lines) {
            System.out.println("line " + count++);
        }

        int n = inputString.length();
        for (int i = 0; i < n; i++) {
            char ch = inputString.charAt(i);
            getOP(ch);

        }
    }

    public static TokenType getOP(char ch) {

        switch (ch) {
        case '+':
            System.out.println(TokenType.OP_ADD + ", " + ch);
            return TokenType.OP_ADD;

        case '-':
            System.out.println(TokenType.OP_SUBTRACT + ", " + ch);
            return TokenType.OP_SUBTRACT;

        case '/':
            System.out.println(TokenType.OP_DIVIDE + ", " + ch);
            return TokenType.OP_DIVIDE;

        case '*':
            System.out.println(TokenType.OP_MULTIPLY + ", " + ch);
            return TokenType.OP_MULTIPLY;
        }

        return null;

    }

    public static void main(String[] args) {
        lineNumber(inputString);
    }
}
我想要达到的目标是:

Line 1: OP_ADD, +
Line 3: OP_MULTIPLY, *
etc.

谢谢

您应该检查每一行的操作,而不是一次检查整个输入

public static void lineNumber (String inputString)
{
    int count = 1;

    String[] lines = inputString.split("\\r?\\n");
    for (String line : lines) {
        System.out.println("line " + count++);
        int n = line.length();
        for (int i = 0; i < n; i++)
        {
            char ch = line.charAt(i);
            getOP(ch);

        }
    }
}
publicstaticvoidlinenumber(stringinputstring)
{
整数计数=1;
字符串[]行=inputString.split(\\r?\\n”);
用于(字符串行:行){
System.out.println(“行”+count++);
int n=line.length();
对于(int i=0;i
您的代码可能如下所示:

公共类测试{
枚举标记类型{
运算乘法,
OP_DIVIDE,
OP_MOD,
补充:,
OP_减法,
无操作性,
OP_LESSEQUAL,
奥普大酒店,
OP_Greater Equal,
OP_EQUAL,
OP_NOTEQUAL,
不,,
OP_分配,
OP_和,
OP_或,
点滴
}
公共静态void main(字符串[]args){
行号();
}
public static final String inputString=“public class HelloWorld{\n”+
“公共静态void-*/--main(字符串[]args){\n”+
“//打印出+8+*+hello world\n”+
“System.out.println(\“Hello World!\”);\n”+
“}\n”+
“}\n”;
专用静态无效行号(){
字符串[]行=inputString.split(\\r?\\n”);
对于(int i=0;i
嵌套循环允许保留当前行索引。 另外,您的
getOP
方法应该只负责根据字符值确定数学运算。它不应该打印任何内容,这是
行号的责任

输出:

Line 2: OP_SUBTRACT, -
Line 2: OP_MULTIPLY, *
Line 2: OP_DIVIDE, /
Line 2: OP_DIVIDE, /
Line 2: OP_SUBTRACT, -
Line 2: OP_SUBTRACT, -
Line 3: OP_DIVIDE, /
Line 3: OP_DIVIDE, /
Line 3: OP_ADD, +
Line 3: OP_ADD, +
Line 3: OP_MULTIPLY, *
Line 3: OP_ADD, +

因此,关键是将行号与符号打印放在同一个循环中。我还增加了一些复杂性,以避免打印行号时不带任何内容

public class Testing {

public enum TokenType {

    OP_MULTIPLY,
    OP_DIVIDE,
    OP_MOD,
    OP_ADD,
    OP_SUBTRACT,
    OP_LESS,
    OP_LESSEQUAL,
    OP_GREATER,
    OP_GREATEREQUAL,
    OP_EQUAL,
    OP_NOTEQUAL,
    OP_NOT,
    OP_ASSIGN,
    OP_AND,
    OP_OR,
    OP_DOT

}

public static String inputString = "public class HelloWorld {\n" +
    " public static void-*//-- main(String[] args) {\n" +
    " // to print out+8+*+ hello world\n" +
    " System.out.println(\"Hello World!\");\n" +
    " }\n" +
    "}\n";

public static void lineNumber(String lineNumber) {
    int count = 0;

    String[] lines = inputString.split("\\r?\\n");
    for (String line: lines) {
        count++;
        int n = line.length();
        boolean first = true;
        for (int i = 0; i < n; i++) {
            char ch = line.charAt(i);
            if (getOP(ch, first, count) != null) {
                first = false;
            }

        }
        System.out.println();
    }


}



public static TokenType getOP(char ch, boolean firstOnLine, int lineNum) {
    if (firstOnLine && (ch == '+' || ch == '-' || ch == '/' || ch == '*')) {
        System.out.print("line " + lineNum + " ");
    }
    switch (ch) {
        case '+':
            System.out.println(TokenType.OP_ADD + ", " + ch);
            return TokenType.OP_ADD;

        case '-':
            System.out.println(TokenType.OP_SUBTRACT + ", " + ch);
            return TokenType.OP_SUBTRACT;

        case '/':
            System.out.println(TokenType.OP_DIVIDE + ", " + ch);
            return TokenType.OP_DIVIDE;

        case '*':
            System.out.println(TokenType.OP_MULTIPLY + ", " + ch);
            return TokenType.OP_MULTIPLY;
    }

    return null;

}

public static void main(String[] args) {
    lineNumber(inputString);
}

}
公共类测试{
公共枚举令牌类型{
运算乘法,
OP_DIVIDE,
OP_MOD,
补充:,
OP_减法,
无操作性,
OP_LESSEQUAL,
奥普大酒店,
OP_Greater Equal,
OP_EQUAL,
OP_NOTEQUAL,
不,,
OP_分配,
OP_和,
OP_或,
点滴
}
public static String inputString=“public class HelloWorld{\n”+
“公共静态void-*/--main(字符串[]args){\n”+
“//打印出+8+*+hello world\n”+
“System.out.println(\“Hello World!\”);\n”+
“}\n”+
“}\n”;
公共静态无效行号(字符串行号){
整数计数=0;
字符串[]行=inputString.split(\\r?\\n”);
用于(字符串行:行){
计数++;
int n=line.length();
布尔值优先=真;
对于(int i=0;i
我喜欢您将解析与编写内容分离,并在过程中避免不必要的输出。
public class Testing {

public enum TokenType {

    OP_MULTIPLY,
    OP_DIVIDE,
    OP_MOD,
    OP_ADD,
    OP_SUBTRACT,
    OP_LESS,
    OP_LESSEQUAL,
    OP_GREATER,
    OP_GREATEREQUAL,
    OP_EQUAL,
    OP_NOTEQUAL,
    OP_NOT,
    OP_ASSIGN,
    OP_AND,
    OP_OR,
    OP_DOT

}

public static String inputString = "public class HelloWorld {\n" +
    " public static void-*//-- main(String[] args) {\n" +
    " // to print out+8+*+ hello world\n" +
    " System.out.println(\"Hello World!\");\n" +
    " }\n" +
    "}\n";

public static void lineNumber(String lineNumber) {
    int count = 0;

    String[] lines = inputString.split("\\r?\\n");
    for (String line: lines) {
        count++;
        int n = line.length();
        boolean first = true;
        for (int i = 0; i < n; i++) {
            char ch = line.charAt(i);
            if (getOP(ch, first, count) != null) {
                first = false;
            }

        }
        System.out.println();
    }


}



public static TokenType getOP(char ch, boolean firstOnLine, int lineNum) {
    if (firstOnLine && (ch == '+' || ch == '-' || ch == '/' || ch == '*')) {
        System.out.print("line " + lineNum + " ");
    }
    switch (ch) {
        case '+':
            System.out.println(TokenType.OP_ADD + ", " + ch);
            return TokenType.OP_ADD;

        case '-':
            System.out.println(TokenType.OP_SUBTRACT + ", " + ch);
            return TokenType.OP_SUBTRACT;

        case '/':
            System.out.println(TokenType.OP_DIVIDE + ", " + ch);
            return TokenType.OP_DIVIDE;

        case '*':
            System.out.println(TokenType.OP_MULTIPLY + ", " + ch);
            return TokenType.OP_MULTIPLY;
    }

    return null;

}

public static void main(String[] args) {
    lineNumber(inputString);
}

}