在异常处理的方面还需要进一步的加强和改善,下一步应该加强对正则表达式的使用。
package com.cn.tibco.time;
import java.util.Scanner;
public class GetTime {
public static void main(String args[]) {
GetTime getTime = new GetTime();
boolean ison = true;
while (ison) {
getTime.resetTime();
System.out.println("Again?(y/n)");
Scanner keyboard = new Scanner(System.in);
String yn = keyboard.next();
if (yn.equalsIgnoreCase("n")) {
ison = false;
}
}
}
public void resetTime() {
System.out.println("Enter time in 24-hour notation:");
Scanner keyboard = new Scanner(System.in);
String time = keyboard.nextLine();
System.out.println("That is the same as");
try {
isFormat(time);
int hours = getHours(time);
int minutes = getMinutes(time);
if (hours > 12 && minutes >9) {
String h1 = Integer.valueOf(hours - 12).toString();
time = h1 + ":" + getMinutes(time) ;
System.out.println(time + " pm");
} else if ( hours > 12 && minutes <10) {
String h1 = Integer.valueOf(hours - 12).toString();
time = h1 + ":0" + getMinutes(time) ;
System.out.println(time + " pm");
}else {
System.out.println(time + " am");
}
} catch (TimeFormatException e) {
e.printStackTrace();
resetTime();
}
}
//检验时间格式,还需要进一步加强格式的处理。
public void isFormat(String time) throws TimeFormatException {
if(!time.contains(":")){
throw new TimeFormatException("There is no such time as " + time);
}
int hours = getHours(time);
int minutes = getMinutes(time);
if (hours < 0 || hours > 24) {
throw new TimeFormatException("There is no such time as " + time);
} else if (minutes < 0 || minutes > 59) {
throw new TimeFormatException("There is no such time as " + time);
}
}
//得到小时数
public int getHours(String time) {
int pos;
pos = time.indexOf(":");
int hours = Integer.parseInt(time.substring(0, pos));
return hours;
}
//得到分钟数
public int getMinutes(String time) {
int pos;
pos = time.indexOf(":");
int minutes = Integer.parseInt(time.substring(pos + 1));
return minutes;
}
}
异常处理类
package com.cn.tibco.time;
public class TimeFormatException extends Exception {
public TimeFormatException(){
super();
}
public TimeFormatException(String message){
super(message);
}
@Override
public String getMessage() {
return super.getMessage();
}
}
//使用正则表达式对方法isFormat()进行修正,代码如下:
public void isFormat(String time) throws TimeFormatException {
if(!time.matches("\\d{1,2}\\:\\d{2}")){
throw new TimeFormatException("There is no such time as " + time);
}
int hours = getHours(time);
int minutes = getMinutes(time);
if (hours < 0 || hours > 24) {
throw new TimeFormatException("There is no such time as " + time);
} else if (minutes < 0 || minutes > 59) {
throw new TimeFormatException("There is no such time as " + time);
}
}
分享到:
相关推荐
可以将输入的内容转换或过滤成有正则表达式的输出表达式
C语言编写的将正则表达式转换为NFA,可以运行
其中包括windows和linux文件路径的正则表达式;去除空格和斜杠转换的replaceall方法
《学习正则表达式》从正则表达式的基本概念讲起,到编写完整的sed和Perl脚本,再到转换HTML文件,将这种强大的工具解释得清晰透彻。书中贯穿了大量简洁明了的示例,旨在让读者轻松掌握正则表达式。此外,书中各在线...
代码相对简单; c语言实现; 正则表达式转换为nfa;
正则表达式
正则表达式与自动机的转换,以及自动机的化简
正则表达式转换最小化的小工具。非常好用。输入正则表达式,则得到相应最小化DFA。
正则表达式匹配(自动转换)无需在为正则表达式而烦恼啦
用JAVA写的一个将正则表达式转换为NFA的代码,基于Thompson算法的思想,递归构建NFA。jar为源码文件。 输出非确定有限自动状态机的有向图。如正则表达式: c(a|b)NFA为:0-c->1-ep->2-a->3-ep->7 ,0-c->1-ep->4-b->5-...
JAVA实现的正则表达式转换成DFA,并将DFA用Graph画出,画图须安装Graph。
正则表达式转换为NFA,dfa,确定化 简单 方便实现
通过匹配特定单词、字符和模式,读者很快就可以自己动手使用正则表达式匹配、提取和转换文本。正则表达式是程序员必备的强大工具,得到了各种Unix实用程序,以及Perl、Java、JavaScript、C#等编程语言的支持。读完...
基于Java实现了DFA,NFA,DFA最小化,NFA转化为DFA以及正则表达式转化为NFA的算法,对于初学者来说,是学习词法分析的一份不错资源
课程设计 正规式构造nfa.这是编译原理的一个实验, 是把一个正则表达式转化为不确定有穷自动机NFA的算法程序,朋兴趣的朋友可以下载来看看哦。
硬件CRC转换工具,软件正则表达式工具
输入一个正则表达式,然后将此正则表达式转换为NFA,最后将此NFA输出 【实验要求】: 写出程序运行环境, 体会,程序结构,程序清单, 典型结果,以及结果分析 【程序运行环境】: 此程序是在Visual C++下,新建一...
编译原理课的大作业 包含三个小实验 在一个cpp文件里 正则表达式转换为nfa nfa转换为dfa dfa最小化 个人原创代码
《学习正则表达式》从正则表达式的基本概念讲起,到编写完整的sed和Perl脚本,再到转换HTML文件,将这种强大的工具解释得清晰透彻。书中贯穿了大量简洁明了的示例,旨在让读者轻松掌握正则表达式。此外,书中各在线...
C#字符串和正则表达式参考手册 目 录 第1章 系统处理文本的方式 1 1.1 .NET Framework 1 1.1.1 公共语言运行时 2 1.1.2 .NET Framework类库 3 1.2 文本是一种数据类型 4 1.2.1 C#的数据类型 5 1.2.2 字符和字符集 6 ...