Java:使用文本文件

时间:2014-05-26 12:33:14

标签: java io

我的主屏幕加载如下:

  

按1查找艺术家列表

     

按2进行预订活动

     

按3退出程序

     

菜单1:艺术家列表2:预订票3:退出请选择一个选项:1    - 3

选项1显示我存储在文本文件中的艺术家列表,这很好。但是选项2是问题所在,当我选择选项2时,它显示为:

  

菜单1:艺术家列表2:预订票3:退出请选择一个选项:1    - 3

     

2

     

提示信息:请注意,允许的最高票数   购买是6。

     

输入艺术家:beyonce

     

所需门票数量:2

     

输入客户详细信息

我的代码是:

案例2://预订场地

    String chosenArtist = " ";


    int availability = 0;

    int capacity = 0;  //for the calculation of capacity that will be left

    //is the text file that customer details are stored too
        String file_name = "/Users/petercanning/Desktop/customerFile.txt";

    WriteFile data = new WriteFile(file_name, true);

    //the prompt message displays when option 2 is selected
    System.out.println("\n" + "Prompt Message: ");
    System.out.println("Please be advised that maximum tickets allowed to be purchased is 6." + "\n");

    //input the artist name
    System.out.println("\n" + "Input Artist: ");
    chosenArtist = sc.nextLine();
    sc.nextLine();



    //error handling, if ticket is above or below number of tickets allowed
    while(numberTickets < 1 || numberTickets > 6){

    //input the number of tickets required
    System.out.println("\n" + "Number of Tickets required: ");
    numberTickets = sc.nextInt();

    if(numberTickets > 6)
    {
    //this error is displayed if the number of tickets selected is above 6
    System.out.println("Error: You have input the incorrect number of tickets allowed.");

    System.out.println("Number of Tickets required: ");
    numberTickets = sc.nextInt();
    }
    if(numberTickets < 1)
    {
    //this error is displayed if the number of tickets selected is below 1
    System.out.println("Error: You have input the incorrect number of tickets allowed.");

    System.out.println("Number of Tickets required: ");
    numberTickets = sc.nextInt();

    }

    }


    for (Artist A : artistList)

    //this is used to get the ticket price and times it by the number of tickets
    if (A.getArtist().equals(chosenArtist))
    {

    System.out.println("Total Price including fees: ��" + (A.getTicketPrice() * numberTickets + answer));
    }

    System.out.println("\n" + "Input Customer Details");

    //this is the input of customer details that will be saved to the customer file
    System.out.print("\n" + "First Name: ");
    firstName = sc.next();
    System.out.println("\n" + "Surname: ");
    secondName = sc.next();
    System.out.println("\n" + "Address: ");
    sc.nextLine();
    address = sc.nextLine();
    System.out.println("\n" + "Paymet Method: Visa/American Express/Mastercard");
    sc.nextLine();
    System.out.println("\n" + "Card Number: ");
    cardNumber = sc.nextLong();
    sc.nextLine();

    System.out.println("Payment Transaction complete ");

    System.out.println("First Name: " + firstName + "\nSurname: " + secondName + "\nAddress: " + address);

    data.writeToFile(artistList + " " + numberTickets + " " + firstName + " " + secondName + " " + address);


    System.out.println("Payment Successful");

    System.out.println("Customer booking saved!");

    System.out.print("Which artist do you wish to update - Enter Artist");
    chosenArtist = sc.next();

    for(Artist A : artistList)
    {
    if(A.getArtist().equals(chosenArtist)){
    System.out.println("Artist " + chosenArtist + " found");

    availability = (A.getAvailability());

    capacity = 1;
    }
    }

    if(capacity == 0){
    System.out.println("The person does not exist");
    }


    break;

在我输入门票数量之后的某些原因,该程序忽略了添加门票数量的部分,我无法弄清楚为什么会这样做和我也无法弄清楚容量的更新是如何工作的。

我的艺术家文字文件也如下:

  

beyonce hydro 44.40 1200

     

dan glasgow 23 120

     

BonJovi SECC 82 5000

2 个答案:

答案 0 :(得分:1)

所以这段代码不起作用......

for (Artist A : artistList)

//this is used to get the ticket price and times it by the number of tickets
if (A.getArtist().equals(chosenArtist))
{

System.out.println("Total Price including fees: ��" + (A.getTicketPrice() * numberTickets + answer));
}

显然你的'if'条件没有得到满足。我会说你的文本文件在某种程度上是不正确的,或者你正在做什么来解析文本文件中的艺术家名称并没有解析你的想法。

检查您的解析器。

答案 1 :(得分:0)

就你所说,看起来你的artistList是空的,但你应该调试你的程序以确保它。如果您有任何问题,请告诉我们您的编程环境(命令行,日食等)是什么,让我们为此寻找解决方案。