-1

I am extremely new to Java programming and have run into a roadblock for a school assignment. My program works in JGRASP and does not produce any errors. I get all the correct outputs. But when I submit the assignment I get the following error. I can't figure out what is wrong.

A run-time error occurred when running the SpaceTicket program: java.lang.StringIndexOutOfBoundsException: String index out of range: -23. Please test your program at home and fix the error before resubmitting. (2 occurrences)

The desired input is supposed to be:

12579500s15300701209817DSpaceX-001 Earth to Mars

Code:

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.Random;



public class SpaceTicket
{


static final double STUDENT_DISCOUNT = 0.25;
static final double CHILD_DISCOUNT = 0.35;



public static void main(String[] args)
{

  Scanner userInput = new Scanner(System.in);
  String ticketCode = "";
  String price = "";
  String spaceTicket = "";

  double priceParse = 0;
  double cost = 0;
  int ranNum = 0;


  System.out.print("Enter ticket code: ");
  ticketCode = userInput.nextLine();
  ticketCode = ticketCode.trim();

  if (ticketCode.length() < 25) {
     System.out.println("*** Invalid ticket code ***");
     System.out.println("Ticket code must have at least 25 
 characters.");
  }
  else {
     price = ticketCode.substring(0, 6);
     priceParse = Double.parseDouble(price);


     if (ticketCode.charAt(8) == 's') {
        cost = priceParse - (priceParse * STUDENT_DISCOUNT);
     }
     else if (ticketCode.charAt(8) == 'c') {
        cost = priceParse - (priceParse * CHILD_DISCOUNT);
     }
     else {
        cost = priceParse;
     }
  }


  spaceTicket = ticketCode.substring(24, ticketCode.length());
  System.out.println("Space Ticket: " + spaceTicket);

  System.out.println("");

  System.out.println("Date: " + ticketCode.substring(13, 15) + "/"
     + ticketCode.substring(15, 17) + "/" + ticketCode.substring(17, 
21)
     + "   Time: " + ticketCode.substring(9, 11) + ":"
     + ticketCode.substring(11, 13) + "   Seat: "
     + ticketCode.substring(21, 24));

  DecimalFormat df = new DecimalFormat("$#,##0.00");
  priceParse = Double.parseDouble(price);   
  System.out.println("Price: " + df.format(priceParse)
     + "   Category: " + ticketCode.charAt(8)
     + "   Cost: " + df.format(cost));

  Random generator = new Random();
  ranNum = generator.nextInt(999999) + 1;
  System.out.print("Prize Number: " + ranNum);



}

}
halfer
  • 18,701
  • 13
  • 79
  • 158
AndrewT
  • 31
  • 1
  • 2
  • Add the complete stacktrace, What is the input? – Jens Sep 08 '18 at 12:49
  • You should stop your program in the part ` if (ticketCode.length() < 25) {` – Jens Sep 08 '18 at 12:51
  • You asked about how to debug such problems. Yes, the duplicated question is about slightly different exceptions, but all the advice and practices are the same, no matter if we talk arrays or strings. – GhostCat Sep 08 '18 at 14:02

2 Answers2

0

Your error is based on substring method. Before calling ticketCode.substring method, you control length of the ticketCode. If ticketCode's length is smaller than 24, you take following error:String index out of range: -23

Gökhan Memiş
  • 168
  • 1
  • 7
0

Stop your program if ticketCode.length() < 25

if (ticketCode.length() < 25) {
     System.out.println("*** Invalid ticket code ***");
     System.out.println("Ticket code must have at least 25 
 characters.");
    return;
  }

else the statement

spaceTicket = ticketCode.substring(24, ticketCode.length());

throws the exception

in your case looks like the ticket length is 1 so ticketCode.substring(24,1); is called

Jens
  • 60,806
  • 15
  • 81
  • 95