我的for循环不会中断,无限打印数组

时间:2013-10-26 00:02:32

标签: java loops for-loop

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

这是我的代码:

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

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

    }

它出了什么问题?

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

Main.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
} }

Car.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);
    }
}

CarDatabase.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个插槽或类似的东西。

这是netbeans的10个插槽的输出:

    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

Ad无限。

3 个答案:

答案 0 :(得分:1)

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

String command = sc.nextLine();

在命令循环中。

答案 1 :(得分:0)

你确定程序在这个地方挂起吗?我没有看到循环有什么问题。尝试使用调试器或调试日志记录来本地化问题。

答案 2 :(得分:0)

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

if(command.equals("all")) {
     cdb.displayAll();
     sc.nextLine();
}