
Patrick B. answered 05/24/21
Math and computer tutor/teacher
import java.util.ArrayList;
import java.io.*;
class MathChecker
{
protected ArrayList<String> A;
public MathChecker()
{
A = new ArrayList<String>();
}
public int GetCount() { return(A.size()); }
public String IndexerGetAtIndex(int iIndexPos)
{
String strReturn = null;
if (iIndexPos>=0 && iIndexPos<A.size())
{
strReturn = (String)A.get(iIndexPos);
}
return(strReturn);
}
public void Go(String filename)
{
try
{
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inbuff = new String("?");
while (inbuff != null)
{
inbuff = bufferedReader.readLine();
if (inbuff==null || inbuff.length()==0) { break; }
//tokens[0] contains the 1st number; tokens[2] contains the 2nd number
//tokens[1] contains the operations symbol; tokens[3] contains the equal sign
// tokens[4] contains the alleged answer
String tokens[] = (inbuff.trim()).split(" ");
// for (int iLoop=0; iLoop<tokens.length; iLoop++) { System.out.println(" token # " + iLoop + " = " + tokens[iLoop]);}
double num1 = Double.parseDouble(tokens[0]);
double num2 = Double.parseDouble(tokens[2]);
double answer = Double.parseDouble(tokens[4]);
double Answer=0;
switch (tokens[1].charAt(0))
{
case '+' : { Answer = num1 + num2; break; }
case '-' : { Answer = num1 - num2; break; }
case '*' : { Answer = num1 * num2; break; }
case '/' :
{
if (num2==0)
{
Answer = Double.POSITIVE_INFINITY;
}
else
{
Answer = num1/num2;
}
break;
}
case '%' :
{
if (num2==0)
{
Answer = Double.POSITIVE_INFINITY;
}
else
{
Answer = (long)num1%(long)num2;
}
break;
}
} //switch
if (Answer!=answer)
{
A.add(inbuff);
}
} //while
bufferedReader.close();
int n = A.size();
for ( int iLoop=0; iLoop<n; iLoop++)
{
System.out.println((String)A.get(iLoop));
}
}
catch (IOException ex)
{
System.out.println(" IO Exception occurs opening/reading file ");
}
}
public static void main(String args[])
{
if (args.length>=1)
{
MathChecker x = new MathChecker();
x.Go(args[0]);
}
else
{
System.out.println(" issue the following command at the system prompt: \n\n" +
" > java MathChecker filename ");
}
}
}
/******************************************
Input file, E:\mathproblems.dat is:
1.0 + 2.0 = 4.0
2.5 + 2.5 = 5.0
3.0 / 2.0 = 1.0
3.2 * 5.0 = 16.0
15.0 - 7.0 = 8.0
19.0 - 11.0 = 7.0
6.0 * 8.0 = 42.0
7 % 5 = 2
15.0 / 3.0 = 5.0
************************************/
Kyle B.
thank you !!!05/24/21