如何解析JAVA中文本文件的输入?(How do you parse input from a text file in JAVA?)

我试图从我的input.txt文件解析String,但每次都会抛出NumberFormatException。 我已经添加了目前为止所有的代码。 我也试过.trim。 我是Java中文本文件的新手; 因此,我不知道这里有什么问题。 但是,任何一点帮助将不胜感激。

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; public class Lab5_Exceptions_Redo extends Exception { public static void main(String[] args) throws FileNotFoundException { String file = "inputt.txt"; String file1 = "outputt.txt"; String file2 = "errorr.txt"; PrintWriter errorr = new PrintWriter(file2); PrintWriter inputt = new PrintWriter(file); inputt.println(15951); // int whatever = Integer.parseInt(file); // System.out.print(whatever); inputt.close(); Scanner scan = new Scanner(file); String number = scan.nextLine(); int num = Integer.parseInt(number.trim()); System.out.printf("%d", num); PrintWriter outputt = new PrintWriter(file1); outputt.println(num); outputt.close(); // inputt.close(); scan.close(); // System.out.printf("%d", num); try { } catch (InputMismatchException e) { errorr.println("There was an input mismatch error."); } catch (NoSuchElementException e) { errorr.println("There is no such element."); } catch (UnsupportedOperationException e) { errorr.println("An unsupported operation occured."); } catch (NumberFormatException e) { System.out.println(e.getMessage()); e.printStackTrace(); } errorr.close(); } }

I tried to parse the String from my input.txt file, but I get the NumberFormatException thrown every time. I've added all the code that I have so far. I have tried .trim as well. I am very new to text files in Java; therefore, I have no idea what is wrong here. However, any bit of help would be appreciated.

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; public class Lab5_Exceptions_Redo extends Exception { public static void main(String[] args) throws FileNotFoundException { String file = "inputt.txt"; String file1 = "outputt.txt"; String file2 = "errorr.txt"; PrintWriter errorr = new PrintWriter(file2); PrintWriter inputt = new PrintWriter(file); inputt.println(15951); // int whatever = Integer.parseInt(file); // System.out.print(whatever); inputt.close(); Scanner scan = new Scanner(file); String number = scan.nextLine(); int num = Integer.parseInt(number.trim()); System.out.printf("%d", num); PrintWriter outputt = new PrintWriter(file1); outputt.println(num); outputt.close(); // inputt.close(); scan.close(); // System.out.printf("%d", num); try { } catch (InputMismatchException e) { errorr.println("There was an input mismatch error."); } catch (NoSuchElementException e) { errorr.println("There is no such element."); } catch (UnsupportedOperationException e) { errorr.println("An unsupported operation occured."); } catch (NumberFormatException e) { System.out.println(e.getMessage()); e.printStackTrace(); } errorr.close(); } }

最满意答案

你犯了一个错误。 在Scanner构造函数中,您应该传递File对象而不是String对象。 因为你传递字符串“inputt.txt”它试图解析这个字符串,结果你得到NFE。 试试这个

Scanner scan = new Scanner(new File(file));

You are doing a mistake. In Scanner constructor you should pass File object not String object. Since you are passing string "inputt.txt" its trying to parse this string and as a result you are getting NFE. Try this

Scanner scan = new Scanner(new File(file));

更多推荐