For loop with a string as the subject [SOLVED]
I am building a basic calculator and this is my first program in Java after reading about.. 2 tutorials and I am having a hard time getting the program to check the input on the string and run code if that input satisfies the condition.
The user inputs either +, -, * or /
I want to check to see if the input is a +, if it is then run the code. So far it runs no matter what you enter, null or otherwise.
Here is my code - let's not judge if it is crap.. you started this way (maybe);
Code java:
import java.io.*;
public class Main {
public static void main (String args[]) throws IOException {
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader readIN = new BufferedReader(inStream);
System.out.println("Multiply | Divide | Add | Subtract");
System.out.println(" * | / | + | - ");
try {
System.out.println("What operation?");
String oprt = readIN.readLine();
/*if(oprt != null){
System.out.println("Null entry detected.");
System.out.println("End of program. Press any key to terminate.");
}*/
if("+".equals(oprt));{
System.out.println("You selected addition!");
}
System.out.println( oprt );
}
catch (IOException err){
System.out.println("Error reading input.");
}
System.out.println("End of program. Press any key to terminate.");
String termin = readIN.readLine();
}
}
Re: For loop with a string as the subject
Take a closer look at your if statement:
if("+".equals(oprt));{
You've to an extra ';' in there, which is causing the block of code immediately following it to execute no matter what. I assume that's what you mean by "it runs no matter what you enter".
Re: For loop with a string as the subject
That is the biggest fail ever.
Cheers mate, and off topic: How come the code tags didn't highlight the java codes syntax? Is it [java][/java]?
Re: For loop with a string as the subject
Quote:
Originally Posted by
Fluidz
That is the biggest fail ever.
Cheers mate, and off topic: How come the code tags didn't highlight the java codes syntax? Is it [java][/java]?
I think what you're looking to do is [highlight=java]code goes here[/highlight]
Re: For loop with a string as the subject
Thanks for that as well :D
Cheers again for the quick reply.