我的代码没有打印

时间:2015-02-04 03:11:00

标签: java database class methods printing

我的代码(读取文本文件,使用我构建的类来对数据进行排序,然后输出到控制台上),不打印任何内容!有人可以告诉我我的小错误在哪里!我知道非常结束还没有结束。请帮忙!!!!!!!!

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Project02 {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    ArrayList<Product> products = new ArrayList<Product>();
    // Enter file name
    System.out.print("Enter database file name: ");
    String fileName = in.nextLine();
    try {
        File file = new File(fileName);
        Scanner inputFile = new Scanner(file);
        System.out.println();
        while (inputFile.hasNext()) {
            Product p = new Product();
            String title = inputFile.nextLine();
            String code = inputFile.nextLine();
            Integer quantity = inputFile.nextInt();
            Double price = inputFile.nextDouble();
            inputFile.nextLine();
            String type = inputFile.nextLine();
            Integer userReview = inputFile.nextInt();
            // read in title
            p.setName(title);
            // read in iCode
            p.setInventoryCode(code);
            // read in quantity
            p.setQuantity(quantity);
            // read in price
            p.setPrice(price);
            // read in type
            p.setType(type);
            // read in user reviews
            while (!userReview.equals(-1)) {
                p.addUserRating(userReview);
                userReview = inputFile.nextInt();
            }
            if (inputFile.hasNext()) {
                inputFile.nextLine();
            }
        }
        inputFile.close();
    } catch (IOException e) {
        System.out.println("There was an error reading from " + fileName);
    }

}

private static String highRating(ArrayList<Product> p) {
    int highestR = 0;
    int indexOfHighestR = 0;
    for (int i = 0; i < p.size(); i++) {
        int rating = p.get(i).getAvgUserRating();
        if (p.get(i).getAvgUserRating() > highestR) {
            highestR = p.get(i).getAvgUserRating();
            indexOfHighestR = i;
        }
    }
    int zero = 0;
    String Star = " ";

    while (highestR > zero) {
        Star = Star + "*";
        zero--;
    }
    String highestRateTitle = p.get(indexOfHighestR).getName() + "  ("
            + Star + ")";

    return highestRateTitle;
}

private static String lowestRating(ArrayList<Product> p) {
    int lowestR = 0;
    int indexOfLowestR = 0;
    for (int i = 0; i < p.size(); i++) {
        int rating = p.get(i).getAvgUserRating();
        if (p.get(i).getAvgUserRating() < lowestR) {
            lowestR = p.get(i).getAvgUserRating();
            indexOfLowestR = i;
        }
    }
    int zero = 0;
    String Star = " ";

    while (lowestR > zero) {
        Star = Star + "*";
        zero--;
    }
    String highestRateTitle = p.get(indexOfLowestR).getName() + "  ("
            + Star + ")";

    return highestRateTitle;
}

private static double maxDollar(ArrayList<Product> p) {
    double largestP = 0;
    int indexOfLargestP = 0;
    for (int i = 0; i < p.size(); i++) {
        double price = p.get(i).getPrice();
        if (p.get(i).getPrice() > largestP) {
            largestP = p.get(i).getPrice();
            indexOfLargestP = i;
        }
    }
    return largestP;
}

private static int minDollar(ArrayList<Product> p) {
    double smallestP = p.get(0).getPrice();
    int indexOfSmallestP = 0;
    for (int i = 0; i < p.size(); i++) {
        if (p.get(i).getPrice() < smallestP) {
            smallestP = p.get(i).getPrice();
            indexOfSmallestP = i;
        }
    }
    return indexOfSmallestP;
}

private static void inventoryList(ArrayList<Product> p) {
    int count = 
    System.out.println("Product Summary Report: ");
    System.out
            .println("------------------------------------------------------------");
    for (int i = 0; i < count; i++) {
        System.out.println("Title: " + p.get(i).getName());
        System.out.println("I Code: " + p.get(i).getInventoryCode());
        System.out.println("Product Type: " + p.get(i).getType());
        System.out.println("Rating: " + p.get(i).getAvgUserRating());
        System.out.println("# Rat.: " + p.get(i).getUserRatingCount());
        System.out.println("Quantity: " + p.get(i).getQuantity());
        System.out.println("Price: " + p.get(i).getPrice());
        System.out.println();
    }
    System.out
            .println("-----------------------------------------------------------------");
    // System.out.println("Total products in database: " + count);
    System.out.println("Highest total dollar item: "
            + p.get(maxDollar(p)) + " ($"+ p.(maxDollar(p)) + ")");
    System.out.println("Smallest quantity item: "
            + p.get(minQuantity(quantities)) + " ("
            + types.get(minQuantity(quantities)) + ")");
    System.out.println("Lowest total dollar item: "
            + titles.get(minDollar(prices)) + " ($"
            + prices.get(minDollar(prices)) + ")");
    System.out
            .println("-----------------------------------------------------------------");
}

1 个答案:

答案 0 :(得分:1)

首先...

在您创建Productp的实例后,您需要将其添加到products列表中,否则您将丢失它的参考,将无法使用再次使用它......

while (inputFile.hasNext()) {
    Product p = new Product();
    //...
    products.add(p);
    if (inputFile.hasNext()) {
        inputFile.nextLine();
    }
}

其次...

您需要将products List传递给想要使用/显示信息的内容,例如inventoryList ...

但等等,那不行?

如果我们仔细看看inventoryList方法......

    int count = 0;
    //...
    for (int i = 0; i < count; i++) {

我们可以看到count始终是0,因此它永远不会打印任何内容!您应该使用p.size()代替,products List

的实际长度
    //...
    for (int i = 0; i < p.size(); i++) {