Results 1 to 7 of 7
Thread: help: matches don`t work
- 01-15-2010, 07:20 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 12
- Rep Power
- 0
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.
- 01-15-2010, 08:26 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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());
- 01-15-2010, 03:11 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 12
- Rep Power
- 0
- 01-15-2010, 11:59 PM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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.
- 01-16-2010, 01:35 AM #5
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
- 01-16-2010, 01:38 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 12
- Rep Power
- 0
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");
- 01-16-2010, 07:06 AM #7
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Similar Threads
-
Lucene Not Throwing Matches When Search String Is Without Space
By nishu.soni in forum LuceneReplies: 0Last Post: 11-17-2009, 01:58 PM -
How "Pattern.matches(regex, input)" function works?
By kishan in forum Advanced JavaReplies: 2Last Post: 04-26-2009, 12:46 AM -
Regex that matches whole word
By moaxjlou in forum New To JavaReplies: 13Last Post: 11-04-2008, 05:48 PM -
how would i get this to work...?
By deeadeed in forum New To JavaReplies: 6Last Post: 12-06-2007, 02:58 AM -
Help with password matches
By Albert in forum AWT / SwingReplies: 1Last Post: 07-10-2007, 04:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks