例如,如果我有一个显示不同整数的文本文件,但如果它遇到一个例如有字母的值,则会抛出NumberFormatException。我已经多次看到会使用try-catch语句,除此之外还有其他方法来处理这个异常吗?这是一个名为" data"的txt文件的示例。 (注意,有三个由空格分隔的整数)
545F6 6 100
12N45 A 50
以下代码是否有效?
while (data.hasNextLine()){
data.nextInt();
if (!data.hasNextInt()){
System.out.println("The number " + data.next() + " is invalid");
data.next();
}
}
我是Java的初学者,所以我很好奇是否有其他方法可以忽略字符串,并且如果它没有返回整数则显示它是无效的。
答案 0 :(得分:0)
您可能想尝试使用正则表达式并在简单循环中使用它们来检查在空格字符分隔的给定字符串中是否只存在数字(我在注释中添加了一些解释):
The number 545F6 is invalid
Valid numbers: [6, 100]
The number 12N45 is invalid
The number A is invalid
Valid numbers: [50]
从这些循环中获得的输出:
Pattern
您可能希望详细了解如何在类ValueError Traceback (most recent call last)
/fsr/rfs07/ELEGANCE/work/code/python/s5p/animation_test.py in <module>()
25 line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
26 interval=50, blit=True)
---> 27 line_ani.save('lines.mp4', writer=writer)
28
29 fig2 = plt.figure()
/software/local/apps/python/2.7.9/lib/python2.7/site-packages/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
1061 # TODO: See if turning off blit is really necessary
1062 anim._draw_next_frame(d, blit=False)
-> 1063 writer.grab_frame(**savefig_kwargs)
1064
1065 # Reconnect signal for first draw if necessary
/software/local/apps/python/2.7.9/lib/python2.7/contextlib.pyc in __exit__(self, type, value, traceback)
33 value = type()
34 try:
---> 35 self.gen.throw(type, value, traceback)
36 raise RuntimeError("generator didn't stop after throw()")
37 except StopIteration, exc:
/software/local/apps/python/2.7.9/lib/python2.7/site-packages/matplotlib/animation.pyc in saving(self, *args, **kw)
287 yield self
288 finally:
--> 289 self.finish()
290
291 def _run(self):
/software/local/apps/python/2.7.9/lib/python2.7/site-packages/matplotlib/animation.pyc in finish(self)
307 def finish(self):
308 'Finish any processing for writing the movie.'
--> 309 self.cleanup()
310
311 def grab_frame(self, **savefig_kwargs):
/software/local/apps/python/2.7.9/lib/python2.7/site-packages/matplotlib/animation.pyc in cleanup(self)
346 def cleanup(self):
347 'Clean-up and collect the process used to write the movie file.'
--> 348 out, err = self._proc.communicate()
349 self._frame_sink().close()
350 verbose.report('MovieWriter -- '
/software/local/apps/python/2.7.9/lib/python2.7/site-packages/subprocess32.pyc in communicate(self, input, timeout)
925
926 try:
--> 927 stdout, stderr = self._communicate(input, endtime, timeout)
928 finally:
929 self._communication_started = True
/software/local/apps/python/2.7.9/lib/python2.7/site-packages/subprocess32.pyc in _communicate(self, input, endtime, orig_timeout)
1711 if _has_poll:
1712 stdout, stderr = self._communicate_with_poll(input, endtime,
-> 1713 orig_timeout)
1714 else:
1715 stdout, stderr = self._communicate_with_select(input, endtime,
/software/local/apps/python/2.7.9/lib/python2.7/site-packages/subprocess32.pyc in _communicate_with_poll(self, input, endtime, orig_timeout)
1767 select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI
1768 if self.stdout:
-> 1769 register_and_append(self.stdout, select_POLLIN_POLLPRI)
1770 stdout = self._fd2output[self.stdout.fileno()]
1771 if self.stderr:
/software/local/apps/python/2.7.9/lib/python2.7/site-packages/subprocess32.pyc in register_and_append(file_obj, eventmask)
1746 poller = select.poll()
1747 def register_and_append(file_obj, eventmask):
-> 1748 poller.register(file_obj.fileno(), eventmask)
1749 self._fd2file[file_obj.fileno()] = file_obj
1750
ValueError: I/O operation on closed file
here的Java API文档中使用正则表达式。