-1

我已经搜索过这个问题,但在尝试了很多之后我仍然不知所措。我对Java完全陌生,所以请原谅我的无知。我正在尝试从扫描仪读取文件。该文件包含 SmartPhone 属性,我想将它们直接分配给智能手机类变量,然后创建我的手机并将其添加到列表中。但是,电话没有被添加,我已确保文本文件采用扫描仪和类属性所期望的格式。谁能发现我的错误?

package model;
import java.io.*; 
import java.util.*; 


public class menu {
    public static void main(String[] args) {
        int inputOption;
        SmartPhone newPhone;
        LinkedList<SmartPhone> phoneList;
        FileToLinkedList smartphoneList;

        smartphoneList = new FileToLinkedList();
        phoneList = smartphoneList.createDatabase();
        System.out.println(phoneList);


        Scanner userInput = new Scanner(System.in);
        //userInput.useDelimiter(System.getProperty("line.separator"));

        int smartphoneLabel = 0;
        String model = "";
        String manufacturer = "";
        int year = 0;
        double price = 0.0;
        String color = "";


        System.out.println("==============================");
        System.out.println("   SmartPhone Database Menu   ");
        System.out.println("==============================");
        System.out.println("Options:                      ");
        System.out.println("  1.  Display all smartphones ");
        System.out.println("  2.  Add a smartphone        ");
        System.out.println("  3.  Delete a smartphone     ");
        System.out.println("  4.  Search for a smartphone ");
        System.out.println("  5.  Quit                    ");
        System.out.println("==============================");

        inputOption = KeyIn.inInt("Please select an option from the menu: ");

        switch(inputOption){
            case 1:
                System.out.println("You've selected to display the entire smartphone database.");



                break;
            case 2:
                System.out.println("You have chosen to add a smartphone to the database. ");
                System.out.println("Smartphone Label: ");
                smartphoneLabel = userInput.nextInt();
                System.out.println("Model: ");
                model = userInput.next();
                System.out.println("Manufacturer: ");
                manufacturer = userInput.next();
                System.out.println("Year: ");
                year = userInput.nextInt();
                System.out.println("Price: ");
                price = userInput.nextDouble();
                System.out.println("Color: ");
                color = userInput.next();

                newPhone = new SmartPhone(smartphoneLabel, model, manufacturer, year, price, color);
                phoneList.add(newPhone);                



                System.out.println("Here is your new database of smartphones. ");
                //phoneList.displayDatabase();

                break;

            case 3:
                System.out.println("You have chosen to delete a smartphone from the database. ");
                break;
            case 4:
                System.out.println("You have chosen to search for a particular model." );
                break;
            case 5:
                System.out.println("Goodbye!");
                break;
            default:
                System.out.println("Invalid selection.");
                break;              
        }
    }
}

package model;

import java.io.*; 
import java.util.*; 

public class FileToLinkedList extends LinkedList {
    LinkedList<SmartPhone> myPhoneList;

    public FileToLinkedList(){
        myPhoneList = new LinkedList<SmartPhone>();
    }

    public LinkedList<SmartPhone> createDatabase(){

        try{
        Scanner scanner = new Scanner("SmartPhone_Database.txt");
        while(scanner.hasNext()) {

            SmartPhone newPhone = new SmartPhone();

            newPhone.setLabel(scanner.nextInt());
            newPhone.setModel(scanner.nextLine());
            newPhone.setManufacturer(scanner.nextLine());
            newPhone.setYear(scanner.nextInt());
            newPhone.setPrice(scanner.nextDouble());
            newPhone.setColor(scanner.nextLine());
            //phoneList.add(newPhone);
            ((LinkedList<SmartPhone>)myPhoneList).add(newPhone);
        }    
    }
    catch(InputMismatchException e){}
        return myPhoneList;
    }
}

*我列出了两种建议的方法,一种被注释掉了——但仍然没有..

package model;
import java.lang.*;

public class SmartPhone {
    private int smartphoneLabel;
    private String model;
    private String manufacturer;
    private int year;
    private double price;
    private String color;

    //constructor
    public SmartPhone(){
        smartphoneLabel = 0;
        model = "";
        manufacturer = "";
        year = 0;
        price = 0.0;
        color = "";
    }

    public SmartPhone(int myLabel, String myModel, String myManufacturer, int myYear, double myPrice, String myColor)
    {
            smartphoneLabel = myLabel;
            model = myModel;
            manufacturer = myManufacturer;
            year = myYear;
            price = myPrice;
            color = myColor;
    }

    public int getLabel(){
        return smartphoneLabel;
    }

    public String getModel(){
        return model;
    }

    public String getManufacturer(){
        return manufacturer;
    }

    public int getYear(){
        return year;
    }

    public double getPrice(){
        return price;
    }

    public String getColor(){
        return color;
    }

    public void setLabel(int label){
        smartphoneLabel = label;
    }

    public void setModel(String model){
        model = model;
    }

    public void setManufacturer(String manufacturer){
        manufacturer = manufacturer;
    }

    public void setYear(int year){
        year = year;
    }

    public void setPrice(double price){
        price = price;
    }

    public void setColor(String color){
        color = color;
    }



    public void display(){
        System.out.println("Label: " + getLabel() + "\n");
        System.out.println("Model: " + getModel() + "\n");
        System.out.println("Manufacturer: " + getManufacturer() + "\n");
        System.out.println("Year: " + getYear() + "\n");
        System.out.println("Price: " + getPrice() + "\n");
        System.out.println("Color: " + getColor() + "\n");

    }
}

这是我的文本文件:

1234    NC2000  Nokia   2009    100.00  Silver
3874    CT4500  iPhone  2012    450.00  White
59780   F5600   Android 2012    475.00  Black
5983    T95000  Android 2011    500.00  Silver
23300   GH3000  Samsung 2010    275.00 Black
47000   TT2700  Samsung 2009    100.00 Silver
4

1 回答 1

0

您的问题出在 createDatabase() 的返回语句中。仅当存在 InputMismatchException 时才返回 LinkedList,当您希望每次调用该方法时都返回 myPhoneList。那应该可以为您解决问题。

于 2012-07-19T17:55:32.853 回答