我本周必须为家庭作业写这个DateConverter程序。该程序应该以月/日的格式将用户输入作为字符串,然后将输入转换为字母数字(即1/21到1月21日)。程序一直运行,直到用户输入“exit”退出。我知道我必须使用while循环来执行此操作,但每次尝试运行并退出时,它都会抛出:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Exit"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DateConverter.Date(DateConverter.java:60)
at DateConverter.main(DateConverter.java:115)
我用来退出程序的代码是:
while (true) {
System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");
if (sc.equals("Exit") || sc.equals("exit")) {
System.out.println("Goodbye");
System.exit(0);
我已经将if语句移动到几个地方以尝试清除异常,但它不会接受它。我也用Google搜索并查看了与此类似的其他几个问题,并且我已经尝试根据这些建议修复程序,但没有任何工作。有谁能请我指出正确的方向?这是我的其余代码:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class DateConverter {
static final ArrayList<Integer> THIRTY_DAYS = new ArrayList<>(Arrays.asList(4, 6, 9, 11));
static void Date() {
Scanner sc = new Scanner(System.in);
String month = null;
System.out.println("Welcome to the date converter!");
while (true) {
System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");
if (sc.equals("Exit") || sc.equals("exit")) {
System.out.println("Goodbye");
System.exit(0);
}
String[] input = sc.nextLine().split("/");
int[] dateInput = new int[input.length];
for (int i = 0; i < input.length; i++) {
dateInput[i] = Integer.parseInt(input[i]);
}
if (dateInput[0] == 1) {
month = "January";
} else if (dateInput[0] == 2) {
month = "February";
} else if (dateInput[0] == 3) {
month = "March";
} else if (dateInput[0] == 4) {
month = "April";
} else if (dateInput[0] == 5) {
month = "May";
} else if (dateInput[0] == 6) {
month = "June";
} else if (dateInput[0] == 7) {
month = "July";
} else if (dateInput[0] == 8) {
month = "August";
} else if (dateInput[0] == 9) {
month = "September";
} else if (dateInput[0] == 10) {
month = "October";
} else if (dateInput[0] == 11) {
month = "November";
} else if (dateInput[0] == 12) {
month = "December";
}
try {
if (dateInput[0] > 12 || dateInput[0] <= 0) {
throw new MonthException();
} else if (dateInput[0] <= 5 && dateInput[1] <= 5 ) {
throw new InputException();
} else if (THIRTY_DAYS.contains(dateInput[0]) && dateInput[1] > 30) {
throw new DayException();
} else if (dateInput[1] > 31 || dateInput[1] <= 0) {
throw new DayException();
} else if (dateInput[0] == 2 && dateInput[1] > 29) {
throw new DayException();
}
System.out.println("The date is " + month + " " + dateInput[1]);
} catch (MonthException ex) {
System.out.println(ex.getMessage());
} catch (DayException ex) {
System.out.println(ex.getMessage());
} catch (InputException ex) {
System.out.println(ex.getMessage());
}
}
}
public static void main(String[] args) {
DateConverter.Date();
}
}
class MonthException extends Exception {
private String month;
MonthException() {
super("Month Exception: Months must be between 1 and 12 inclusively.");
this.month = month;
}
public String getMonth() {
return month;
}
}
class InputException extends Exception {
private String input;
InputException() {
super("Input Exception: The inputed date is in the wrong format.");
this.input = input;
}
public String getInput() {
return input;
}
}
class DayException extends Exception {
private String day;
DayException() {
super("Day Exception: This day is in the wrong range for the month provided.");
this.day = day;
}
public String getDay() {
return day;
}
}
提前谢谢!
修改 这是更新代码的片段,其中包含错误。如果我输入日期,程序将抛出异常,但退出正常。我的印象是我的所有代码都需要在循环中才能继续运行,但也许我错了。有人能指出我正确的方向吗?如果我不理解,那就很抱歉。我仍处于Java编程的第一阶段。
static void Date() {
Scanner sc = new Scanner(System.in);
String month = null;
System.out.println("Welcome to the date converter!");
while (true) {
System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");
// Quits program if user enters "exit"
String inputStr = sc.nextLine();
if (inputStr.equalsIgnoreCase("Exit")) {
System.out.println("Goodbye");
System.exit(0);
}
String[] input = sc.nextLine().split("/");
int[] dateInput = new int[input.length];
for (int i = 0; i < input.length; i++) {
dateInput[i] = Integer.parseInt(input[i]);
}
错误:
Welcome to the date converter!
Enter a numeric date formatted as month/day or "Exit " to quit.
1/21
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DateConverter.Date(DateConverter.java:47)
at DateConverter.main(DateConverter.java:104)
答案 0 :(得分:1)
sc
是扫描程序对象而不是String
我猜你要提示String
所以......
System.out.println("Enter a numeric date formatted as month/day or \"Exit\" to quit.");
String inputStr = sc.nextLine();
if (inputStr.equalsIgnoreCase("Exit")) {
System.out.println("Goodbye");
System.exit(0);
}
// more validation could be done here
String[] input = inputStr.split("/");
您还可以捕获抛出的异常以重新提示用户correct
输入
答案 1 :(得分:-1)
如果import tensorflow as tf
import cv2
import numpy as np
import tensorflow.contrib.slim.nets as nets
slim = tf.contrib.slim
with tf.device('/gpu:1'):
inputs = tf.placeholder(tf.float32, shape=[None,299,299,3])
with slim.arg_scope(nets.inception.inception_v3_arg_scope()):
features,net = nets.inception.inception_v3(inputs=inputs, num_classes=1001)
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement=True
with tf.Session(config=config) as sess:
saver.restore(sess, 'weights/inception_v3.ckpt')
img = cv2.imread('images/dog_ball.jpg')
img = cv2.resize(img,(299,299))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img/255.0
curr_features, curr_net = sess.run([features, net], feed_dict={inputs: [img,img, img]})
for curr_feature in curr_features:
f_ind = np.argsort(curr_feature)[-4:] # inceptionv3
for i in f_ind:
print i
print ' '
不是input
且无法解析,则下面部分代码将抛出NumberFormatException。
integer