Java 读取文本文件并将其与用户输入进行比较的程序

Java Program that reads in text file and compares it to user input

我在打开一个简单的 input.txt 文件并将其读取到我在计算机科学 类 中创建的 Java 程序时遇到了一些问题(基本前提该程序是从文件中获取彩票数据,例如玩家姓名和他们的彩票号码,并将其与作为输入提供的用户号码进行比较)。首先,当我编译并尝试 运行 程序 (Eclipse IDE) 时,它看不到 input.txt 文件,即使它位于同一目录中。我必须在命令行中输入 "src/Assignment1/input.txt" 才能正确找到文件。我查看了 Java 的 Path 实用程序,看看是否可以解决它,但不确定如何实现该代码或是否有更简单的方法。

此外,当程序确实找到文件时,我使用 readFile 方法将文本文件中的第一个数字作为售出的门票数量,然后创建一个链表来保存每个人的数据。我在如下所示的 readFile 方法的第二个嵌套 for 循环中遇到编译错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type lottery is accessible. Must qualify the allocation with an enclosing instance of type lottery (e.g. x.new A() where x is an instance of lottery).

    at Assignment1.lottery.readFile(lottery.java:89)
    at Assignment1.lottery.main(lottery.java:214)

我提供了下面 input.txt 中内容的副本以及我的代码,如有任何指导,我们将不胜感激!

5

卢埃林马克

1 15 19 26 33 46

年轻的布莱恩

17 19 33 34 46 47

古哈奥雅纳

1 4 9 16 25 36

小麦克

17 19 34 46 47 48

巴尔奇穆拉特

5 10 17 19 34 47

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class lottery {

    // Enumeration method that holds constants for how many matches correlate to how much in winnings
    public enum PayOut 
    {
        MATCHTHREE(10), MATCHFOUR(100), MATCHFIVE(10000), MATCHSIX(1000000);

        private int winnings;

        public int getval()
        {
            return winnings;
        }

        private PayOut(int amount)
        {
            this.winnings = amount;
        }
    }

    // Method call to create linked list with purchaser's data in each node
    static LinkedList<PurchaserList> purchasers = new LinkedList<>();

    // Data structure for purchaser information, contained in each node in linked list
    public class PurchaserList
    {
        public String lastName;
        public String firstName;
        public int[] numbers;

        public PurchaserList (String last, String first, int[] nums)
        {
            lastName = last;
            firstName = first;
            numbers = nums;
        }

        public String getLastName()
        {
            return lastName;
        }

        public String getFirstName()
        {
            return firstName;
        }

        public int[] getNumbers()
        {
            return numbers;
        }
    }

    // Method to open and read in data from input file
    public static void readFile (String file) throws IOException
    {
        File fileName = new File(file);
            try
            {
                Scanner in = new Scanner(fileName);

                int numTickets = in.nextInt();

                for(int i = 0; i < numTickets; i++)
                {
                   String lastName = in.next();
                   String firstName = in.next();

                   int[] numbers = new int[6];

                   for(int j = 0; j < 6; j++)
                   {
                        numbers[j] = in.nextInt();
                   }

                   PurchaserList current = new PurchaserList(lastName, firstName, numbers);
                   purchasers.add(i, current);

                }

                in.close();
            }

            catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }
    }

    public static void compareNumbers(int[] winningNumbers)
    {
        // Creates array to hold winning numbers
        List<Integer> winningNums = new ArrayList<>(); //creates a list to store all the winning numbers

        // For loop to store winning numbers in prior array
        for(int num : winningNumbers)
        {
            winningNums.add(num);
        }

        //For loop to go through each purchaser in the linked list and get their lottery numbers
        for (PurchaserList purchaser : purchasers)
        {
            // Generating list that stores all the numbers from the current purchaser
            List<Integer> playerNums = new ArrayList<>();

            // For loop to add numbers to the list
            for(int num : purchaser.getNumbers())
            {
                playerNums.add(num);
            }

            //Converts list in to a set
            Set<Integer> similar = new HashSet<>(playerNums);

            //Compares set of winning numbers to purchaser's numbers, keeps similar values
            similar.retainAll(winningNums);

            //Calls enum method to look for winning amount based on the numbers matched, returns enumeration
            PayOut matchesFound = matches(similar.size());

            //Only display winnings if matches are 3 or greater
            if(matchesFound != null)
            {
                displayPrices(matchesFound, purchaser.getFirstName(), purchaser.getLastName(), similar.size());
            }
        }

    }

    public static PayOut matches(int num)
    {
        PayOut numMatches;

        if(num == 3)
        {
            numMatches = PayOut.MATCHTHREE;
        }

        else if(num == 4)
        {
            numMatches = PayOut.MATCHFOUR;
        }

        else if(num == 5)
        {
            numMatches = PayOut.MATCHFIVE;
        }

        else if(num == 6)
        {
            numMatches = PayOut.MATCHSIX;
        }

        else
        {
            return null;
        }

        return numMatches;
    }

    private static void displayPrices(PayOut numMatches, String first, String last, int matches)
    {
        switch(numMatches)
        {
            case MATCHTHREE:
                System.out.printf("%n%s %s matched %d numbers and won $%d. %n", first, last, matches, PayOut.MATCHTHREE.getval());
                break;

            case MATCHFOUR:
                System.out.printf("%n%s %s matched %d numbers and won $%d. %n", first, last, matches, PayOut.MATCHFOUR.getval()); 
                break;

            case MATCHFIVE:
                System.out.printf("%n%s %s matched %d numbers and won $%d.%n", first, last, matches, PayOut.MATCHFIVE.getval());
                break;

            case MATCHSIX:
                System.out.printf("%n%s %s matched %d numbers and won $%d. %n", first, last, matches, PayOut.MATCHSIX.getval());
                break;

            default:
                System.out.println("None of the players matched any numbers %n");
                break;
        }
    }

    // Main method
    public static void main(String[] args) throws IOException
    {

        Scanner scan = new Scanner(System.in);

        // Asking user for input file
      System.out.println("Enter the name of the file with the ticket data: ");

      String fileName = scan.nextLine();

      //Method call to open and read in data from input file
      readFile(fileName);

      // Creates array to store winning lottery numbers
      int[] winningNumbers = new int[6];

      // Asking user for winning lottery numbers, in ascending order
      System.out.println("Enter the winning Lottery numbers (in ascending order separated by spaces): ");

      // For loop to scan in winning lottery numbers to array
      for(int i = 0; i < 6; i++)
      {
          winningNumbers[i] = scan.nextInt();
      }

      //Method call to compare winning numbers to purchaser's numbers
      compareNumbers(winningNumbers);

      // Close input file
      scan.close();

  }

}

PurchaserList class 不是静态的。因此,它必须像 lotteryInstance.new PurchaserList() 一样访问(因为它不是静态的,它属于 Lottery 实例)。

因此,将 PurchaserList 设置为静态应该可以解决这个问题。