java - 错误 :Exception in thread "main" java. lang.ArrayIndexOutOfBoundsException:0

标签 java

运行代码时出现此错误:

线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException: 0 在 Module11_13_RealWorldProblem.main(Module11_13_RealWorldProblem.java:194)

我不知道如何解决它。我有一个文本文档,它应该读取该文档以获取信息,但它没有运行。

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

class User

{

private final String id;

private final String lstName;

private final String FstName;

private final String addressLine1;

String addressLine2;

private final String City;

private final String State;

private final long zip;

long zipPlus4;

private final double payAmount;

private final String payDate;

private final double payDue;

public User(String id,String lstName,String fstName,

String addressLine1,String addressLine2,String City,

String State,long zip,long zipPlus4,

String payDate,double payAmount,double payDue)

{

this.id=id;

this.lstName=lstName;

this.FstName=fstName;

this.addressLine1=addressLine1;

this.addressLine2=addressLine2;

this.City=City;

this.State=State;

this.zip=zip;

this.zipPlus4=zipPlus4;

this.payAmount=payAmount;

this.payDate=payDate;

this.payDue=payDue;

}

public void setAddLine2(String add){

this.addressLine2=add;}

public void setZipPlus4(long zip){

this.zipPlus4=zip;}

public long getZip()

{

return zip;

}

public double getPayAMount()

{

return payAmount;

}

public double getDueAMount()

{

return payDue;

}

public String getDate()

{

return payDate;

}

public String getaddressLine1()

{

return addressLine1;

}

public String getaddressLine2()

{

return addressLine2;

}

public String getCity()

{

return City;

}

public String getState()

{

return State;

}

public String getlstName()

{

return lstName;

}

public String getId()

{

return id;

}

public String getfstName()

{

return FstName;

}

public long getZipPlus4(){

return zipPlus4;}

}

public class Module11_13_RealWorldProblem {

public static void main(String[] args){

List<User> lst=new ArrayList<User>();


Scanner in=new Scanner(System.in);

File file = new File(args[0]);

if (!file.exists()) {

System.out.println(args[0] + " does not exist.");

return;

}

if (!(file.isFile() && file.canRead())) {

System.out.println(file.getName() + " cannot be read from.");

return;

}

try {

List<String> allLines = Files.readAllLines(Paths.get(args[0]));

for (String line : allLines) {

//System.out.println(line);

String[] str=line.split("\\^");

User user=new User(str[0],str[1],str[2],

str[3],"".equals(str[4])?"NoValue":str[4],str[5],str[6],

Long.valueOf(str[7]),"".equals(str[8])?Long.MAX_VALUE:Long.valueOf(str[8]),

str[9],Double.valueOf(str[10]),Double.valueOf(str[11]));

lst.add(user);

}

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("Do opertauiosn,");

System.out.println("Do you want to output the report to the screen ('S'), to a file ('F') or both ('B')");

String ch=in.nextLine();

StringBuilder sb=new StringBuilder();

for (User user : lst) {

sb.append(user.getId()+"^"+user.getlstName()+","+user.getfstName()+"^"+user.getaddressLine1()

+"^"+("NoValue".equals(user.getaddressLine2())?"":user.getaddressLine2())+"^"+user.getCity()+"^"+user.getState()+"^"+user.getZip()+"^"+(

Long.MAX_VALUE==user.getZipPlus4()?"":user.getZipPlus4())

+"^"+user.getPayAMount()+"^"+user.getPayAMount()+"^"+user.getDueAMount());

sb.append("\n");

}

if("S".equalsIgnoreCase(ch))

{

System.out.println(sb.toString());

}

else if("F".equalsIgnoreCase(ch))

{

System.out.println("Enter output file name with full path");

String outputFilename=in.nextLine();

log(sb.toString(),outputFilename);

System.out.println("File successfully written");

}

else if("B".equalsIgnoreCase(ch))

{

System.out.println(sb.toString());

System.out.println("Enter output file name with full path");

String outputFilename=in.nextLine();

log(sb.toString(),outputFilename);

System.out.println("File successfully written");

}

}

public static void log(String message,String fName){

try {

PrintWriter out = new PrintWriter(new FileWriter(fName, true), true);

out.write(message);

out.close();

} catch (Exception e) {

System.out.print("There is something wrong in output file/file path..retry !");

}



}

}

生成报告时 跳过那些不属于报告格式的字段 重新排列不同顺序的字段,使其符合报告格式 向用户添加提示“您要将报告输出到屏幕('S')、文件('F')还是两者('B')”。 如果用户输入“S”,您的代码应该像当前一样在屏幕上显示报告。 用户输入大写字母还是小写字母都无关紧要。例如“S”或“s”应该不重要。在这两种情况下,用户都应按上述方式显示数据。 如果用户输入“F”(或“f”),那么应用程序应该 提示用户输入所需的文件名(包括文件路径) 使用用户输入的文件名/路径将报告输出到文件。如果发送到屏幕,此输出的格式应与报告完全相同,只是它将报告输出到文件。 如果用户输入“B”(或“b”),则应用程序应将报告输出到屏幕并将其输出到文件。

最佳答案

程序在运行程序时需要来自命令行的输入。

File file = new File(args[0]);

args 是一个空数组,抛出一个indexOutOfBoundException。

确保在运行程序时传递文件名。

关于java - 错误 :Exception in thread "main" java. lang.ArrayIndexOutOfBoundsException:0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58366733/

相关文章:

java - 多个字段上的 Hibernate Search Facet

java - 是否可以使用 BufferedReader 反转文件的每一行?

java - 计算 INSERT/ON DUPLICATE KEY UPDATE

java - ESC/POS 热敏打印机,如何在 Android 中将位图图像居中?

java - 对象和数据结构有什么区别?

java - 多个sql查询与Java二分查找的效率

java - SonarLint super 慢

java - 将数组转换为 LIST 或 Set 的最快方法是什么?

java - 有人可以向我解释一下哨兵在 Java 中的作用吗?或者它是如何工作的?

java - 打开跟踪文件时出错。没有那个文件或目录 [2]