Java:使用文本文件

标签 java io

我的主屏幕加载如下:

Press 1 to find Artist List

Press 2 to Book Event

Press 3 to Exit Program

Menu 1: Artist List 2: Book Ticket 3: Exit Please select an option: 1 - 3

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

Menu 1: Artist List 2: Book Ticket 3: Exit Please select an option: 1 - 3

2

Prompt Message: Please be advised that maximum tickets allowed to be purchased is 6.

Input Artist: beyonce

Number of Tickets required: 2

Input Customer Details

我的代码是:

案例2://预订 field

    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

最佳答案

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

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));
}

显然你的“如果”条件没有得到满足。我想说的是,您的文本文件在某种程度上不正确,或者您从文本文件中解析艺术家姓名所做的任何操作都没有解析您认为的方式。

检查你的解析器。

关于Java:使用文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23870271/

相关文章:

java - 无响应的 HTTP 处理程序

c - 当N个socket与一个监听socket连接时,epoll_wait()会通知进程多少次?

python - 使用 urllib 和 pil 调整 url 图像大小

java - ArrayList添加在android中不起作用

java - 对软件的哪个部分进行哪种测试?

java - 在构造函数中使用对可重写方法的引用是否安全?

java - 提升 json :Custom serializer for java 8 LocalDateTime throwing mapping exception

java - 将用户输入文本复制到输出文本文件(java)

java - 使用Java访问类路径中特定文件夹中的文件

python - 如何在不等待输入的情况下检查可能为空的标准输入?