Skip to Content
Java数据科学实战
book

Java数据科学实战

by Michael R. Brzustowicz, PhD
January 2020
Intermediate to advanced
202 pages
5h 41m
Chinese
Posts & Telecom Press
Content preview from Java数据科学实战
数据的输入与输出
|
5
通常,空值已经被记录为一个空格或者一系列空格。尽管这样做有时让人烦恼,但是它可
以达到某种目的,因为把数据点不存在这一概念编码为
0
并不总是合适的。例如,如果在
记录二值变量(
0
与
1
)时遇到了一个并不知道其值的项,那么为其错误地赋值为
0
(并写
到了文件中),就会错误地把真的负例赋给这个项。在向文本文件写入空值时,我的偏好
是写入长度为
0
的字符串。
1.3.2
 空格
空格广泛存在于实际数据中。采用
String.isEmpty()
方法可以直接检查字符串是否为空
字符串。然而请注意,由空格组成的字符串(即使只有单个空格)并不是空的!首先,用
String.trim()
方法移除那些位于输入值开头部分或结尾部分的空格,然后检查它的长度。
只有在字符串长度为
0
时,
String.isEmpty()
才返回
true
。
private boolean
checkBlank(String value) {
return
value.trim().isEmpty();
}
1.3.3
 解析错误
一旦知道了字符串的值既不是空值也不是空格,就将其解析为所需要的类型。这里不讨论
如何将字符串解析为字符串,因为并没有什么需要解析的。
当处理数值类型时,把字符串直接转换为基本类型,例如
double
、
int
或
long
,是不明智
的。推荐做法是采用对象封装类,例如
Double
、
Integer
和
Long
,它们都有字符串解析方
法。如果发生错误,那么这些方法会抛出
NumberFormatException
异常。可以捕获该异常 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

云安全实用指南

云安全实用指南

Chris Dotson
Kafka权威指南

Kafka权威指南

Neha Narkhede, Gwen Shapira, Todd Palino

Publisher Resources

ISBN: 9787115533302