如何在不同的方法中打破标签 java
How to break label within a different method java
这是我的源代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class PostfixConverter{
static int top = 0;
static String[] mainStack = new String[100];
final static String asterisk = "*";
final static String divisor = "/";
final static String plus = "+";
final static String minus = "-";
final static String store = "ST TEMP";
static int temp = 0;
static int directionCounter = 0;
static String[] directions = new String[100];
static PostfixConverter s = new PostfixConverter();
static String tempString = "TEMP";
public static void main(String args[]) throws Exception {
String string = null;
String load = "LD ";
String multiply = "ML ";
String add = "AD ";
String div = "DV ";
String subtract = "SB ";
String example = "AB+C-";
try {
// file reader code
FileReader file = new FileReader("ignore for now");
BufferedReader reader = new BufferedReader(file);
String line = "";
while ((line = reader.readLine()) != null) {
example = line;
// for loop to print directions
Characterloop:
for (int i = 0; i < example.length(); i++) {
// get letter entered by user 1 by 1
char letter = example.charAt(i);
// convert char to string
String convertedChar = java.lang.String.valueOf(letter);
// finds operands in order or priority
// multiply character
if (convertedChar.equals(asterisk)) {
processOperand(PostfixConverter.multiply(string), PostfixConverter.multiply(string), load,
multiply);
}
// division character
else if (convertedChar.equals(divisor)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, div);
}
// addition character
else if (convertedChar.equals(plus)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, add);
}
// subtraction character
else if (convertedChar.equals(minus)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load,
subtract);
}
// letter character
else {
s.push(convertedChar);
}
}
// print out the instructions
System.out.println("Assembly Directions are as follows: ");
int printDirections = 0;
for (int i = 0; i < directionCounter; i++) {
System.out.println(directions[printDirections]);
printDirections++;
}
printDirections = 0;
directionCounter = 0;
System.out.println("This is the end of the directions.");
System.out.println("");
directionCounter = 0;
temp = 0;
top = 0;
}reader.close();
} catch (FileNotFoundException exception) {
System.out.println("The file was not found.");
}
}
private static void processOperand(String postFileConverterOutput, String postFileConverterOutput2,
String instruction1, String instruction2) {
String outcome;
String opReturn1 = postFileConverterOutput;
String opReturn2 = postFileConverterOutput2;
directions[directionCounter] = instruction1 + opReturn2;
directionCounter++;
directions[directionCounter] = instruction2 + opReturn1;
directionCounter++;
temp++;
outcome = tempString + java.lang.String.valueOf(temp);
directions[directionCounter] = store + java.lang.String.valueOf(temp);
directionCounter++;
s.push(outcome);
}
// multiply method
public static String multiply(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
break Characterloop;
}
String multVariable = PostfixConverter.pop(mainStack[top]);
top--;
return multVariable;
}
// addition method
public static String addition(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String addVariable = PostfixConverter.pop(mainStack[top]);
top--;
return addVariable;
}
// subtraction method
public static String subtraction(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String subVariable = PostfixConverter.pop(mainStack[top]);
top--;
return subVariable;
}
// division method
public static String division(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String divVariable = PostfixConverter.pop(mainStack[top]);
top--;
return divVariable;
}
public static boolean empty() {
boolean check = false;
if (top < 0){
check = true;
}
else{
check = false;
}return check;
}
public static String pop(String j) {
if (top < 0) {
System.out.println("Stack is empty");
System.exit(1);
}
return mainStack[top - 1];
}
public void push(String x) {
if (top == 99) {
System.out.println("Stack Overflow");
System.exit(1);
} else
mainStack[top] = x;
System.out.println("Top:" + top + "||" + " Array: " + mainStack[top]);
top++;
}// end push
}
当给出无效字符串时,我如何才能使用此方法停止 运行 循环?我要问的项目在**问题**下面的星号中。举例来说,我得到后缀字符串“AB+*AB+”和“AB+”。显然这是无效的,因为不可能有两个运算符只有两个操作数。如何跳过第一条语句并打印 "invalid argument" 并继续到下一个字符串 (AB+)?我研究了中断并继续,但我不知道如何在另一种方法中实现它。这是一个家庭作业,只是想指出正确的方向。
// multiply method
public static String multiply(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
**break Characterloop;**
}
可以使用异常和 try/catch 来完成类似的事情。然而,这样做应该谨慎使用。抛出异常不应该用于 "return" 给用户的东西。但是抛出异常有好处:
- 它们更容易重用
- 您可以将信息附加到例外情况
- 编译时检查(仅检查异常)。对于标签,如果
break
语句按预期工作,如果堆栈中有包含适当标签的方法调用,则不会进行编译时检查来验证是否仅调用了该方法
- 他们有更好的 IDE 支持(try / catch 块生成)
- 如果它们有一个共同的超类,您可以捕获整组异常
我们把问题代码简单化一下:
包含无效中断的代码
中断
void a() {
label1: while (check()) {
b();
}
}
void b() {
c();
}
void c() {
if (check2()) {
break label1;
}
doSomething();
}
继续
void d() {
label2: while(check()) {
e();
}
doSomething();
}
void e() {
f();
}
void f() {
if (check2()) {
continue label2;
}
doSomething();
}
使用异常的解决方案
中断
class MyException1 extends Exception {
}
void a() {
try {
while (check()) {
b();
}
} catch (MyException1 ex) {
// we're already outside the loop; nothing to be done here
}
}
void b() throws MyException1 {
c();
}
void c() throws MyException1 {
if (check2()) {
throw new MyException1();
}
doSomething();
}
继续
class MyException2 extends Exception {
}
void d() {
while(check()) {
try {
e();
doSomething();
} catch (MyException2 ex) {
// nothing to do here; We're already at the end of the loop body
}
}
}
void e() throws MyException2 {
f();
}
void f() throws MyException2 {
if (check2()) {
throw new MyException2();
}
doSomething();
}
这是我的源代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class PostfixConverter{
static int top = 0;
static String[] mainStack = new String[100];
final static String asterisk = "*";
final static String divisor = "/";
final static String plus = "+";
final static String minus = "-";
final static String store = "ST TEMP";
static int temp = 0;
static int directionCounter = 0;
static String[] directions = new String[100];
static PostfixConverter s = new PostfixConverter();
static String tempString = "TEMP";
public static void main(String args[]) throws Exception {
String string = null;
String load = "LD ";
String multiply = "ML ";
String add = "AD ";
String div = "DV ";
String subtract = "SB ";
String example = "AB+C-";
try {
// file reader code
FileReader file = new FileReader("ignore for now");
BufferedReader reader = new BufferedReader(file);
String line = "";
while ((line = reader.readLine()) != null) {
example = line;
// for loop to print directions
Characterloop:
for (int i = 0; i < example.length(); i++) {
// get letter entered by user 1 by 1
char letter = example.charAt(i);
// convert char to string
String convertedChar = java.lang.String.valueOf(letter);
// finds operands in order or priority
// multiply character
if (convertedChar.equals(asterisk)) {
processOperand(PostfixConverter.multiply(string), PostfixConverter.multiply(string), load,
multiply);
}
// division character
else if (convertedChar.equals(divisor)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, div);
}
// addition character
else if (convertedChar.equals(plus)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, add);
}
// subtraction character
else if (convertedChar.equals(minus)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load,
subtract);
}
// letter character
else {
s.push(convertedChar);
}
}
// print out the instructions
System.out.println("Assembly Directions are as follows: ");
int printDirections = 0;
for (int i = 0; i < directionCounter; i++) {
System.out.println(directions[printDirections]);
printDirections++;
}
printDirections = 0;
directionCounter = 0;
System.out.println("This is the end of the directions.");
System.out.println("");
directionCounter = 0;
temp = 0;
top = 0;
}reader.close();
} catch (FileNotFoundException exception) {
System.out.println("The file was not found.");
}
}
private static void processOperand(String postFileConverterOutput, String postFileConverterOutput2,
String instruction1, String instruction2) {
String outcome;
String opReturn1 = postFileConverterOutput;
String opReturn2 = postFileConverterOutput2;
directions[directionCounter] = instruction1 + opReturn2;
directionCounter++;
directions[directionCounter] = instruction2 + opReturn1;
directionCounter++;
temp++;
outcome = tempString + java.lang.String.valueOf(temp);
directions[directionCounter] = store + java.lang.String.valueOf(temp);
directionCounter++;
s.push(outcome);
}
// multiply method
public static String multiply(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
break Characterloop;
}
String multVariable = PostfixConverter.pop(mainStack[top]);
top--;
return multVariable;
}
// addition method
public static String addition(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String addVariable = PostfixConverter.pop(mainStack[top]);
top--;
return addVariable;
}
// subtraction method
public static String subtraction(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String subVariable = PostfixConverter.pop(mainStack[top]);
top--;
return subVariable;
}
// division method
public static String division(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String divVariable = PostfixConverter.pop(mainStack[top]);
top--;
return divVariable;
}
public static boolean empty() {
boolean check = false;
if (top < 0){
check = true;
}
else{
check = false;
}return check;
}
public static String pop(String j) {
if (top < 0) {
System.out.println("Stack is empty");
System.exit(1);
}
return mainStack[top - 1];
}
public void push(String x) {
if (top == 99) {
System.out.println("Stack Overflow");
System.exit(1);
} else
mainStack[top] = x;
System.out.println("Top:" + top + "||" + " Array: " + mainStack[top]);
top++;
}// end push
}
当给出无效字符串时,我如何才能使用此方法停止 运行 循环?我要问的项目在**问题**下面的星号中。举例来说,我得到后缀字符串“AB+*AB+”和“AB+”。显然这是无效的,因为不可能有两个运算符只有两个操作数。如何跳过第一条语句并打印 "invalid argument" 并继续到下一个字符串 (AB+)?我研究了中断并继续,但我不知道如何在另一种方法中实现它。这是一个家庭作业,只是想指出正确的方向。
// multiply method
public static String multiply(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
**break Characterloop;**
}
可以使用异常和 try/catch 来完成类似的事情。然而,这样做应该谨慎使用。抛出异常不应该用于 "return" 给用户的东西。但是抛出异常有好处:
- 它们更容易重用
- 您可以将信息附加到例外情况
- 编译时检查(仅检查异常)。对于标签,如果
break
语句按预期工作,如果堆栈中有包含适当标签的方法调用,则不会进行编译时检查来验证是否仅调用了该方法 - 他们有更好的 IDE 支持(try / catch 块生成)
- 如果它们有一个共同的超类,您可以捕获整组异常
我们把问题代码简单化一下:
包含无效中断的代码
中断
void a() {
label1: while (check()) {
b();
}
}
void b() {
c();
}
void c() {
if (check2()) {
break label1;
}
doSomething();
}
继续
void d() {
label2: while(check()) {
e();
}
doSomething();
}
void e() {
f();
}
void f() {
if (check2()) {
continue label2;
}
doSomething();
}
使用异常的解决方案
中断
class MyException1 extends Exception {
}
void a() {
try {
while (check()) {
b();
}
} catch (MyException1 ex) {
// we're already outside the loop; nothing to be done here
}
}
void b() throws MyException1 {
c();
}
void c() throws MyException1 {
if (check2()) {
throw new MyException1();
}
doSomething();
}
继续
class MyException2 extends Exception {
}
void d() {
while(check()) {
try {
e();
doSomething();
} catch (MyException2 ex) {
// nothing to do here; We're already at the end of the loop body
}
}
}
void e() throws MyException2 {
f();
}
void f() throws MyException2 {
if (check2()) {
throw new MyException2();
}
doSomething();
}