Re: textfield to enter integer and float expressions
It is not well tested and it is not pretty! Not a classroom example of
how to parse strings and evaluate the expressions. The code is below.
The class does not extend TextField as I think it is nicer to implement
the class as ether a ActionListener or a FocusListener and then add it
to the TextField as needed.
There should be room for inprovements.
Jan Agermose
perltools: http://www.quateams.com/oro/downloads/
CODE::
import java.util.Vector;
import java.awt.event.FocusListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.TextComponent;
import java.io.*;
import com.oroinc.text.perl.*;
public final class Math implements ActionListener, FocusListener {
public void actionPerformed(ActionEvent e) {
try {
TextComponent c = (TextComponent)e.getSource();
double d = evaluate(c.getText());
c.setText(Double.toString(d));
} catch (Exception exception) {}
}
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
try {
TextComponent c = (TextComponent)e.getSource();
double d = evaluate(c.getText());
c.setText(Double.toString(d));
} catch (Exception exception) {}
}
public double evaluate(String str) {
Perl5Util perl = new Perl5Util();
Vector v = new Vector();
String process = str;
do {
perl.match("/([\\(\\)\\-\\+\\*\\%\\/]|[\\d\\.\\,]+)/", process);
for(int i=1;i0 &&
(!process.trim().equals("")));
// return op(op(0,v),v);
return op(true, 0,v);
}
private String pop(Vector v) {
String s = (String)v.elementAt(0);
v.removeElementAt(0);
return s;
}
private double op(boolean ekstra,double d, Vector v) {
double val = d;
try {
String op = pop(v);
if (op.equals("+")) {
val = d+op(true, 0,v);
} else if (op.equals("-")) {
val = d-op(true, 0,v);
} else if (op.equals("*")) {
double d2 = op(false, 0,v);
val = op(true, d*d2, v);
} else if (op.equals("/")) {
double d2 = op(false, 0,v);
val = op(true, d/d2, v);
} else if (op.equals("(")) {
val = op(true, 0, v);
ekstra = true;
} else if (op.equals(")")) {
val = d;
ekstra = false;
} else {
val = Double.parseDouble(op);
}
if (ekstra)
val = op(false,val,v);
} catch (Exception e) {
}
return val;
}
public static void main(String[] argv) {
Math m = new Math();
if (argv.length == 1) {
if ((argv[0].equals("TEST"))) {
java.awt.Frame f = new java.awt.Frame("Test");
java.awt.TextField tf = new java.awt.TextField(20);
tf.addActionListener(m);
tf.addFocusListener(m);
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((TextComponent)e.getSource()).selectAll();
}
});
f.add(tf);
f.pack();
f.show();
} else {
System.err.println(m.evaluate(argv[0]));
}
}
else {
try {
BufferedReader keyb = new BufferedReader(new
InputStreamReader(System.in));
String str;
while (!(str = keyb.readLine()).equals("")) {
System.out.println("val = "+m.evaluate(str));
}
} catch (Exception e) {
System.err.println(e);
}
}
}
}
Joost Helberg wrote:
>
> Hi,
>
> I'm developing a system for small IT businesses and I need to enter
> numbers into fields.
>
> Currently I use a JTextField to enter the number into, converting it
> to Integer or Double when needed.
>
> I want to implement a speciliazed version of JTextField which will
> evaluate integer expressions.
>
> So entering
> (3 * 14 + 2) / (5+6)
>
> will display (upon leaving the field)
> 4
>
> Anyone familiar with Open Source Code to (partly) implement this?
>
> Joost Helberg
>
> PS: I don't understand why not all number-entering fields in all
> applications support this. It is so obvious! Applications with a
> pop-up calculator are so improductive!
>
> THANKS in advance
>
> --
> Joost Helberg Unix consultants v v
> [EMAIL PROTECTED] OO developers \ /
> ### #### # >---X---<
> http://snow.nl # # # # # # # # / \
> Sn
Re: Question
Hi, Firstly: applets CAn read and write files as through JDBC Secondly: you have to place the postgresql.jar file in your applet's home dir. gr. Eric --- James Seigel <[EMAIL PROTECTED]> wrote: > Check your applet tag for codebase tag > Check to make sure your datbase driver is available in > the directory where > your codebase is implied. > Make sure you database is on the same machine as the > applet is loaded > from. > > > 3 is for after you fix the first two. > > J > > Pooh Bear -- "I am just a bear of little brain" > > > On Thu, 4 Nov 1999, DD23 CCChang2 wrote: > > > > > > Dear Sirs: > > > I am junior JAVA programmer, one question need your > help. > > > I had RedHat Linux 6.0 with Java 1.2 SDK and > PostgreSQL database, > > > and my web server is Apache 1.3.6 version. > > > I had copy the postgresql.jar into > /jdk1.2/jre/lib/ext. > > > > > > then, I wrote an applet just query the data from > PostgreSql database, > > > and show the result on screen, then created the > test.html file, > > > and include the class , when I use appletviewer > test.html, I can see > > > the data everything is OK, > > > > > > but when I put the html on my web, then I can see the > applet area show > > > "Can't find Database Driver .. > postgresql.drive" > > > It's anything Linux envirounment setting error? > > > Tks! > > > > > > Jones Chang > > > > > > > > > > -- > > To UNSUBSCRIBE, email to > [EMAIL PROTECTED] > > with a subject of "unsubscribe". Trouble? Contact > [EMAIL PROTECTED] > > > > > -- > To UNSUBSCRIBE, email to > [EMAIL PROTECTED] > with a subject of "unsubscribe". Trouble? Contact > [EMAIL PROTECTED] > > __ Do You Yahoo!? Bid and sell for free at http://auctions.yahoo.com -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: Netscape and applets
Hi, Hotjava may solve the problem for the developer. The end user however wants it to run on Netscape or whatever. I'd personally like everyone running appletviewer but, alas! gr. Eric --- peter johnson <[EMAIL PROTECTED]> wrote: > Try HotJava. Since it is written in Java, it will run > anything the JVM from > your JDK will run. > > Paul Gearon wrote: > > > I have a strange problem with Netscape on my RH6.1 > system. > > > > I have a class called myapp.server.FrontEnd which > extends Applet > > (naturally). I have a reference to it in a .html file: > > HEIGHT=300> > > and it seems to work fine with appletviewer. However, > as soon as I load > > it with Netscape I get the following in the status bar: > > > > Applet myapp.server.FrontEnd exception: > java.lang.ClassCastException > > myapp.server.FrontEnd is not an applet > > > > I'm stumped. I've written other applets for use with > Netscape which > > have worked fine. Has anyone any suggestions as to > where I should look > > for a solution? > > > > Also, since I don't know if the problem is mine or > Netscape's, is there > > another browser out there for Linux which will run > applets? > > > > -- > > Regards, > > Paul > > > > Paul Gearon > > [EMAIL PROTECTED] > > > > Catapultam habeo. Nisi pecuniam omnem mihi dabis, ad > caput tuum saxum > > immane > > mittam. > > (Translation from latin: "I have a catapult. Give me > all the money, or I > > will > > fling an enormous rock at your head.") > > > > > -- > > To UNSUBSCRIBE, email to > [EMAIL PROTECTED] > > with a subject of "unsubscribe". Trouble? Contact > [EMAIL PROTECTED] > > > -- > To UNSUBSCRIBE, email to > [EMAIL PROTECTED] > with a subject of "unsubscribe". Trouble? Contact > [EMAIL PROTECTED] > > __ Do You Yahoo!? Bid and sell for free at http://auctions.yahoo.com -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: JDBC newbie question
Peter, I have no problem on my end. I have 4 years expeience with Server Side Java. I want the most advanced JDBC driver/db combination. The question I am rasing is which JDBC drier is more robust and closer to the JDBC 2.0 speck. Once I have a solid driver, there is no problem writing code to use it here! Do you know of any independent comparisons of the JDBC drivers for PostgresSQL and mySQL? I just got back from the Colorado Software Summit and feedback there was mixed. john On 26-Oct-99 Peter Mount wrote: > On Mon, 25 Oct 1999, John N. Alegre wrote: > >> Has anyone else had this experience. I have been reluctant to move to >> PostgrSQL for reasons of complexity and speed. > > Although I don't touch applets anymore, the problems with placing either > all of the classes into a single jar file, or extracting the classes so > they are downloaded individually are old, but tips I still give out when > people ask me about problems with browsers. > >> Is the JDBC support that much more advanced in PostgrSQL? > > I should hope so, as it takes up a lot of my time :-) > > For the next release I'm hoping to have most of the JDBC2 spec complete, > but mainly the BLOB and Array support. > >> All comments welcome. >> >> john >> >> On 24-Oct-99 Eric vanberkel wrote: >> > I've been thru this. >> > >> > You go to check you have the 1.2.2 driver and not the >> > 1.2.1, which did not work on my prev2 JDK + latest mysql >> > >> > Also for apps: >> > java -cp .:$CLASSPATH proggie >> > with a correct classpath set >> > >> > in applets $CLASSPATH won't work. >> > You'll want to put into your html: >> > >> > >> > .. >> > .. >> > >> > >> > put the mm jarfile in this tag and in the applet home dir. >> > If there is no jar, copy the mm.mysql level into the applet >> > home dir. I know it's a shame, butt... >> > >> > If you haven't got a jar you use the jar tools to pack the >> > mm.mysql classes into a jar file. >> > >> > This knowledge cost me a lotta sweat. I hope it pays off >> > for you brother... >> > >> > Oh, >> > I managed to step over the line to PostgrSQL. Support for >> > JDBC is much better. This might be a hint. They also >> > upgraded to JDBC2 faster then mysql usually does >> > >> > Good luck, >> > gr. Eric >> > >> > --- Jalaluddin Riaz <[EMAIL PROTECTED]> wrote: >> >> hi, >> >> I am a newbie to JDBC programming and am having some >> >> problems. I am >> >> using mysql rdbms and mm.mysql.jdbc-1.2 driver and have >> >> jdk1.2preV2 >> >> installed. the problem is everytime i try to run a prog. >> >> i get class not >> >> found exception. the prog is not able to find the driver >> >> class files. i have >> >> tried different CLASSPATH settings, still does no works.. >> >> any ideas. >> >> >> >> thanks. >> >> >> >> __ >> >> Get Your Private, Free Email at http://www.hotmail.com >> >> >> >> >> >> >> > -- >> >> To UNSUBSCRIBE, email to >> >> [EMAIL PROTECTED] >> >> with a subject of "unsubscribe". Trouble? Contact >> >> [EMAIL PROTECTED] >> >> >> >> >> > >> > __ >> > Do You Yahoo!? >> > Bid and sell for free at http://auctions.yahoo.com >> > >> > >> > -- >> > To UNSUBSCRIBE, email to [EMAIL PROTECTED] >> > with a subject of "unsubscribe". Trouble? Contact >> > [EMAIL PROTECTED] >> >> -- >> E-Mail: John N. Alegre <[EMAIL PROTECTED]> >> Date: 25-Oct-99 >> Time: 13:02:49 >> >> This message was sent by XFMail >> -- >> >> >> -- >> To UNSUBSCRIBE, email to [EMAIL PROTECTED] >> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED] >> > > -- >Peter T Mount [EMAIL PROTECTED] > Main Homepage: http://www.retep.org.uk > PostgreSQL JDBC Faq: http://www.retep.org.uk/postgres > Java PDF Generator: http://www.retep.org.uk/pdf > > > -- > To UNSUBSCRIBE, email to [EMAIL PROTECTED] > with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED] -- E-Mail: John N. Alegre <[EMAIL PROTECTED]> Date: 06-Nov-99 Time: 18:04:59 This message was sent by XFMail -- -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
