Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: For loop with a string as the subject

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);

    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();
    	}
     
    }
    Last edited by Fluidz; June 20th, 2011 at 07:51 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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".
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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]?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: For loop with a string as the subject

    Quote Originally Posted by Fluidz View Post
    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]
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: For loop with a string as the subject

    Thanks for that as well

    Cheers again for the quick reply.

Similar Threads

  1. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  2. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. While Loop Exit with String
    By Zaroth in forum Loops & Control Statements
    Replies: 8
    Last Post: November 9th, 2009, 04:20 PM