编辑:
您必须原谅我,该方法将不起作用。您需要做的是一次读取一行,然后按每个空格将其分开,以获得一组单词。从那里您可以识别整数并存储值。
final static String filename = "FILENAME.txt"; public static void main(String[] args) { Scanner scan = null; File f = new File(filename); try { scan = new Scanner(f); } catch (FileNotFoundException e) { System.out.println("File not found."); System.exit(0); } int total = 0; boolean foundInts = false; //flag to see if there are any integers while (scan.hasNextLine()) { //Note change String currentLine = scan.nextLine(); //split into words String words[] = currentLine.split(" "); //For each word in the line for(String str : words) { try { int num = Integer.parseInt(str); total += num; foundInts = true; System.out.println("Found: " + num); }catch(NumberFormatException nfe) { }; //word is not an integer, do nothing } } //end while if(!foundInts) System.out.println("No numbers found."); else System.out.println("Total: " + total); // close the scanner scan.close(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)