Reputation: 930
So I have a switch statement that will switch on each string in an array. When it encounters an operator, it will add that to an ArrayList. However, for some reason, when I compile the code it says that certain statements are "unreachable". I've marked which statements are the offending ones in my code below with "//".
Thanks in advance!
Code:
import java.util.*;
import java.io.*;
public class a3
{
public static void main(String[] args) throws FileNotFoundException
{
ArrayList<Token> tokens = new ArrayList<Token>();
String[] readTokens;
Stack<Operator> postStack = new Stack<Operator>();
String filename = "input.infix";
String DELIM = " ";
File in = new File(filename);
Scanner sc = new Scanner(in);
while (sc.hasNextLine())
{
readTokens = sc.nextLine().split(DELIM);
for (String s : readTokens)
{
switch(s)
{
case "(":
tokens.add(new Operator(opType.LPAR));
break;
case ")":
tokens.add(new Operator(opType.RPAR));
break; //unreachable
case "*":
tokens.add(new Operator(opType.MULT));
break; //unreachable
case "/":
tokens.add(new Operator(opType.DIV));
break; //unreachable
case "%":
tokens.add(new Operator(opType.MOD));
break; //unreachable
case "+":
tokens.add(new Operator(opType.ADD));
break; //unreachable
case "-":
tokens.add(new Operator(opType.SUB));
break; //unreachable
//Assuming the expression are valid (according to the
//assignment notes, anything other than operators are
//operands.
//
//NOTE: Even though spaces exist, they will not be
//interpreted as they are the delim
default:
tokens.add(new Operand(Integer.parseInt(s)));
break; //unreachable
}
}
String postfix = infix2postfix(tokens);
int finalResult = evalPostfix(postfix);
System.out.println(postfix + " = " + finalResult);
}
}
public static String infix2postfix(ArrayList<Token> al)
{
return "";
}
public static int evalPostfix(String post)
{
return 0;
}
}
Upvotes: 4
Views: 3470
Reputation: 41117
Your code is completely fine as suggested buy others.
It is okay to put a break
inside a statement a swhich
lock. But the break should be at the last statement, you probably have forgotten in some of the case statements.
Upvotes: 1
Reputation: 86774
I added the missing class/interface/enum declarations to your code and cannot reproduce the error. The problem is clearly in code you haven't posted.
Here's what compiles fine for me:
import java.util.*;
import java.io.*;
public class Test
{
public static interface Token
{
}
public static class Operator implements Token
{
public Operator(opType type)
{
}
}
public static enum opType
{
LPAR, RPAR, MULT, DIV, MOD, ADD, SUB
}
public static class Operand implements Token
{
public Operand(int val)
{
}
}
public static void main(String[] args) throws FileNotFoundException
{
ArrayList<Token> tokens = new ArrayList<Token>();
String[] readTokens;
Stack<Operator> postStack = new Stack<Operator>();
String filename = "input.infix";
String DELIM = " ";
File in = new File(filename);
Scanner sc = new Scanner(in);
while (sc.hasNextLine())
{
readTokens = sc.nextLine().split(DELIM);
for (String s : readTokens)
{
switch (s)
{
case "(":
tokens.add(new Operator(opType.LPAR));
break;
case ")":
tokens.add(new Operator(opType.RPAR));
break;
case "*":
tokens.add(new Operator(opType.MULT));
break;
case "/":
tokens.add(new Operator(opType.DIV));
break;
case "%":
tokens.add(new Operator(opType.MOD));
break;
case "+":
tokens.add(new Operator(opType.ADD));
break;
case "-":
tokens.add(new Operator(opType.SUB));
break;
// Assuming the expression are valid (according to the
// assignment notes, anything other than operators are
// operands.
//
// NOTE: Even though spaces exist, they will not be
// interpreted as they are the delim
default:
tokens.add(new Operand(Integer.parseInt(s)));
break;
}
}
String postfix = infix2postfix(tokens);
int finalResult = evalPostfix(postfix);
System.out.println(postfix + " = " + finalResult);
}
}
public static String infix2postfix(ArrayList<Token> al)
{
return "";
}
public static int evalPostfix(String post)
{
return 0;
}
}
Upvotes: 3