获取StringIndexOutOfBoundsException

时间:2016-01-02 15:14:11

标签: java arraylist

这是我的代码:

a.setLayout(null);

当我运行 //Ticket parent class import java.util.ArrayList; import java.util.Scanner; import java.io.*; public class Ticket { public Ticket() { seatArray = new ArrayList<TicketObject>(); } public void loadIn() { //generic seating for plays and concerts seatArray.add(new TicketObject("A1", 40)); seatArray.add(new TicketObject("A2", 40)); seatArray.add(new TicketObject("A3", 40)); seatArray.add(new TicketObject("A4", 40)); seatArray.add(new TicketObject("A5", 40)); seatArray.add(new TicketObject("B1", 35)); seatArray.add(new TicketObject("B2", 35)); seatArray.add(new TicketObject("B3", 35)); seatArray.add(new TicketObject("B4", 35)); seatArray.add(new TicketObject("B5", 35)); } public String getSeats(int x) { return seatArray.get(x).getName() + " $" + seatArray.get(x).getPrice(); } protected ArrayList<TicketObject> seatArray; } //Concert ticket child class import java.util.ArrayList; import java.util.Scanner; import java.io.*; public class ConcertTicket extends Ticket { public ConcertTicket() { super(); } public void loadIn() { super.loadIn(); //Special option for concerts seatArray.add(new TicketObject("Backstage Pass", 100)); seatArray.add(new TicketObject("Backstage Pass", 100)); seatArray.add(new TicketObject("Backstage Pass", 100)); seatArray.add(new TicketObject("Backstage Pass", 100)); seatArray.add(new TicketObject("Backstage Pass", 100)); } } //Play ticket child class import java.util.ArrayList; import java.util.Scanner; import java.io.*; public class PlayTicket extends Ticket { public PlayTicket() { super(); } public void loadIn() { super.loadIn(); //Specialized seating for plays seatArray.add(new TicketObject("Box 1", 150)); seatArray.add(new TicketObject("Box 2", 150)); seatArray.add(new TicketObject("Box 3", 150)); seatArray.add(new TicketObject("Box 4", 150)); seatArray.add(new TicketObject("Box 5", 150)); } } //Ticket object for each seat; also used to populate array public class TicketObject { public TicketObject(String inSeatName, int inSeatPrice) { seatName = inSeatName; seatPrice = inSeatPrice; } public String getName() { return seatName; } public int getPrice() { return seatPrice; } private String seatName; private int seatPrice; } //Price calculation class public class Calculations { public static double addTax(int total) { return total * 1.07; } } //Tester class import java.util.Scanner; public class TicketTester { public static void main(String[] args) { Scanner in = new Scanner(System.in); try { //Introduction for user System.out.println("Welcome to the ticket purchasing program!"); System.out.println("You will be able to purchase either a concert or play ticket"); System.out.println("\n"); System.out.println("Type concert or play to display available seats and deals"); running = true; //Displaying tickets of either a concert or play String answer = in.next(); if (answer.equalsIgnoreCase("Concert")) { ConcertTicket journeyConcert = new ConcertTicket(); journeyConcert.loadIn(); System.out.println("Seat Price"); for(int i=0;i<15;i++) { System.out.println(journeyConcert.getSeats(i)); } System.out.println("\n"); System.out.println("Type in a seat name/option and press enter to reserve it."); System.out.println("Type purchase and press enter to finalize your ticket purchase"); } else if(answer.equalsIgnoreCase("Play")) { PlayTicket catsPlay = new PlayTicket(); catsPlay.loadIn(); System.out.println("Seat Price"); for(int i=0;i<15;i++) { System.out.println(catsPlay.getSeats(i)); } System.out.println("\n"); System.out.println("Type in a seat name/option and press enter to reserve it."); System.out.println("Type purchase and press enter to finalize your ticket purchase"); } else System.out.println("Enter a valid input"); //Adding up chosen seat numbers and costs while(running) { String seatChoice = in.next(); if ((seatChoice.substring(0,1)).equalsIgnoreCase("A") && seatChoice.length() == 2) { total = total + 40; seats = seats + 1; } else if ((seatChoice.substring(0,1)).equalsIgnoreCase("B")&& seatChoice.length() == 2) { total = total + 35; seats = seats + 1; } else if ((seatChoice.substring(0,3)).equalsIgnoreCase("Box")&& seatChoice.length() == 5) { total = total + 150; seats = seats + 1; } else if ((seatChoice.substring(0,14)).equalsIgnoreCase("Backstage Pass") && seatChoice.length() == 14) { total = total + 100; seats = seats + 1; } else if (seatChoice.equalsIgnoreCase("Purchase")) { System.out.println("You reserved " + seats + " seats at a price of $" + Calculations.addTax(total)); running = false; } else System.out.println("Enter a valid input"); } } finally { if(in!=null) in.close(); } } private static Boolean running; private static int total = 0; private static int seats = 0; } 课程时,一切都正常运行,直到我尝试添加后台通行证或票房座位。我收到这个错误:

TicketTester

由此我可以看到错误发生在以下行,但我不知道如何解决它。

"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 14    at java.lang.String.substring(Unknown Source)   at TicketTester.main(TicketTester.java:67)"

1 个答案:

答案 0 :(得分:3)

&&从左到右计算语句。因此,可以在短于14个符号的substring(0,13)上调用String,从而导致异常。您可以通过交换长度和相等性检查来修复它:

if (seatChoice.length() == 14 && (seatChoice.substring(0, 13)).equalsIgnoreCase("Backstage Pass"))

但是,如果您检查length()为14,则无法使用substring,因此您可以将其重写为:

if (seatChoice.equalsIgnoreCase("Backstage Pass"))