如何在Java中从文本文件中添加字符串标记号

如何在Java中从文本文件中添加字符串标记号,java,Java,文本文件(myfile.txt)的编号如下: 6 19 20 23 34 45 47 51 54 56 61 70 72 76 90 93 94 96 105 111 我可以用Java作为字符串标记读取和打印此文本文件,我的代码是: File fileName = new File("myfile.txt"); Scanner inFile = new Scanner(fileName);

文本文件(myfile.txt)的编号如下:

6
19
20
23
34
45
47
51
54
56
61
70
72
76
90
93
94
96
105
111
我可以用Java作为字符串标记读取和打印此文本文件,我的代码是:

                      File fileName = new File("myfile.txt");         
                      Scanner inFile = new Scanner(fileName);        
                      while (inFile.hasNext())         
                      {         
                      token = inFile.next( ); 

                      System.out.println(token);

                      } 

                    inFile.close();

现在,我如何在java中获得这些数字的总和?我的意思是SUM(6+19+20+23+111)

您可以使用
Integer.parseInt
方法将字符串转换为int。然后将其添加到需要在while之外声明的
sum
变量中

Scanner inFile = new Scanner(fileName);        
int sum = 0;
while (inFile.hasNext()) {         
    token = inFile.next( ); 

    System.out.println(token);
    sum += Integer.parseInt(token);

} 

如果您正在读取整数值,那么使用
infle.nextInt()
是一个更好的选择。在while循环中使用
infle.hasNextInt()

使用int变量求和,并使用
scanner.nextInt()
提取数字。并在每次迭代中进行加法

                      File fileName = new File("myfile.txt");         
                      Scanner inFile = new Scanner(fileName);
                       int sum=0;        
                      while (inFile.hasNext())         
                      {         
                           sum+= inFile.nextInt( );     
                      } 
                      System.out.println(sum);

                    inFile.close();

首先使用
Scanner
类的
nextInt()
将整数读入
列表中。然后在列表中使用“enhanced”
for
loop

将这些数字添加到列表中,这样我就能够解决我的问题并取得了成功,但我仍然希望任何人提供关于是否以及如何通过使用teranary运算符简化此语法的信息

import java.util.Scanner;
import java.io.*;
public class Midterm
{
    public static void main(String[]args) throws Exception
    {
     final String FILE = "integers.txt";
     final String RELATIVE_DIRECTORY_FILE = "integers.txt ";
    int positiveNumberCount = 0,
    negativeNumberCount = 0;
    String dataItem;
    double sum = 0;
    double sumForAverage = 0;
    double average = 0;

    File inputFile = new File(RELATIVE_DIRECTORY_FILE);
    if(!inputFile.exists())
    {
        System.out.printf("%n%s%s%s", "The file, ",   RELATIVE_DIRECTORY_FILE, " does not exist");
    }

    if(inputFile.exists())
    {
        System.out.printf("%s%s%s", "The file ", RELATIVE_DIRECTORY_FILE,       "was found");
        Scanner file = new Scanner(inputFile);
        //***if i need to use a delimiter *****
        //file.useDelimiter(",") where in between " "  is my type of delimiter
        while(file.hasNext())
        {



            dataItem = file.nextLine();

            double numericalDataItem = Double.parseDouble(dataItem);

            boolean isPositive = false;
            boolean isNegative = false;

            if(numericalDataItem > 0)
            {
                isPositive = true;
                positiveNumberCount++;
            }
            else if(numericalDataItem < 0)
            {
                isNegative = true;
                negativeNumberCount++;
            }

            sum += Double.parseDouble(dataItem);
            sumForAverage = positiveNumberCount + negativeNumberCount;
            average = sum/sumForAverage;

        } 
        System.out.printf("%n%s%d%n%s%d%n%s%.2f%n%s%.2f", "Positive Number Count: ", positiveNumberCount,
        "Negative Number Count: ", negativeNumberCount, "Sum Of Numbers: ", sum, "Average of Numbers: ", average);
    }
}
}
import java.util.Scanner;
导入java.io.*;
公共课期中考试
{
公共静态void main(字符串[]args)引发异常
{
最终字符串文件=“integers.txt”;
最终字符串相对目录文件=“integers.txt”;
int-EnumberCount=0,
负枚举计数=0;
字符串数据项;
双和=0;
双集总平均值=0;
双平均=0;
文件输入文件=新文件(相对目录文件);
如果(!inputFile.exists())
{
System.out.printf(“%n%s%s%s”,“文件”,“相对目录文件”,“不存在”);
}
if(inputFile.exists())
{
System.out.printf(“%s%s%s”,“找到文件”,相对目录文件”);
扫描仪文件=新扫描仪(inputFile);
//***如果我需要使用分隔符*****
//file.useDelimiter(“,”),其中“”是我的分隔符类型
while(file.hasNext())
{
dataItem=file.nextLine();
double numericalDataItem=double.parseDouble(数据项);
布尔值isPositive=false;
布尔值为负=假;
如果(numericalDataItem>0)
{
isPositive=真;
正计数++;
}
else if(numericalDataItem<0)
{
isNegative=true;
负计数++;
}
sum+=Double.parseDouble(数据项);
sumForAverage=正枚举计数+负枚举计数;
平均值=总和/总和平均值;
} 
System.out.printf(“%n%s%d%n%s%d%n%s%.2f%n%s%.2f”,“正数计数:”,positiveEnumberCount,
“负数计数:”,NegativeEnumberCount,“数和:”,和,“数的平均值:”,平均值);
}
}
}

下次,将新问题作为新问题发布,而不是作为答案发布。我们很高兴您找到了解决方案,但根据您在回答中的帮助请求,您不太可能获得进一步的帮助。如果你需要进一步的帮助,你应该发布一个新问题。您可能想在此处阅读有关的信息和问题。