0

我有一个应该吐出数组内容的方法。但是,当我运行它时,它会无限循环。

这是我的代码:

    public void displayAll() {
    for (int i = 0; i < cars.length; i++) {

        System.out.println(cars[i]);

    }

它出什么问题了?

编辑:这是Java,顺便说一句。这是我的整个代码:

主.java:

package csc;
import java.util.*;
import java.io.*;
public class Main {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {


    System.out.println("Welcome to the Car Database");

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the size of the array:");
    int input = keyboard.nextInt();
    keyboard.nextLine();
    CarDatabase cdb = new CarDatabase(input);



    System.out.println("Enter the name of the input file:");
    cdb.readFile(keyboard.next());

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter make, mpg, weight, all, or quit:");
    String command = sc.nextLine();


    while(!command.equals("quit")) {

        if(command.equals("all")) {
            cdb.displayAll();
//some other methods
} }

汽车.java:

    package csc;
public class Car {

    String model;
    String make;
    double mpg;
    int weight;
    int year;

    public Car(String carModel, String carMake, double carMpg, int carWeight, int carYear) {
        model = carModel;
        make = carMake;
        mpg = carMpg;
        weight = carWeight;
        year = carYear;
    }
    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return ("Model:" + model + " Make:" + make + " mpg:" + mpg + " weight:" + weight + " year:" + year);
    }
}

汽车数据库.java

    package csc;
import java.io.File;
import java.util.*;

public class CarDatabase {

    private Car[] cars;

    public CarDatabase(int s) {
        cars = new Car[s];
    }


    public void readFile(String a) throws Exception{
       Scanner sc = new Scanner(new File(a));
       Scanner sc2;
       for (int i = 0; i < cars.length; i++) {
           if (sc.hasNextLine()) {
               sc2 = new Scanner(sc.nextLine());
               sc2.useDelimiter(",");
               cars[i] = new Car(sc2.next(),sc2.next(),sc2.nextDouble(),sc2.nextInt(),sc2.nextInt());
               //System.out.println(cars[i]);
           }

       }

    }

    public void displayAll() {
        for (int i = 0; i < cars.length; i++) {

            System.out.println(cars[i]);

        }

    } //some other methods

目标是获取一个 csv 文件并将对象放入数组中,并让用户搜索特征。例如:品牌、型号、mpg。但是我的数组确实无限循环,即使我告诉它是 10 个插槽或类似的小东西。

以下是具有 10 个插槽的 netbeans 的一些输出:

    Welcome to the Car Database
Enter the size of the array:
10
Enter the name of the input file:
cardb.csv
Enter make, mpg, weight, all, or quit:
all
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70
Model:torino Make:ford mpg:17.0 weight:3449 year:70
Model:galaxie 500 Make:ford mpg:15.0 weight:4341 year:70
Model:impala Make:chevrolet mpg:14.0 weight:4354 year:70
Model:fury iii Make:plymouth mpg:14.0 weight:4312 year:70
Model:catalina Make:pontiac mpg:14.0 weight:4425 year:70
Model:ambassador dpl Make:amc mpg:15.0 weight:3850 year:70
Model:chevelle malibu Make:chevrolet mpg:18.0 weight:3504 year:70
Model:skylark 320 Make:buick mpg:15.0 weight:3693 year:70
Model:satellite Make:plymouth mpg:18.0 weight:3436 year:70
Model:rebel sst Make:amc mpg:16.0 weight:3433 year:70

无穷无尽。

4

3 回答 3

1

执行后您不会向用户查询新命令displayAll,也许?这意味着command将保留"all",这意味着程序会displayAll一遍又一遍地执行。尝试移动线路

String command = sc.nextLine();

在命令循环内。

于 2013-10-27T18:54:21.163 回答
0

通过在主类的while循环后重新提示用户解决。

if(command.equals("all")) {
     cdb.displayAll();
     sc.nextLine();
}
于 2013-10-27T21:13:34.250 回答
0

你确定程序挂在这个地方吗?我看不出循环有什么问题。尝试使用调试器或调试日志来定位问题。

于 2013-10-26T00:17:04.863 回答