请教个问题 我这有个java application程序 完成RMB小写转大写功能
现在我想在一个WEB页面通过jsp+servlet完成 在第一个文本框输入小写 点下按钮 在第二个文本框中输出大写以下是程序:<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="Change" method="post">
<table>
<tr>
<td>
请输入一个数字:
</td>
<td>
<input type="text" name="number" size="50"/>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="执行转换"><br/></td>
</tr>
<tr>
<td>
</td>
<td>
<input type="text" name="result" size="50"/>
</td>
</tr>
</table>
</form>
</body>
</html>这是servletpackage test;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Change extends HttpServlet {
public Change() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void init() throws ServletException {
// Put your code here
}}
这是java小程序package test;public class Trans2RMB {
public static void main(String[] args) {
System.out.println("\n--------将数字转换成中文金额的大写形式------------\n");
Trans2RMB t2r = new Trans2RMB();
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
if(!"".equals(s)) {
System.out.println("转换成中文后为:" + s);;
}
System.out.println("\n---------------------------------------------");
}
private String getNum() {
String s = null;
System.out.println("请输入一个数字(精确到小数点后两位):");
java.util.Scanner scanner = new java.util.Scanner(System.in);
s = scanner.next();
scanner.close();
if(this.checkNum(s)) {
return s;
} else {
return "";
}
}
private boolean checkNum(String s) {
try {
float f = Float.valueOf(s);
if(f < 0) {
System.out.println("非法数据,请检查!");
return false;
}else {
return true;
}
} catch (NumberFormatException e) {
System.out.println("非法数据,请检查!");
return false;
}
}
private String splitNum(String s) {
if("".equals(s)) {
return "";
}
int index = s.indexOf(".");
String intOnly = s.substring(0, index);
String part1 = this.numFormat(1, intOnly);
String smallOnly = s.substring(index + 1);
String part2 = this.numFormat(2, smallOnly);
String newS = part1 + part2;
return newS;
}
private String roundString(String s) {
if("".equals(s)) {
return "";
}
double d = Double.parseDouble(s);
d = (d * 100 + 0.5) / 100;
s = new java.text.DecimalFormat("##0.000").format(d);
int index = s.indexOf(".");
String intOnly = s.substring(0, index);
if(intOnly.length() > 13) {
System.out.println("输入数据过大!(整数部分最多13位!)");
return "";
}
String smallOnly = s.substring(index + 1);
if(smallOnly.length() > 2) {
String roundSmall = smallOnly.substring(0, 2);
s = intOnly + "." + roundSmall;
}
return s;
}
private String numFormat(int flag, String s) {
int sLength = s.length();
String bigLetter[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String unit[] = {"圆", "拾", "佰", "仟", "万",
"拾", "佰", "仟",
"亿", "拾", "佰", "仟", "万"};
String small[] = {"分", "角"};
String newS = "";
for(int i = 0; i < sLength; i ++) {
if(flag == 1) {
newS = newS + bigLetter[s.charAt(i) - 48] + unit[sLength - i - 1];
} else if(flag == 2) {
newS = newS + bigLetter[s.charAt(i) - 48] + small[sLength - i - 1];
}
}
return newS;
}
private String cleanZero(String s) {
if("".equals(s)) {
return "";
}
while(s.charAt(0) == '零') {
s = s.substring(2);
if(s.length() == 0) {
return "零";
}
}
String regex1[] = {"零仟", "零佰", "零拾"};
String regex2[] = {"零亿", "零万", "零元"};
String regex3[] = {"亿", "万", "元"};
String regex4[] = {"零角", "零分"};
for(int i = 0; i < 3; i ++) {
s = s.replaceAll(regex1[i], "零");
}
for(int i = 0; i < 3; i ++) {
s = s.replaceAll("零零零", "零");
s = s.replaceAll("零零", "零");
s = s.replaceAll(regex2[i], regex3[i]);
}
for(int i = 0; i < 2; i ++) {
s = s.replaceAll(regex4[i], "");
}
s = s.replaceAll("亿万", "亿");
return s;
}
}我想请教下 如何通过servlet来实现呢?? 新人 请多包涵。。
现在我想在一个WEB页面通过jsp+servlet完成 在第一个文本框输入小写 点下按钮 在第二个文本框中输出大写以下是程序:<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="Change" method="post">
<table>
<tr>
<td>
请输入一个数字:
</td>
<td>
<input type="text" name="number" size="50"/>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="执行转换"><br/></td>
</tr>
<tr>
<td>
</td>
<td>
<input type="text" name="result" size="50"/>
</td>
</tr>
</table>
</form>
</body>
</html>这是servletpackage test;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Change extends HttpServlet {
public Change() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void init() throws ServletException {
// Put your code here
}}
这是java小程序package test;public class Trans2RMB {
public static void main(String[] args) {
System.out.println("\n--------将数字转换成中文金额的大写形式------------\n");
Trans2RMB t2r = new Trans2RMB();
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
if(!"".equals(s)) {
System.out.println("转换成中文后为:" + s);;
}
System.out.println("\n---------------------------------------------");
}
private String getNum() {
String s = null;
System.out.println("请输入一个数字(精确到小数点后两位):");
java.util.Scanner scanner = new java.util.Scanner(System.in);
s = scanner.next();
scanner.close();
if(this.checkNum(s)) {
return s;
} else {
return "";
}
}
private boolean checkNum(String s) {
try {
float f = Float.valueOf(s);
if(f < 0) {
System.out.println("非法数据,请检查!");
return false;
}else {
return true;
}
} catch (NumberFormatException e) {
System.out.println("非法数据,请检查!");
return false;
}
}
private String splitNum(String s) {
if("".equals(s)) {
return "";
}
int index = s.indexOf(".");
String intOnly = s.substring(0, index);
String part1 = this.numFormat(1, intOnly);
String smallOnly = s.substring(index + 1);
String part2 = this.numFormat(2, smallOnly);
String newS = part1 + part2;
return newS;
}
private String roundString(String s) {
if("".equals(s)) {
return "";
}
double d = Double.parseDouble(s);
d = (d * 100 + 0.5) / 100;
s = new java.text.DecimalFormat("##0.000").format(d);
int index = s.indexOf(".");
String intOnly = s.substring(0, index);
if(intOnly.length() > 13) {
System.out.println("输入数据过大!(整数部分最多13位!)");
return "";
}
String smallOnly = s.substring(index + 1);
if(smallOnly.length() > 2) {
String roundSmall = smallOnly.substring(0, 2);
s = intOnly + "." + roundSmall;
}
return s;
}
private String numFormat(int flag, String s) {
int sLength = s.length();
String bigLetter[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String unit[] = {"圆", "拾", "佰", "仟", "万",
"拾", "佰", "仟",
"亿", "拾", "佰", "仟", "万"};
String small[] = {"分", "角"};
String newS = "";
for(int i = 0; i < sLength; i ++) {
if(flag == 1) {
newS = newS + bigLetter[s.charAt(i) - 48] + unit[sLength - i - 1];
} else if(flag == 2) {
newS = newS + bigLetter[s.charAt(i) - 48] + small[sLength - i - 1];
}
}
return newS;
}
private String cleanZero(String s) {
if("".equals(s)) {
return "";
}
while(s.charAt(0) == '零') {
s = s.substring(2);
if(s.length() == 0) {
return "零";
}
}
String regex1[] = {"零仟", "零佰", "零拾"};
String regex2[] = {"零亿", "零万", "零元"};
String regex3[] = {"亿", "万", "元"};
String regex4[] = {"零角", "零分"};
for(int i = 0; i < 3; i ++) {
s = s.replaceAll(regex1[i], "零");
}
for(int i = 0; i < 3; i ++) {
s = s.replaceAll("零零零", "零");
s = s.replaceAll("零零", "零");
s = s.replaceAll(regex2[i], regex3[i]);
}
for(int i = 0; i < 2; i ++) {
s = s.replaceAll(regex4[i], "");
}
s = s.replaceAll("亿万", "亿");
return s;
}
}我想请教下 如何通过servlet来实现呢?? 新人 请多包涵。。
Trans2RMB t2r = new Trans2RMB();
String result = t2r.cleanZero(t2r.splitNum(t2r.roundString(request.getParameter("number").trim())));
request.setAttribute("result", result);
request.getRequestDispatcher("你的JSP文件名").forward();2 配置Servlet的url-pattern为Change3 JSP页面中,
<input type="text" name="result" size="50"/>
这行改成
<input type="text" name="result" size="50" value="${result}"/>
String result = t2r.cleanZero(t2r.splitNum(t2r.roundString(request.getParameter("number").trim())));
request.setAttribute("result", result);
request.getRequestDispatcher("你的JSP文件名").forward();
这几句放入doPost()中报错
还有forword那里提示 not applicable for the arguments()...