-1

私はこの質問を検索しましたが、多くのことを試してもまだ途方に暮れています。私はJavaにまったく慣れていないので、無知を許してください。スキャナーからファイルを読み込もうとしています。ファイルにはスマートフォンの属性が含まれており、それらをスマートフォンのクラス変数に直接割り当ててから、自分の電話を作成してリストに追加したいと考えています。ただし、電話が追加されていないため、テキスト ファイルがスキャナとクラスの属性が期待する形式であることを確認しました。誰かが私の間違いを見つけることができますか?

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

*提案された2つの方法をリストしました.1つはコメントアウトされましたが、まだ何もありません..

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() の return ステートメントにあります。メソッドが呼び出されるたびに myPhoneList を返したい場合は、InputMismatchException がある場合にのみ LinkedList を返します。これで問題が解決するはずです。

于 2012-07-19T17:55:32.853 に答える