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

时间:2015-09-03 18:41:52

标签: java arrays file-io linked-list

我在打开和读取一个简单的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

Llewellyn Mark

1 15 19 26 33 46

Young Brian

17 19 33 34 46 47

Guha Arup

1 4 9 16 25 36

Siu Max

17 19 34 46 47 48

Balci Murat

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

  }

}

1 个答案:

答案 0 :(得分:0)

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

因此,使PurchaserList静态应该修复它。