導航:首頁 > 編程語言 > javaformatlist

javaformatlist

發布時間:2021-02-23 16:57:02

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

閱讀全文

與javaformatlist相關的資料

熱點內容
app美團如何下載 瀏覽:197
弄畫框用什麼app 瀏覽:814
java獲取網頁圖片 瀏覽:193
jsp集合對象轉json 瀏覽:231
文件櫃在cad裡面長啥樣 瀏覽:554
iphone手機文件保存在哪裡 瀏覽:817
解壓文件後要刷新 瀏覽:786
cc資料庫怎麼獲得時間 瀏覽:226
ug3d硬料開出怎麼編程 瀏覽:151
如何獲取文件Linux命令 瀏覽:981
大智慧軟體哪個版本最好 瀏覽:698
狼人殺自動主持app叫什麼 瀏覽:949
checkbox怎麼綁定資料庫 瀏覽:945
編程怎麼設置一分鍾開燈 瀏覽:754
如何把桌面文件發送到自己郵箱 瀏覽:498
校園網站怎麼看選修的課 瀏覽:59
大數據專業哪個最好 瀏覽:467
一個文件內容替換另一個文件 瀏覽:288
ios8最好的版本 瀏覽:400
錄屏決定文件大小的是什麼 瀏覽:322

友情鏈接