如何跳过爪哇的下一个角色
我正在研究将前缀转换为后缀表达式的程序。但是,当表达式中有一个意外的空白空间,例如“ $+ - ABC+de f”,而不是“ $+ - abc+d-ef”,程序无法正常工作。我如何写下下一个字符并忽略空格,试图通过使用布尔iSblank方法的else语句进行操作。
public class PrefixConverter{
// Checks if character is an operator
public boolean isOperator(char c){
switch (c){
case '+':
case '-':
case'*':
case'/':
case'$':
return true;
}
return false;
}
// Ignores white space
public boolean isBlank(char c){
switch (c){
case ' ':
return true;
}
return false;
}
// Method to convert Prefix expression to Postfix expression
public String preToPost (String prefix_exp){
// Create a new stack with length of the prefix string
int size = prefix_exp.length();
Stack expression_stack = new Stack (size);
// Read expression from right to left
for (int i = size -1; i >=0 ; i-- ){
if (isOperator(prefix_exp.charAt(i))){
// Pop two operands from the stack
String op1 = expression_stack.peek();
expression_stack.pop();
String op2 = expression_stack.peek();
expression_stack.pop();
// Concatenate the operands and the operator
String temp = op1 + op2 + prefix_exp.charAt(i);
// Push the result back onto the stack
expression_stack.push(temp);
}
else if(isBlank(prefix_exp.charAt(i))){
// Skip to next character
}
// If the symbol is an operand
else {
// Push the operand onto the stack
expression_stack.push(prefix_exp.charAt(i) + "");
}
}
return expression_stack.peek();
}
}
I am working on a program that converts a prefix to a postfix expression. However, when there is an unexpected blank space in an expression such as "$+-ABC+D-E F" instead of "$+-ABC+D-EF" the program doesn't work correctly. How do I write skip to the next character and ignore the whitespace, trying to do it through an if else statement using a boolean isBlank method.
public class PrefixConverter{
// Checks if character is an operator
public boolean isOperator(char c){
switch (c){
case '+':
case '-':
case'*':
case'/':
case'
:
return true;
}
return false;
}
// Ignores white space
public boolean isBlank(char c){
switch (c){
case ' ':
return true;
}
return false;
}
// Method to convert Prefix expression to Postfix expression
public String preToPost (String prefix_exp){
// Create a new stack with length of the prefix string
int size = prefix_exp.length();
Stack expression_stack = new Stack (size);
// Read expression from right to left
for (int i = size -1; i >=0 ; i-- ){
if (isOperator(prefix_exp.charAt(i))){
// Pop two operands from the stack
String op1 = expression_stack.peek();
expression_stack.pop();
String op2 = expression_stack.peek();
expression_stack.pop();
// Concatenate the operands and the operator
String temp = op1 + op2 + prefix_exp.charAt(i);
// Push the result back onto the stack
expression_stack.push(temp);
}
else if(isBlank(prefix_exp.charAt(i))){
// Skip to next character
}
// If the symbol is an operand
else {
// Push the operand onto the stack
expression_stack.push(prefix_exp.charAt(i) + "");
}
}
return expression_stack.peek();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是在else中写下
继续;
如果():继续将简单地移至循环的下一个迭代,
但是,如果您不需要空格您可以在开始时从prefix_exp字符串中删除它们:
只需在尺寸变化时调用字符串的.length()之前进行替换。
One way would be to write a
continue;
in this else if():continue will simply move to the next iteration of the loop
However, if you do not need the spaces you can remove them from the prefix_exp String in the beginning by doing this:
Just make sure you do the replaceAll before you call .length() of the String as the size changes.
只需使用
继续
语句即可跳到循环的末尾。这将触发循环以使用下一个字符运行。但是,由于您的其余代码都在IF语句中,因此您的代码不做任何事情都应该表现良好。
另请参见 https://docs.oracle.com/javase.com/javase/tutorial/ java/nutsandbolts/branch.html
Just use the
continue
statement to skip to the end of your for loop.This will trigger the loop to run with the next character. But since the rest of your code is in if statements anyway, your code should behave well by just doing nothing.
See also https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html