[英]The try and catch statement gives me no results?
**The aim of the code is to read some data from a file. **代码的目的是从文件中读取一些数据。 The data is split into a few lines and the data on each line is split using the "-" character.
数据被分成几行,每行上的数据使用“-”字符分割。 The code should spliut the data into separate strings and transfer the separate strings into an array called "splitLine".
代码应该将数据拆分为单独的字符串,并将单独的字符串传输到名为“splitLine”的数组中。 The point of the test data is that is throws an "ArrayIndexOutOfBoundsException" in which it does.
测试数据的重点是它会抛出一个“ArrayIndexOutOfBoundsException”。 However, after this exception is given, the catch should simply allow the code to continue however it's not working.
然而,在给出这个异常之后,catch 应该简单地允许代码继续,但是它不起作用。 **
**
public class asd
{
public static void main(String[] args)
{
String line = "";
String[] splitLine;
Scanner keyboard = new Scanner (System.in);
System.out.print("Enter the file name: ");
String filename = keyboard.nextLine();
Scanner fileReader = null;
try
{
File Fileobject = new File (filename);
fileReader = new Scanner (Fileobject);
System.out.println("The file " + filename + " contains the following lines");
System.out.println("==============================");
int x=0;
while(fileReader.hasNext())
{
try
{
line = fileReader.nextLine();
splitLine = line.split(" - "); //For assignment use line.split(" - ")
System.out.printf("Title: %20s \nAuthor: %17s \nPublisher: %9s\nPrice: %10s\nPages: %8s\nISBN: %16s\n===================\n", splitLine[x] , splitLine[x+1], splitLine[x+2], splitLine[x+3], splitLine[x+4], splitLine[x+5]);
}
catch (ArrayIndexOutOfBoundsException e)
{
continue;
}
}
}
catch(FileNotFoundException e)
{
System.out.println("Error - file does not exist");
System.exit(0);
}
}
}
I recommend to do validations on input before accessing it, and not just to use it and fail on exceptions.我建议在访问之前对输入进行验证,而不仅仅是使用它并在出现异常时失败。
Another Direction:另一个方向:
Anyway - per your message, your data is organized with " - ", so why not just to save the same data as JSON file ?无论如何 - 根据您的消息,您的数据是用“-”组织的,那么为什么不将相同的数据保存为 JSON 文件呢?
Then you can read it to your object like that: First, create your own Article.java class Then, write code like below:然后你可以像这样将它读到你的对象中:首先,创建你自己的Article.java类然后,编写如下代码:
import com.fasterxml.jackson.*
public static void someMethod()
{
File file = new File("....somepath...\\Desktop\\data.json");
ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(file , Article.class);
System.out.printf("Title: %20s \nAuthor: %17s \nPublisher: %9s\nPrice: %10s\nPages: %8s\nISBN: %16s\n===================\n", article.getTitle(), article.getPublisher(), article.getPrice(), article.getPages(), article.getSomethingMore1(), article.getSomethingMore2());
}
So as far as I understand you want to skip iteration when the splitLine
doesn't contain the required values?因此,据我所知,当
splitLine
不包含所需值时,您想跳过迭代? In that case wouldn't it be simpler to just check if you have enough params?在这种情况下,检查是否有足够的参数不是更简单吗?
splitLine = line.split(" - "); //For assignment use line.split(" - ")
if(splitLine.lenght != 6) {
continue;
}
System.out.printf("Title: %20s \nAuthor: %17s \nPublisher: %9s\nPrice: %10s\nPages: %8s\nISBN: %16s\n===================\n", splitLine[x] , splitLine[x+1], splitLine[x+2], splitLine[x+3], splitLine[x+4], splitLine[x+5]);
Also I would recommend extracting the inner try to a separate method for better readability此外,我建议将内部尝试提取为单独的方法以提高可读性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.