Exception in thread "main" java.lang.NoClassDefFoundError: xxx (wrong name:xxx)

今天用Eclipse编了个小程序,功能是将输入的金额转换成大写表示,比如我们转账的时候,输入1000,会提示“壹仟元整”。

程序包括一个class,里面有4个函数:

convert()--主转换函数

getInteger()--获取输入数字的整数部分

getDecimal()--获取输入数字的小数部分

numReplace()--替换掉尾部的0的数


代码如下:

package convert;

import java.text.DecimalFormat;
import java.util.Scanner;

public class MoneyConvert {
	
	public static final String STR_UNIT[] = { "", "拾", "佰", "仟", "万", "拾",  
        "佰", "仟", "亿", "拾", "佰", "仟" };  
    public static final String STR_NUMBER[] = { "零", "壹", "贰", "叁", "肆", "伍",  
        "陆", "柒", "捌", "玖" };  
    public static final String STR_UNIT1[] = { "角", "分", "厘" };
	
	public static void main(String[] args) {
		try {
			Scanner scan = new Scanner(System.in);
			System.out.println("请输入转账金额:");
			String convert = convert(scan.nextDouble());
			System.out.println("请确认金额:" + convert);
			scan.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String convert(double d) {
		    DecimalFormat df = new DecimalFormat("#0.###");
		    String strNum = df.format(d);
		    
		    if(strNum.indexOf(".") != -1) { //-1 means strNum has no ".".
		    	String num = strNum.substring(0, strNum.indexOf("."));//取整数部分
		    	
		    	if(num.length() > 12) {
		    		System.out.println("金额大于千亿,不能转账!");
		    		System.exit(0);
		    	}
		    }
		    if(strNum.indexOf(".") == -1) {
		    	String num = strNum.substring(0);
		    	if(num.length() > 12) {
		    		System.out.println("金额大于千亿,不能转账!");
		    		System.exit(0);
		    	}
		    }
		    String point = "";
		    if(strNum.indexOf(".") != -1) {
		    	point = "元";
		    }else {
		    	point = "元整";
		    }
		    
		    String result = getInterger(strNum) + point + getDecimal(strNum);
		    if(result.startsWith("元")) {
		    	result = result.substring(1, result.length());
		    }
		    return result;
	}
	
	public static String getInterger(String num) {
		if (num.indexOf(".") != -1) {
			num = num.substring(0, num.indexOf("."));
		}
		num = new StringBuffer(num).reverse().toString();
		StringBuffer temp = new StringBuffer();
		
		for(int i = 0; i < num.length(); i++) {
			temp.append(STR_UNIT[i]);
			temp.append(STR_NUMBER[num.charAt(i) - 48]);
		}
		num = temp.reverse().toString(); // 反转字符串
		num = numReplace(num, "零拾", "零"); // 替换字符串的字符
		num = numReplace(num, "零佰", "零"); // 替换字符串的字符
		num = numReplace(num, "零仟", "零"); // 替换字符串的字符
		num = numReplace(num, "零万", "万"); // 替换字符串的字符
		num = numReplace(num, "零亿", "亿"); // 替换字符串的字符
		num = numReplace(num, "零零", "零"); // 替换字符串的字符
		num = numReplace(num, "亿万", "亿"); // 替换字符串的字符
		
		if(num.indexOf("零") == num.length() - 1) {
			num = num.substring(0, num.length() - 1);
		}
		return num;
	}
	
	public static String getDecimal(String num) {
		if (num.indexOf(".") == -1) {
			return "";
		}
		num = num.substring(num.indexOf(".") + 1);//获取.之后的数字 
		num = new StringBuffer(num).toString();
		
		StringBuffer temp = new StringBuffer();
		for (int i = 0; i < num.length(); i++) {
			temp.append(STR_NUMBER[num.charAt(i) - 48]);
			temp.append(STR_UNIT1[i]);
		}
		
		num = temp.toString(); // 替换字符串的字符
		num = numReplace(num, "零角", "零"); // 替换字符串的字符
		num = numReplace(num, "零分", "零"); // 替换字符串的字符
		num = numReplace(num, "零厘", "零"); // 替换字符串的字符
		num = numReplace(num, "零零", "零"); // 替换字符串的字符
		
		if (num.lastIndexOf("零") == num.length() - 1)
			num = num.substring(0, num.length() -1);
		return num;
	}
	
	public static String numReplace(String num, String oldStr, String newStr) {
		while (true) {
			if(num.indexOf(oldStr) == -1) break;
			num = num.replaceAll(oldStr, newStr);
		}
		return num;
	}
}

如果在Linux命令运行,有点需要注意,因为代码里有

package convert;
而我的编译文件又是放在了Eclipse默认的bin目录下,如下:

[root@sparkmaster bin]# ll convert/MoneyConvert.class 
-rw-r--r--. 1 root root 4075 Feb 20 02:25 convert/MoneyConvert.class
[root@sparkmaster bin]# pwd
/root/workspace/Classic300/bin

所以,命令行里运行的时候,必须先去到class文件所在的包convert所在的目录,即MoneyConvert是类名,而这个类在convert这个package中,而MoneyConvert.class所在目录是/root/workspace/Classic300/bin/convert/MoneyConvert.class,所以我得进入/root/workspace/Classic300/bin去运行java convert/MoneyConvert才正确:

[root@sparkmaster bin]# java convert/MoneyConvert
请输入转账金额:
10000
请确认金额:壹万元整

否则,均会报如题所示错误:Exception in thread "main" java.lang.NoClassDefFoundError: MoneyConvert (wrong name: convert/MoneyConvert) 或者 Error: Could not find or load main class convert.MoneyConvert


后补

稍作修改之后,我把main函数单独写到一个文件里面,所以convert包括2个文件:MoneyConvert.java和RunConvert.java, 这样也算是更符合OOP吧

导出convert包为jar包之后,在Linux上运行:

[root@sparkmaster bin]# cd /opt/myjars/
[root@sparkmaster myjars]# ll
total 12
-rw-r--r--. 1 root root 3184 Feb 20 07:28 moneyconvert.jar
-rw-r--r--. 1 root root 6939 Jan 21 22:10 spark-wordcount-in-scala.jar
[root@sparkmaster myjars]# java -jar moneyconvert.jar 
请输入转账金额:
10000
请确认金额:壹万元整


简单明了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞鸿踏雪Ben归来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值