从整数中删除字符 [英] Remove character from integer

查看:172
本文介绍了从整数中删除字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileDataModel以格式

FileDataModel accepts data in the format

 userId,itemId,pref(long,long,Double).

目前我有一些itemId,由数字末尾的x组成。
如何编辑一些itemID,以便它删除'x'?
可以用简单的try catch语句来做到这一点吗?

At the moment I have some itemId that consist of an 'x' at the end of the number. How do I edit the some of the itemID such that it removes the 'x' ? Is it possible to do this with a simple try catch statement?

DataModel model = null;
try{
    model = new FileDataModel(new File("book_data/BX-Book-Ratings.csv"));

}catch(NumberFormatException e){
    REMOVE X


}

代码:

DataModel model = new FileDataModel(new File("book_data/BX-Book-Ratings.csv"));

错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "034545104X"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:589)
    at java.lang.Long.parseLong(Long.java:631)

$数据:

276725,034545104X,0
276726,155061224,5
276727,446520802,0
276729,052165615X,3


推荐答案

一种方法是在转换其内容的文件上运行预处理器,以确保它与 FileDataModel 兼容。如果您的数据格式非常简单,您可以使用正则表达式替换。否则我建议有一个解析器可以将数据导入内存,然后用变压器执行操作,最后使用序列化程序保存结果。

One way would be to run a pre-processor on the file that transforms it's content to ensure it is compatible with FileDataModel. If your data format is very simple you may just use a regular expression replace. Otherwise I'd suggest having a parser that can bring the data into memory, then perform your manipulation with a transformer and finally save back the results with a serializer.

File file = new File("book_data/BX-Book-Ratings.csv");
FileDataModelTransformer transformer = new FileDataModelTransformer();
transformer.transformInPlace(file);
DataModel model = new FileDataModel(file);

顺便说一下,依靠流量或缓冲区比 File更灵活

By the way, it's more flexible to rely on streams or buffers than File.

这篇关于从整数中删除字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆