Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with JSP if statement

    I am new to JSP, my background is in PHP. I am having trouble with this basic if statement and don't know why.


    I'm trying to read from a text file and match a value from the file to a preset variable.

    Text File:
    news_2010_12_01.jsp
    news_2010_12_02.jsp
    news_2010_12_03.jsp
    news_2010_12_04.jsp
    news_2010_12_05.jsp
    news_2010_12_06.jsp
    news_2010_12_07.jsp
    news_2010_12_08.jsp

    Code:
    String articleVarOrg = "news_2010_12_08.jsp";
    
     try {
      
    	 BufferedReader input = new BufferedReader(new FileReader("articleOrder.txt"));
    	 String line = "";
    	 while ((line=input.readLine()) != null)
    	 {
    		 out.print(line); //this prints just fine
    		if(line.trim() == articleVarOrg.trim()) {
    			out.print("AAA"); //this is not printing
    		}
    	 }
    	
    	 input.close();
    	}
    	 catch(Exception e) { out.println(e); }
    I'm expecting "AAA" to print as a result of the last line of the text file "news_2010_12_08.jsp" matching the variable articleVarOrg. I even added a trim to make sure there are no linebreaks to un-validate the if statement, however I'm not sure if the trim is actually working.

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    When dealing with string equality you have to use the .equals method because strings are not a primitive type...

    Code:
    String meName = "Andy":
    String otherName = "Bob";
    
    if(meName.equals(otherName)){
         System.out.println("They match");
    }
    else{
        System.out.println("They dont match");
    }
    Like that!

  • Users who have thanked renegadeandy for this post:

    brick (12-17-2010)

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That worked perfectly, thank you so much for your help and quick reply!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •