簡體   English   中英

掃描儀中的StringIndexOutOfBoundsException

[英]StringIndexOutOfBoundsException in Scanner

我想掃描儀控制台並繼續將用戶輸入為字符串,然后輸入掃描儀文件的名稱為該字符串。

public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); intro(); System.out.print("input file name?"); String inputfile = console.next(); Scanner file = new Scanner(new File(inputfile)); } public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); intro(); System.out.print("input file name?"); String inputfile = console.next(); Scanner file = new Scanner(new File(inputfile)); }但是當我輸入的文件名如personality.txt,JAVA一直告訴我在線程異常“主要” java.lang.StringIndexOutOfBoundsException:字符串索引超出范圍:9.我怎樣才能解決這個問題?

嘗試過您的代碼,但沒有發生異常。 不知道是什么原因導致該錯誤,但可能是您可以使用String inputfile = console.nextLine();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM