Ok, I m丢失。 我必须说明如何证实愤怒,但出于某种原因,我只能使用Try-Catch方法。 我知道这是最容易的方法,因此互联网上的所有解决办法都在使用。
Java。
交易是这样,我需要有人以数字身份识别和密码。 如果两个投入中的任何一个都无效,我必须告诉它们,它们是错误的。
谁能帮助我?
Ok, I m丢失。 我必须说明如何证实愤怒,但出于某种原因,我只能使用Try-Catch方法。 我知道这是最容易的方法,因此互联网上的所有解决办法都在使用。
Java。
交易是这样,我需要有人以数字身份识别和密码。 如果两个投入中的任何一个都无效,我必须告诉它们,它们是错误的。
谁能帮助我?
如果我正确理解你的话,你就会看到一种愤怒或从标准投入中加以扼杀,你希望证实,愤怒实际上是一种愤怒。 http://java.sun.com/a>。 如同你的任务一样,它禁止使用例外处理方法(因此我正确理解这一点),因此不允许你利用这一内在功能,必须加以执行。
Ok. 因此,由于这是家庭工作,我不会给你完整的回答,但这里是假装:
let result = 0 // accumulator for our result let radix = 10 // base 10 number let isneg = false // not negative as far as we are aware strip leading/trailing whitespace for the input string if the input begins with + : remove the + otherwise, if the input begins with - : remove the - set isneg to true for each character in the input string: if the character is not a digit: indicate failure otherwise: multiply result by the radix add the character, converted to a digit, to the result if isneg: negate the result report the result
这里的关键是,每一位数的分数比位数直接高出几倍,因此,如果我们总是用红.乘,我们就可以正确看到str,那么每位数就具有适当的意义。 现在,如果我错了,而且你实际上能够使用捕捉物,但只是粗略地指出:
int result = 0; boolean done = false; while (!done){ String str = // read the input try{ result = Integer.parseInt(str); done = true; }catch(NumberFormatException the_input_string_isnt_an_integer){ // ask the user to try again } }
并非确切地确定,正在验证的<代码>String应当核对内容是否只包含编号,或者是否可在int
的有效范围内实际代表。 解决这一问题的一个途径是围绕String
的特性加以调整,并检查这些特性只是由数字构成。
由于这似乎属于家务劳动,因此,这里的假体编码很少:
define isANumber(String input):
for (character in input):
if (character is not a number):
return false
return true
您可使用>java.util.Scanner
和hasNextInt(
)>,以核实
作为一种额外特征,它能够跳出白色空间,容忍额外的垃圾(你可以检查)。
String[] ss = {
"1000000000000000000",
" -300 ",
"3.14159",
"a dozen",
"99 bottles of beer",
};
for (String s : ss) {
System.out.println(new Scanner(s).hasNextInt());
} // prints false, true, false, false, true
我用 Java1.6,为我工作:
if (yourStringVariable.trim().matches("^\d*$"))
那么,你就会发现一种积极的愤怒。
Pattern p = Pattern.compile("\d{1,}");
Matcher m = p.matcher(inputStr);
boolean isNumber = false;
if (m.find()) {
isNumber = true;
}
http://java.sun.com/javase/7/docs/api/java/util/Scanner.html 请注意,每种类型的方法(例如:hasNextBigDecimal ) 而不仅仅是Next.
EDIT:无,只有在斯堪的内尔过早关闭的情况下,才会放弃无效的投入。
public static boolean IsNumeric(String s)
{
if (s == null)
return false;
if (s.length() == 0)
return false;
for (int i = 0; i < s.length(); i++)
{
if (!Character.isDigit(s.charAt(i)))
return false;
}
return true;
}
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...