0

配列の内容を吐き出すことになっているメソッドがあります。ただし、実行すると無限ループします。

これが私のコードです:

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

車データベース.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 に答える