① java java.lang.NumberFormatException: empty String
因为你这个是报的异常
你首先应该看异常信息是什么 上面说的是
NumberFormatExceptionextends
如果你不认识你首先应该去版查JDK的帮权助文档,上面是这样写的
public class NumberFormatExceptionextends IllegalArgumentException
当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常。
看到这我想你应该明白是什么意思了吧。
也就是说你将一个可能包括非数学字符相关的字符串转成成数值,导致错误
比如说 12as34,他是不可以转成数值的。
你的代码我没有细看,我想可能是通过getText时获得了非数值字符转
看你的详见
我看可能是由
JComboBox控件引起
② 请问Java中DateFormat df=new simpleDateFormat()是什么意思
DateFormat和SimpleDateFormat都是java.text包下的类,两者的关系是:
DateFormat是抽象类,SimpleDateFormat是具体类,DateFormat是SimpleDateFormat的父内类。
由于DateFormat是抽象类,因此没法用new来构建。容而SimpleDateFormat可以。
Java的多态性,决定了SimpleDateFormat的类对象可以向上转型为DateFormat类型,因此这句是可以的。
(另外,类名首字母都是大写,你写的simpleDateFormat的首字母写错了)
③ 关于JAVA中JLIST显示问题
你这个问题是说
用JLIST.setSelctedIndex(index)方法,没有办法设置选择项吗?
下边有段简单的代码,希望对你有帮助。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JButton;
public class JListTest extends JFrame implements ActionListener {
private String pattern = "selected {0}";
private int len = 5;
private JButton btnNewButton = null;
private int index = 0;
private JList list = null;
public JListTest() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
DefaultListModel listModel1 = new DefaultListModel();
list = new JList(listModel1);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 1; i <= len; i++) {
calendar.set(Calendar.DATE, i);
listModel1.addElement(sdf.format(calendar.getTime()));
}
list.setSelectedIndex(5);
list.setBounds(23, 10, 127, 240);
getContentPane().add(list);
btnNewButton = new JButton(getBtnName());
btnNewButton.setBounds(162, 7, 148, 21);
btnNewButton.addActionListener(this);
getContentPane().add(btnNewButton);
setVisible(true);
}
private String getBtnName() {
int random = (int) (Math.random() * 1000);
index = random % len;
System.out.println(index);
return MessageFormat.format(pattern, index);
}
public static void main(String[] args) {
new JListTest();
}
public void actionPerformed(ActionEvent e) {
list.setSelectedIndex(index);
btnNewButton.setText(getBtnName());
}
}
④ java dateformat怎么转换时间格式
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HHmm");
System.out.println(sdf.format(d));
⑤ 用Java编写数字格式化程序
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class FormateBankAccountId {
List<String> standardBankAccountIdFormat ;
public FormateBankAccountId(String propertiesFileName) throws FileNotFoundException, IOException
{
// 加载资源文件
Properties properties = new Properties();
properties.load(new FileReader(propertiesFileName));
this.standardBankAccountIdFormat = new ArrayList<String>();
int keyNumber = 1;
String value = null;
// 读取键值对,键的格式是: formate_1 ,..., formate_10,...,formate_100 等
while( ( value = properties.getProperty("formate_" + keyNumber++) ) != null)
{
value = value.trim();
this.standardBankAccountIdFormat.add(value);
}
}
public List<String> formate(String orginalBankAccountId)
{
List<String> proceededlBankAccountIds = new :
⑥ java.lang.NullPointerException
AddBookAction 应该有一个Double型的参数,但是这个参数从前端传来的是null值,所以在转换的时专候出错了。 产生错误的原因是属:
Double d = Double.parseDouble(null);
⑦ java.lang.NumberFormatException: multiple points
SimpleDateFormat是非线程安全的,每次用的时候new一个
⑧ Java 格式化问题:我要写一个程序来格式化一串代表银行帐号的数字。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class FormateBankAccountId {
List<String> standardBankAccountIdFormat ;
public FormateBankAccountId(String propertiesFileName) throws FileNotFoundException, IOException
{
// 加载资源文件
Properties properties = new Properties();
properties.load(new FileReader(propertiesFileName));
this.standardBankAccountIdFormat = new ArrayList<String>();
int keyNumber = 1;
String value = null;
// 读取键值对,键的格式是: formate_1 ,..., formate_10,...,formate_100 等
while( ( value = properties.getProperty("formate_" + keyNumber++) ) != null)
{
value = value.trim();
this.standardBankAccountIdFormat.add(value);
}
}
public List<String> formate(String orginalBankAccountId)
{
List<String> proceededlBankAccountIds = new ArrayList<String>();
orginalBankAccountId = orginalBankAccountId.trim();
for(String formateString : this.standardBankAccountIdFormat)
{
// 格式字符串中的数字个数,
int formateStringDigitNum = formateString.replace("-", "").length();
//原字符串过长,截取后面部分
if(orginalBankAccountId.length() > formateStringDigitNum)
{
orginalBankAccountId = orginalBankAccountId.substring(orginalBankAccountId.length() - formateStringDigitNum);
}
//原字符串过短,前面补零
else if(orginalBankAccountId.length() < formateStringDigitNum)
{
String pre = "";
for(int i = 0 ; i < formateStringDigitNum - orginalBankAccountId.length() ; i ++) pre += "0";
orginalBankAccountId = pre + orginalBankAccountId;
}
// 格式化字符串中分隔符位置
int delimiterPosition = 0;
String temp = orginalBankAccountId;
while(-1 != (delimiterPosition = formateString.indexOf('-',delimiterPosition)))
{
// 添加分割符号
temp = temp.substring(0,delimiterPosition) + '-' + temp.substring(delimiterPosition );
delimiterPosition ++;
}
proceededlBankAccountIds.add(temp);
}
return proceededlBankAccountIds;
}
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
String orginalBankAccountId = "000111111222";
FormateBankAccountId formateTool = new FormateBankAccountId("bankAccountId.properties");
for(String proceededBankAccountId: formateTool.formate(orginalBankAccountId))
{
System.out.println(proceededBankAccountId);
}
}
}
/*
bankAccountId.properties:
formate_1 ###-######-###
formate_2 ##-####-###-##
formate_3 ####-#-###-###
formate_4 #-########-###
formate_5 ###-########-#
*/
/*
output :
000-111111-222
00-1111-112-22
0011-1-111-222
0-00111111-222
000-11111122-2
*/
⑨ java.text.messageformat.format会影响效率吗
这个坑,就出现在下面这个api的使用
java.text.MessageFormat.format(String pattern,java.lang.Object... arguments);
先来个正常的:
上代码
Code:
package chapter5;
import java.text.MessageFormat;
public class MessageFormatDemo {
public static void main(String[] args) {
String pattern = "select * from staff where salary>{0} and age>{1}";
String result = MessageFormat.format(pattern, 100, 20);
System.out.println(result);
}
}
执行下看看结果是不是:
select * from staff where salary>100 and age>20
Output:
select * from staff where salary>100 and age>20
与预期一致
OK
现在什么物价啊,salary大于100的太多了(木有少于100的好不好)
改下,要查工资大于50 000的
Code:
String result = MessageFormat.format(pattern, 50000, 20);
执行下看看结果:
select * from staff where salary>50,000 and age>20
看到没
50000变成50,000
这到数据库中肯定是不行啊
为什么出现这种情况呢
扒扒源码
java.text.MessageFormat.java的
subformat(Object[] arguments,StringBuffer result,FieldPosition fp,List characterIterators)
方法中有对输入Object数据进行格式化的逻辑
} else if (obj instanceof Number) {
// format number if can
subFormatter = NumberFormat.getInstance(locale);
} else if (obj instanceof Date) {
// format a Date if can
subFormatter = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, locale);//fix
} else if (obj instanceof String) {
arg = (String) obj;
把格式化Number的这段代码摘出来,
Format subFormatter = NumberFormat.getInstance(Locale.getDefault());
System.out.println(subFormatter.format(1000));
subFormatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());//fix
System.out.println(subFormatter.format(new Date()));
执行下看看
Output:
1,000
16-4-7 上午12:40
输出结果与预期一致,果然多了个逗号
细心的tx已经看到了,日期类型也不行,一样会格式的哦
7
如果还想使用MessageFormat应该怎么处理呢?
在上面的源码中,可以看到,在格式化时,String类型是不处理的
只要把arguments的参数改为String就可以了
Code:
String result = MessageFormat.format(pattern, "50000", 20);
⑩ 求教:运行nutch抓取网页时出错,无法抓取网页,貌似是java jdk的问题 ,jdk版本1.6 crawl.log 错误信息
不是java问题,应该是配置不对,没有找到文件/usr/local/apache-nutch-1.2/4