Sunday 15 May 2011

Using try-catch to catch string input of fractions from JTextField in Java -


There is a string input from a JTextField, it should have a partial input with numbers, which will be separated by a single '/'. '.

I need to throw an error when a user uses the wrong string; So far, I can match the pattern but the exception will not be thrown out. I do not know what I'm doing or if there is an easy way to do this, (though I need to try it);

  Public Zero Set InputData (string strFrac1, string strFrac2, string operation) {String Test 1, Test 2; Test1 = strFrac1; Test2 = strFrac2; Try {test1.matches ("(d +) (p /) (d +)"); } Hold (number format exception e) {joppenpen Show message dialects (empty, E. mailsage), "ALERT!", JOpspain. ARROOM_MSES); } String [] fraction2 = strFrac2.split ("/"); String [] fraction1 = strFrac1.split ("/"); // It will fill an array with the need for numbers and operators to use us (int i = 0; i & lt; = 1; i ++) {fraction [i] = numerator 1 [i]; If (I == 0 || I == 1) {fraction [i + 2] = fraction 2 [i]; }} Fraction [4] = operational; Return (); }   

Am I using the wrong catcher?

  test1.matches ("(d +) (p /) (d + ) ");   

A boolean will return

You can obviously throw an exception

  try {if (test1.matches (" (D +) (P /) (D +) ")) Enter the new number format exception (); } Hold (number format exception e) {joppenpen Show message dialects (empty, E. mailsage), "ALERT!", JOpspain. ARROOM_MSES); }    

No comments:

Post a Comment