得到这个错误:" main" java.lang.NumberFormatException&#34 ;;可能是一个根本问题

时间:2018-02-04 23:56:29

标签: java encoding

我想我可以在作业前加上这个? assignment

无论如何,我几乎不理解作业,我会说实话。真的是自由球,但我已经把它归结为:

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

public class TimeTravel
{


   static final double STUDENT_DISCOUNT = .50;
   static final double EMPLOYEE_DISCOUNT = .25;

    /** 
   * Discounts described   
   *
   * @param args Command line arguments (not used).
   */ 

   public static void main(String[] args) {


      Scanner userInput = new Scanner(System.in);
      //double ticketcode;



      System.out.println("Enter ticketcode: ");
      //ticketcode = userInput.nextDouble();
      Scanner input = new Scanner(System.in);

      String ticketcode = input.nextLine();
      System.out.println((char)Integer.parseInt(ticketcode));
      int tcode = Integer.parseInt(ticketcode);

      //String s = Double.toString(ticketcode);


      if (tcode <= 26) {

         System.out.println("*** Invalid ticket code ***"
            + "\nTicket code must have at least 26 characters.");
      }

      else {


         System.out.println("Time: ");
         System.out.println(ticketcode.substring(0, 4));

         System.out.println("Date: ");
         System.out.println(ticketcode.substring(5, 12));

         System.out.println("Category: ");
         System.out.println(ticketcode.substring(0, 4));

         System.out.println("Seat: ");
         System.out.println(ticketcode.substring(27, 29));

         System.out.println("Itinerary: ");
         System.out.println(ticketcode.substring(30, 54));

         double price = Double.parseDouble(ticketcode);
         DecimalFormat df = new DecimalFormat("#,###.00");
         System.out.println("Price: " + df.format((ticketcode.substring(14, 22))));

         System.out.println(ticketcode.substring(0, 4));
         System.out.println("Cost: ");




         System.out.println("Prize Number: ");
         System.out.println(ticketcode.substring(0, 4));   



      }
   }
}

主体内的每一条评论都是我的漫无边际。

1 个答案:

答案 0 :(得分:0)

如果插入标准输入(在您的情况下为ticketcode)非整数字符,则Integer.parseInt(ticketcode)将抛出NumberFormatException。这是因为无法解析非整数输入。