简体   繁体   English

为什么我的变量没有从用户输入中存储? JAVA

[英]Why aren't my variables being stored from the user input? JAVA

I've got this program here that is supposed to take in this user input and become a taco sorter.我这里有这个程序,它应该接受这个用户输入并成为一个 taco 分拣机。 However, it's like the user isn't putting in any information to the console as it just prints out the default values of "none", "none" and "0.0" when the code should be taking this user input and be able to sort it out and print the inputted information.但是,就像用户没有向控制台输入任何信息一样,它只是打印出默认值“none”、“none”和“0.0”,而代码应该接受此用户输入并能够排序它出来并打印输入的信息。 Any help would be great任何帮助都会很棒

Taco.java:塔可.java:

public class Taco {
    private String name;
    private String location;
    private double price;
    public Taco() {
        this.name = this.location = "none";
        this.price = 0.0;
    }
    
    //parameterized constructor
    public Taco(String aName, String aLocation, double aPrice) {
        this.setName(aName);
        this.setLocation(aLocation);
        this.setPrice(aPrice);
    }
    
    //accessors
    public String getName() {
        return this.name;
    }
    public String getLocation() {
        return this.location;
    }
    public double getPrice() {
        return this.price;
    }
    
    //mutators
    public void setName(String aName) {
        if(aName != null)
            this.name = aName;
        this.name = "none";
    }
    public void setLocation(String aLocation) {
        if(aLocation != null)
            this.location = aLocation;
        this.location = "none";
    }
    public void setPrice(double aPrice) {
        if(aPrice >= 0.0)
            this.price = aPrice;
        this.price = 0.0;
    }
    
    //toString and .equals
    public String toString() {
        return "Name: "+this.name+" Location: "+this.location+" Price: $"+this.price;
    }
    public boolean equals(Taco aTaco) {
        return aTaco != null && 
                this.name.equals(aTaco.getName()) &&
                this.location.equals(aTaco.getLocation()) && 
                this.price == aTaco.getPrice();
    }
}

TacoManager.java: TacoManager.java:

import java.util.Scanner;
import java.io.*;
public class TacoManager {
    //instance variable
    private Taco[] tacos;
    //constants
    public static final int DEF_SIZE = 10;
    public static final String DELIM = "\t"; //delimiter
    public static final int BODY_FIELD_AMT = 3;
    public static final int HEADER_FIELD_AMT = 2;
    
    //CONSTRUCTORS
    // --- default constructor ---
    public TacoManager() {
        init(DEF_SIZE);
    }
    
    // --- parameterized constructor ---
    public TacoManager(int size) {
        init(size);
    }
    
    //initialization method; 
    public void init(int size) {
        if(size >= 1)
            tacos = new Taco[size];
        else
            tacos = new Taco[DEF_SIZE];
    }
    
    //adding method
    public void addTaco(Taco aTaco) {
        //check if taco array is full
        if(tacos[tacos.length-1] != null)
            return;
        //find the first empty space
        for(int i = 0; i < tacos.length; i++) {
            if(tacos[i] == null) {
                tacos[i] = aTaco;
                break;
            }
        }
        this.sortTacos();
    }
    
    //remove method
    public void removeTaco(String aName) {
        int removeIndex = -1; //set to an index that doesn't exist for a check later
        //search for element trying to remove by name
        for(int i = 0; i < tacos.length; i++) {
            if(tacos[i] != null && tacos[i].getName().equals(aName)) {
                removeIndex = i;
                break;
            }
        }
        if(removeIndex == -1) //taco was never found
            return;
        else { //taco was found so shift everything to the left by 1
            for(int i = removeIndex; i < tacos.length-1; i++)
                tacos[i] = tacos[i+1];
            //make sure the last index is ALWAYS null;
            tacos[tacos.length-1] = null;
        }
    }
    
    //sorting using bubble sort
    private void sortTacos() {
        boolean swapped = true;
        while(swapped == true) {
            swapped = false;
            for(int i = 0; i < tacos.length-1; i++) {
                if(tacos[i+1] == null) {
                    break; //checks if the next elements is null or not; if it is, the loop has to be stopped
                }
                if(tacos[i].getPrice() > tacos[i+1].getPrice()) { //out of order, swap! compare first taco and its price to its neighbor
                    Taco temp = tacos[i];
                    tacos[i] = tacos[i+1];
                    tacos[i+1] = temp;
                    swapped = true;
                }
            }
        }
    }
    
    //write to a file!!!!!!
    public void writeTacoFile(String aName) {
        try {
            PrintWriter fileWriter = new PrintWriter(new FileOutputStream(aName));
            
            //Header
            fileWriter.println("Taco Amt:"+DELIM+tacos.length);
            
            //Body
            for(Taco taco : tacos) {
                if(taco == null)
                    break;
                fileWriter.println(taco.getName()+DELIM+taco.getLocation()+DELIM+taco.getPrice());
            }
            fileWriter.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    //read from this taco file!!!
    public void readTacoFile(String aName) {
        try {
            Scanner fileScanner = new Scanner(new File(aName));
            
            //read the header
            String fileLine = fileScanner.nextLine();
            String[] splitLines = fileLine.split(DELIM);
            if(splitLines.length == HEADER_FIELD_AMT) {
                int size = Integer.parseInt(splitLines[1]);
                init(size);
            }
            else
                return;
            //read the body!
            while(fileScanner.hasNextLine()) {
                fileLine = fileScanner.nextLine();
                splitLines = fileLine.split(DELIM);
                if(splitLines.length == BODY_FIELD_AMT) {
                    String name = splitLines[0];
                    String location = splitLines[1];
                    double price = Double.parseDouble(splitLines[2]);
                    Taco aTaco = new Taco(name, location, price);
                    this.addTaco(aTaco);
                }
            }
            fileScanner.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    //print method
    public void printTacos() {
        for(Taco taco : tacos) {
            if(taco == null)
                break;
            System.out.println(taco);
        }
    }
}

TacoManagerFE.java: TacoManagerFE.java:

/*
 * written by thomas scholz
 */
import java.util.Scanner;
public class TacoManagerFE {

    private static Scanner keyboard = new Scanner(System.in); //defined here because it needs to be used across any other methods that we develop
    private static TacoManager tacoManager = new TacoManager();
    public static void main(String[] args) {
        printGreeting();
        boolean quit = false;
        while(!quit) {
            printChoices();
            int choice = keyboard.nextInt();
            keyboard.nextLine();
            switch(choice) { //could be if, else if, etc
            case 1:
                addTaco();
                break;
            case 2:
                removeTaco();
                break;
            case 3:
                readTacoFile();
                break;
            case 4:
                writeTacoFile();
                break;
            case 9:
                quit = true;
                break;
                default:
                    System.out.println("Invalid input");
            }
            tacoManager.printTacos();
        }

    }
    
    //greeting
    public static void printGreeting() {
        System.out.println("Welcome to the Taco Manager");
    }
    
    //print choices
    public static void printChoices() {
        System.out.println("Enter 1 to add a taco\n"
                + "Enter 2 to remove a taco\n"
                + "Enter 3 to read a taco database file\n"
                + "Enter 4 to write to a taco database file\n"
                + "Enter 9 to quit");
    }
    
    //prompt user add taco method
    public static void addTaco() {
        System.out.println("Enter the name of the taco");
        String name = keyboard.nextLine();
        System.out.println("Enter the location of the taco");
        String location = keyboard.nextLine();
        System.out.println("Enter the price of the taco");
        double price = keyboard.nextDouble();
        keyboard.nextLine();
        tacoManager.addTaco(new Taco(name,location,price));     
    }
    
    //prompt user remove taco method
    public static void removeTaco() {
        System.out.println("Enter the name of the taco to remove");
        String name = keyboard.nextLine();
        tacoManager.removeTaco(name);
    }
    
    //read from a taco database file method
    public static void readTacoFile() {
        System.out.println("Enter the file name to read a TacoDB");
        String fileName = keyboard.nextLine();
        tacoManager.readTacoFile(fileName);
    }
    
    //write to a taco file method
    public static void writeTacoFile() {
        System.out.println("Enter the file name to write a TacoDB file");
        String fileName = keyboard.nextLine();
        tacoManager.writeTacoFile(fileName);
    }
}

Here is the output from the console:这是来自控制台的 output:

Name: none Location: none Price: $0.0
Enter 1 to add a taco
Enter 2 to remove a taco
Enter 3 to read a taco database file
Enter 4 to write to a taco database file
Enter 9 to quit

You have the errors in these lines:您在这些行中有错误:

    public void setName(String aName) {
        if(aName != null)
            this.name = aName;
        this.name = "none";
    }
    public void setLocation(String aLocation) {
        if(aLocation != null)
            this.location = aLocation;
        this.location = "none";
    }
    public void setPrice(double aPrice) {
        if(aPrice >= 0.0)
            this.price = aPrice;
        this.price = 0.0;
    }

The error is that always is executed the lines: this.location= "none" this.price = 0.0 this.name = "none"错误是总是执行以下行: this.location this.location= "none" this.price = 0.0 this.name = "none"

You have to add an else statement:您必须添加一个 else 语句:

    public void setName(String aName) {
        if(aName != null)
            this.name = aName;
        else this.name = "none";
    }
    public void setLocation(String aLocation) {
        if(aLocation != null)
            this.location = aLocation;
        else this.location = "none";
    }
    public void setPrice(double aPrice) {
        if(aPrice >= 0.0)
            this.price = aPrice;
        else this.price = 0.0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM