Reading only the integers from a txt file and adding the value up for each found

前端 未结 5 1974
情书的邮戳
情书的邮戳 2021-01-07 07:54

I am trying to read a txt file that contains strings with integers. I would like to be able to get just the integers from this file and add the value each to create a total.

5条回答
  •  温柔的废话
    2021-01-07 08:15

    You have done all the work and stuck with simple addition. Declare a variable total and add integers(values) to it. In the code block, if(scan.hasNextInt()) add the following statement : total += scan.nextInt().

    Do not forget to declare the variable before hand.

提交回复
热议问题