这是我的主要类,我无法弄清楚为什么我得到一个数组越界错误。我知道它为什么会发生什么。任何帮助,将不胜感激。它只是给了我这个错误,我无法弄清楚为什么它这样做,如果你想知道这也是文本文件
制作:别克
模型:富豪
门:4
制造:现代
模型:Veloster
门:3
制作:SmartCar 模型:硬顶 门:2
制造:丰田
模型:塔科马
门:2
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cars;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Alex Masse
*/
public class Main {
public static void main(String[] args) {
Cars myCar = new Cars("Buick\t", "Regal\t", 4);
Cars impala = new Cars("Chevy\t", "Impala\t", 4);
Cars joesCar = new Cars("Hyundai\t", "Veloster\t", 3);
SmartCars TheHalwking = new SmartCars("SmartCar\t", "Hard Top\t", 2);
Truck dadsTruck = new Truck("Toyota\t","Tacoma\t", 2);
Motorcycle yama = new Motorcycle("Yamaha\t", "Vstar",650,78);
SportBike z1 = new SportBike("Suzuki\t", "Hybusa\t",1400, 110);//constructors
CafeRacer suz = new CafeRacer("Suzuki\t","Sérgio Teixeira's\t",750, 80);
DirtBike kawa=new DirtBike("Kawasaki\t", "KDX50\t",250, 46);
int opt=0;
System.out.println("1 --- view list via System print stream\n2 --- Write list to a text file\n3 --- Read list from text file");
Scanner sc=new Scanner(System.in);
System.out.println("Select: ");
opt=sc.nextInt();
if (opt==1)
{
int x=5;
while (x<5)//dows this 5 times
{
z1.speed();//adds 18 so z1 speed
}
suz.setHight(1.7);
suz.setLenght(4.7);
yama.speed();//adds 10 so yama speed
myCar.setSpeed(65);
joesCar.setSpeed(65);
TheHalwking.setSpeed(20);
myCar.accelerate();
TheHalwking.setIntel(80);
dadsTruck.setBedSize("Small");
dadsTruck.setSpeed(30);
dadsTruck.setSpeed(68);
dadsTruck.accelerate();
z1.setMaxSpeed(280);
z1.setZero260(2.78);
kawa.setHight(2.7);
kawa.setLenght(5.9);
kawa.speed();
System.out.println(myCar.toString());
System.out.println("\n");
System.out.println(joesCar.toString());
System.out.println("\n");
System.out.println(TheHalwking.toString());
System.out.println("\n");
System.out.println(dadsTruck.toString());
System.out.println("\n");
System.out.println(yama.toString());
yama.slow();//displays persuit msg
System.out.println("\n");
System.out.println(z1.toString());
z1.slow();
System.out.println(suz.toString());
System.out.println(kawa.toString());
System.out.println("\n");
System.out.println(impala.toString());
}
if (opt==23421)//DONT USE THIS YET!!!
{
PrintWriter out=null;
try {
System.out.println("Writing Vehicals to file......");
out = new PrintWriter(
new BufferedWriter(
new FileWriter("Veh.txt")));
out.println(myCar);
out.println(joesCar);
out.println(TheHalwking);
out.println(dadsTruck);
out.println(impala);
out.flush();
System.out.println("Wrote to file, file has been flushed");
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
out.close();
System.out.println("File has been closed");
}
if (opt==3)
{
try {
BufferedReader in = new BufferedReader(
new FileReader("veh.txt"));
String line = null;
String rl = in.readLine();
while (rl!=null)
{
String [] c=rl.split("\t");
String Make=c[0];
String Model=c[1];
int Doors=9;//Integer.parseInt(c[2]);
Cars p = new Cars(Make, Model, Doors);
System.out.println(p);
rl=in.readLine();
}
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
答案 0 :(得分:2)
行
String Model=c[1]; // no index 1 in array
给出错误,因为你的数组没有索引1
所以如果你读第一行文件就好像是
String rl="Make: Buick";
System.out.println(Arrays.toString(rl.split("\t")));
给出[Make: Buick]
即没有索引1
你应该检查逻辑