java有效索引范围_Java索引超出范围:0
我拼命試圖找出一種方法來阻止“字符串索引超出范圍:0”錯誤……只要我不輸入任何內(nèi)容然后繼續(xù)執(zhí)行,就會發(fā)生這種情況:
static String getRef(Scanner Keyboard)
{
Scanner keyboard = new Scanner(System.in);
String ref= "";
boolean valid = false;
int errors = 0;
boolean problem = false;
while(valid==false)
{
System.out.println("Please enter a reference number which is two letters followed by three digits and a letter(B for business accounts and N for non business accounts)");
ref = keyboard.nextLine();
for (int i=0; i<6; i++)
{
if (ref.charAt(i)=='\0')
{
problem = true;
}
}
if(problem == true)
{
System.out.println("The reference must consist of 6 Characters");
}
else
{
if ((Character.isDigit(ref.charAt(0))== true) || (Character.isDigit(ref.charAt(1))== true))
{
System.out.println("The first 2 characters must be letters");
errors = errors + 1;
}
if ((Character.isDigit(ref.charAt(2))== false) || (Character.isDigit(ref.charAt(3))== false)||(Character.isDigit(ref.charAt(4))== false))
{
System.out.println("The 3rd,4th and 5th characters must be numbers");
errors = errors + 1;
}
if ((!ref.toUpperCase().endsWith("B"))&&(!ref.toUpperCase().endsWith("N")))
{
System.out.println("The 6th character must be either B(for business accounts) or N(for non business accounts) ");
errors = errors + 1;
}
if (errors==0)
{
valid=true;
}
}
}
return ref;
}
我想要的是,如果字符串不包含特定索引處的字符,則能夠輸出錯誤消息,例如ref.charAt(0).
這將有所幫助(例子):
if(ref.charAt(aNumber)==null) {
// display error message
}
總結(jié)
以上是生活随笔為你收集整理的java有效索引范围_Java索引超出范围:0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java简单投票系统_JSP实现的简单W
- 下一篇: java如何输出线程的标识符_Java多