如何解决在java中越界
我试图通过将输入转换为小写并取输入的第一个字母以产生“y”或“n”来清理用户给出的输入。但是,我不断抛出异常
这是我的代码片段
System.out.println("Welcome to \"My Pet Budget\" 1.2!");
System.out.println("");
System.out.println("Enter the month and year for your budget:");
monthYear = keyboard.nextLine();
System.out.println("Enter the name of your pet:");
petName = keyboard.nextLine();
System.out.println("Enter the type of your pet:");
petType = keyboard.nextLine();
System.out.println("Enter the breed of your pet:");
petBreed = keyboard.nextLine();
System.out.println("Enter the age of your pet:");
petAge = keyboard.nextInt();
// item if statements
System.out.println("Would you like to add an item? (y/n):");
String answer;
answer= keyboard.nextLine(); // lower case and use the first letter to simplify
// edge cases.
if (answer.toLowerCase().charAt(0)== 'y') {
System.out.println("Enter the name for item 1:");
itemName1 = keyboard.nextLine();
System.out.println("Enter the price for item 1:");
itemPrice1 = keyboard.nextDouble();
System.out.println("Enter the quantity for item 1:");
itemQuantity1 = keyboard.nextInt();
它会抛出一个越界异常(可能与 .charat 方法有关。
如果有人能帮忙,我会很高兴
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。