Results 1 to 7 of 7
  1. #1
    asabdo is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default help: matches don`t work

    function of code is putting if statement and discover do the if statement wrong or not?
    but the answer is always wrong, plz the help to get result when put the if correct statement is not wrong.
    thank you

    Java Code:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.regex.Pattern;
    
    
    public class Main {
      public static void main(String args[]) throws IOException {
    
    
    
        FileWriter fout = new FileWriter("test.txt");
        fout.write("if a = 30  c = a / 5 ;");
        fout.close();
    
        FileReader fin = new FileReader("Test.txt");
    
       
    
        Scanner src = new Scanner(fin);
    
    
    
           try {
               
                 boolean z = Pattern.matches("if"+"[a-zA-Z]"+"="+"[0-9]"+"[a-zA-Z]"+"="+"[a-zA-Z]"+"/"+"[0-9]"+";",fin.getEncoding());
                 System.out.println("pattern is: "+z);
          } catch (Exception e) {
              System.out.println("error");
          }      
    
       
    
        
    
        fin.close();
       
    
      }
    }
    Last edited by asabdo; 01-15-2010 at 03:13 PM.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Is this really so hard you need to ask someone else? Did you try printing out the two strings you are trying to match? if you had you would see that fin.getEncoding() = "Cp1252" I think that you are trying to take what is in the file and match it to "if". See if they match...
    Java Code:
    boolean z = Pattern.matches("if",src.next());

  3. #3
    asabdo is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by collin389 View Post
    Is this really so hard you need to ask someone else? Did you try printing out the two strings you are trying to match? if you had you would see that fin.getEncoding() = "Cp1252" I think that you are trying to take what is in the file and match it to "if". See if they match...
    Java Code:
    boolean z = Pattern.matches("if",src.next());
    THNX on helping
    i use the last those src.next(),src.toString() and src.findInLine()
    but all give me false
    i want to put if-statement and test on it if it is correct or no ?
    and thank you

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    I don't really understand your question. Are you saying you want to check to see if a == 30, and if it is true, set c = a/5? or are just just trying to figure out if "if" is in the file.

  5. #5
    Syntax's Avatar
    Syntax is offline Member
    Join Date
    Jan 2010
    Location
    Georgia
    Posts
    20
    Rep Power
    0

    Default

    None of these quotations make any sense to me.

    FileWriter fout = new FileWriter("test.txt");
    FileReader fin = new FileReader("Test.txt");
    boolean z = Pattern.matches("if"+"[a-zA-Z]"+"="+"[0-9]"+"[a-zA-Z]"+"="+"[a-zA-Z]"+"/"+"[0-9]"+";",fin.getEncoding());
    11th Grade | Beginner Programmer | Looking into College

  6. #6
    asabdo is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    thank you
    and i reach solution
    this is solution
    Java Code:
    String abdo=JOptionPane.showInputDialog("Enter sentence to be Parsed");
    
     
    
           boolean b = Pattern.matches("[i][f]"+"[ \t\n\f\r]+"+"([a-zA-Z][0-9]*|| [a-zA-Z])+"+"[ \t\n\f\r]*"+"="+"[ \t\n\f\r]*"
                +"[0-9]+"+"[ \t\n\f\r]*"+"([a-zA-Z][0-9]*|| [a-zA-Z])+"+"[ \t\n\f\r]*"+"="+"[ \t\n\f\r]*"+"([a-zA-Z][0-9]*|| [a-zA-Z])+"+"[ \t\n\f\r]*"+"/"+"[ \t\n\f\r]*"
                +"[0-9]+"+"[ \t\n\f\r]*"+";",abdo);
       
        boolean d = Pattern.matches("[i][f]"+"([ \t\n\f\r]*[(])"+"[ \t\n\f\r]*"+"([a-zA-Z][0-9]*|| [a-zA-Z])+"+"[ \t\n\f\r]*"+"="+"[ \t\n\f\r]*"
                +"[0-9]+"+"[ \t\n\f\r]*"+"[(]"+"[ \t\n\f\r]*"+"([a-zA-Z][0-9]*|| [a-zA-Z])+"+"[ \t\n\f\r]*"+"="+"[ \t\n\f\r]*"+"([a-zA-Z][0-9]*|| [a-zA-Z])+"+"[ \t\n\f\r]*"+"/"+"[ \t\n\f\r]*"
                +"[0-9]+"+"[ \t\n\f\r]*"+";",abdo);
           // System.out.println("b= "+b+"  and d= "+ d);
       if ((b == true)||(d == true) )  {
      
        System.out.println("The Code is Correct");

  7. #7
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Well that would've been impossible to determine based on your original question.

Similar Threads

  1. Replies: 0
    Last Post: 11-17-2009, 01:58 PM
  2. How "Pattern.matches(regex, input)" function works?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-26-2009, 12:46 AM
  3. Regex that matches whole word
    By moaxjlou in forum New To Java
    Replies: 13
    Last Post: 11-04-2008, 05:48 PM
  4. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM
  5. Help with password matches
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-10-2007, 04:17 PM

Posting Permissions

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