Meaningless Drivel is fun!
The moose likes Java in General and the fly likes How to Convert an HTML PARAM into a Java Double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Convert an HTML PARAM into a Java Double" Watch "How to Convert an HTML PARAM into a Java Double" New topic

How to Convert an HTML PARAM into a Java Double

Jason Foster

Joined: Feb 13, 2006
Posts: 1
This is the HTML:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
Applet Test Page
<h2 align="center"><span style="font-weight: 400"> <font face="Courier New" size="2">App.html</font></span></h2>
<h2 align="center"><b>Applet Test Page</b></h2>
<p align="center">This page launches a 300 x 200 pixel
applet named:   <font face="Courier New" size="2">App.class</font></p>
<p align="center">

<PARAM NAME=Date VALUE="5/4/2007">

<p align="left">NOTES:</p>
<ol style="margin-top: 18">
<p style="margin-bottom: 18">If the applet doesn't run, make
sure your browser is Java-enabled.</p></li>
<p style="margin-bottom: 18">Some browsers, such as <i>Internet Explorer</i>, may require "plug-ins".
Other browsers, such as <i>Mozilla Firefox</i>, may not.</p></li>
<p align="left"> </p>
</HTML><!-- text below generated by server. PLEASE REMOVE --><!-- Counter/Statistics data collection code --><script language="JavaScript" src=""></script><script language="javascript">geovisit();</script><noscript><img src="" alt="setstats" border="0" width="1" height="1"></noscript>

And this is the Java:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.*;
import java.lang.*;

public class App extends Applet {
public void init() {
resize(250, 50);
setLayout(new BorderLayout());
String rateAsString = getParameter("finChg"); // RETRIEVE HTML PARAMETER
double rate = valueOf("getParameter("finChg")"); // CONVERT TO PRIMITIVE FORM ???HOW???
NumberFormat nf = NumberFormat.getPercentInstance();
Label message = new Label("Finance charge: " + nf.format(rate));
message.setFont(new Font("SansSerif", Font.BOLD, 18));
add(message, BorderLayout.CENTER);
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

double rate = Double.parseDouble(rateAsString);

Note that this throws NumberFormatException.
[ February 13, 2006: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
Its simple really.
double rate = (new Double(Double.parseDouble(finChg))).doubleValue()
Convert the String to Double and then convert it back to primitive type.
I agree. Here's the link:
subject: How to Convert an HTML PARAM into a Java Double
It's not a secret anymore!