[英]Printing out the line number a Char was found on in a String
i'm writing a program to scan for mathematical operators in a given string, when the program finds a match, i want to return the operator and the line that it was found on, below is my attempt.我正在编写一个程序来扫描给定字符串中的数学运算符,当程序找到匹配项时,我想返回运算符和找到它的行,下面是我的尝试。
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);
}
}
This is my current output:这是我目前的 output:
line 1
line 2
line 3
line 4
line 5
line 6
OP_SUBTRACT, -
OP_MULTIPLY, *
OP_DIVIDE, /
OP_DIVIDE, /
OP_SUBTRACT, -
OP_SUBTRACT, -
OP_DIVIDE, /
OP_DIVIDE, /
OP_ADD, +
OP_ADD, +
OP_MULTIPLY, *
OP_ADD, +
What im trying to achieve:我试图实现的目标:
Line 1: OP_ADD, +
Line 3: OP_MULTIPLY, *
etc.
Thanks谢谢
Your code might look as follows:您的代码可能如下所示:
public class Testing {
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 void main(String[] args) {
lineNumber();
}
public static final 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";
private static void lineNumber() {
String[] lines = inputString.split("\\r?\\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
for (int j = 0; j < line.length(); j++) {
TokenType tokenType = getOP(line.charAt(j));
if (tokenType != null) {
System.out.printf("Line %d: %s, %s\n", i + 1, tokenType.name(), line.charAt(j));
}
}
}
}
private static TokenType getOP(char ch) {
switch (ch) {
case '+':
return TokenType.OP_ADD;
case '-':
return TokenType.OP_SUBTRACT;
case '/':
return TokenType.OP_DIVIDE;
case '*':
return TokenType.OP_MULTIPLY;
// more
}
return null;
}
}
Nested loop allows for keeping current line index.嵌套循环允许保持当前行索引。 Also, your getOP
method should be responsible only for determining a math operation based on the character value.此外,您的getOP
方法应该只负责根据字符值确定数学运算。 It shouldn't print anything, it's a responsibility of the lineNumber
.它不应该打印任何东西,这是lineNumber
的责任。
Output: Output:
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, +
You should do your operation checking for each separate line, instead of doing it on the whole input at once.您应该对每个单独的行进行操作检查,而不是一次对整个输入进行检查。
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);
}
}
}
So the key was to get the line numbering inside the same loop as the symbol printing.所以关键是在与符号打印相同的循环中获取行号。 I also added some complexity to avoid printing line numbers with nothing on them.我还添加了一些复杂性以避免打印行号而没有任何内容。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.