Java - input.next()和input.nextInt()的问题;

时间:2015-12-15 21:37:59

标签: java zip

我正在为学校做一个项目,我们在那里上课#34; Animal"和不同类型动物的子类。我们必须将动物插入ArrayList并从文件中获取输入," Unit4TakehomeInput.txt"。我遇到的问题是,当从文件中扫描输入时,它不只是扫描一行,我不明白为什么?

这是Class Animal:

package FinalProject;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public abstract class Animal implements Comparable<Animal> {
    static int count = 0;
    String name;
    int birthyear;
    double bill;
    String species;

// Default Constructor
Animal() { count++; }

// Basic Constructor
Animal(String n, int y, double b, String s) {
    count++;
    this.name = n;
    this.birthyear = y;
    this.bill = b;
    this.species = s;
}

// Get Bill Balance
double getBill() { return this.bill; }

// Set Bill Balance
void setBill(double b) { this.bill = b; }

public int compareTo(Animal other) {
    return (int)(100*(this.bill - other.bill));
}

public static void main(String[] args) throws Exception {
        ArrayList<Animal> aa = getFromFile(args[0]);
        Collections.sort(aa);
        printAnimal(aa);
        System.out.println(Animal.count);
}

static ArrayList<Animal> getFromFile(String inputFile) throws Exception {
File f = new File(inputFile);
    Scanner input = new Scanner(f);
    int total = input.nextInt();
    ArrayList<Animal> al = new ArrayList<Animal>(total);

    for (int i = 0; i < total; i++) {
        String name = input.next();
        System.out.println(name);
        int year = input.nextInt();
        double bill = input.nextDouble();
        String species = input.next();
        species.toLowerCase();
        switch (species) {
            case "dog":
                int legs = input.nextInt();
                boolean spotty = input.nextBoolean();
                Animal tmp = new Dog(name, year, bill, species, legs, spotty);
                al.add(tmp);
                break;
            case "cat":
                legs = input.nextInt();
                String eyeColor = input.next();
                tmp = new Cat(name, year, bill, species, legs, eyeColor);
                al.add(tmp);
                break;
            case "snake":
                String bloodtype = input.next();
                tmp = new Snake(name, year, bill, species, bloodtype);
                al.add(tmp);
                break;
            case "kangaroo":
                legs = input.nextInt();
                boolean pouch = input.nextBoolean();
                tmp = new Kangaroo(name, year, bill, species, legs, pouch);
                al.add(tmp);
                break;
            case "skunk":
                legs = input.nextInt();
                int stripes = input.nextInt();
                tmp = new Skunk(name, year, bill, species, legs, stripes);
                al.add(tmp);
                break;
        }
    }
    input.close();
    return al;
}

static void swapAnimal(Animal[] aa, int i, int j) {
        Animal tmp = aa[i];
        aa[i] = aa[j];
        aa[j] = tmp;
}

public String toString() {
    String x = String.format("| %-8s | %-8s | %-9d | $%-8.2f |", this.species, this.name, this.birthyear, this.bill);
    return x;
}

static void printAnimal(ArrayList<Animal> aa) {
    System.out.println("-----------------------------------------------");
    System.out.printf("| %-8s | %-8s | %-8s | %-9s | \n", "Species", "Name", "Birthyear", "Balance");
    System.out.println("-----------------------------------------------");
    for (Animal a: aa) {
        System.out.println(a);
    }
    System.out.println("-----------------------------------------------");
    }
}

这是输入文件:

2

Hopper
2003
555
Kangaroo
2
false

Kitty
2009
44
Cat
4
Green

Spot
2005
333
Dog
4
true

Johnny
2008
111
Cat
4
Blue

Striper
2011
432
Skunk
4
3

Edward
1995
630
Horse
4
true

Casper
1998
88
snake
ColdBlood

Boots
2005
987
Horse
4
false

如您所见,有许多不同类型的动物。 出于某种原因,getFromFile()中的input.next()正在扫描:

Hopper
2

而不仅仅是扫描:

Hopper

项目的完整zip包括其他动物类和输入文件,可以在以下网址下载:Download Zip Here! 非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

问题在于我没有把交换机中的所有案例都用于整个程序。谢谢@IliiazAkhmedov你的解决方案以及@CalvinP的解决方案帮助我找到了问题。

我错过了马案和默认案例。

感谢大家的帮助!