Since headshot4444's inbox is full I'll answer his question here, hopefully it will help someone else too.
Originally Posted by headshot4444
Hey I heard you saying you're pretty good with a java. I'm a beginner and stuck on something. I'm trying to read in an operator from an input file. Operators such as "+ - * /". Any good idea on how to read them in? Right now I read them in as strings, and use String.equals("+"), or whatever the operator may be, to determine which operator it is. Thanks.
Sorry for the late response, I don't really check my messages.
Well you could compare as a char :P It really depends on what the input from the file looks like. If the file says something like the following you can use an infix expression algorithm.
Infix:
or
Postfix:
<strike>I could go into the best way to do this but it requires data structures you probably haven't learned yet (regex string splitting, stack) but the best way to do it (without your teacher accusing you of cheating) goes something like this:</strike> Only answering your question (without your teacher accusing you of cheating) and not writing the entire calculator for you. :P
Code:
switch (in.read()) { //InputStream#read() reads a single int from the input stream
case '+':
//add
break;
case '-':
//subtract
break;
case '*':
//multiply
break;
case '/':
//divide
break;
default:
//invalid input
//break; //break; isn't actually required here because the code will continue execution at the same place because it's the last case in the switch. If it wasn't the last case then without the break; the switch would fall through to the next case in the switch.
}