如何将Java中的字符串数字转换为整数?

How to convert a string number into an integer in Java?

我已将一个文本文件导入到我的程序中,其中包含来自游戏玩家的多个分数。我需要添加每个玩家的分数 - 在尝试这样做时,我意识到这些分数来自文本文件,因此,它们实际上是字符串,而不是数字。我想将字符串数字转换为整数,但无法通过使用 parseInt() 方法实现,我尝试在我的代码中使用以下方法:

String myString = "1234";

int foo = Integer.parseInt(myString);

但是一直没能成功。这是我目前的工作:

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;


public class luis_ramirez_GamesReport {

    private static String gamer;

    public static void main(String[] args) throws IOException
    {
        File fileName = new File("/Users/luisramirez/eclipse-workspace/GameScores.txt");
        if (fileName.exists())
        {
            BufferedReader br = null;
            String line = "";
            String cvsSplitBy = ",";
            int recordCount = 0;
            String number ="167";
            int result = Integer.parseInt(number);
            
            br = new BufferedReader(new FileReader(fileName));
        
        System.out.println("-----------------------------------------------------------------------------------------------");
        System.out.println("Games Report");
        System.out.println("-----------------------------------------------------------------------------------------------");
        System.out.println("Gamer    1       2       3       4       5       6       7       8       9       10       Total");
        System.out.println("-----------------------------------------------------------------------------------------------");
        while ((line = br.readLine()) != null)
        {
        String[] record = line.split(cvsSplitBy);
        System.out.println(record[0] + "\t"
            + record[1] + (record[2].length() > 7 ? "\t" : "\t")
            + record[2] + (record[2].length() > 7 ? "\t" : "\t")
            + record[3] + (record[3].length() > 7 ? "\t" : "\t")
            + record[4] + (record[4].length() > 7 ? "\t" : "\t")
            + record[5] + (record[5].length() > 7 ? "\t" : "\t")
            + record[6] + (record[6].length() > 7 ? "\t" : "\t")
            + record[7] + (record[7].length() > 7 ? "\t" : "\t")
            + record[8] + (record[8].length() > 7 ? "\t" : "\t")
            + record[9] + (record[9].length() > 7 ? "\t" : "\t")
            + record[10] + (record[10].length() > 7 ? "\t" : "\t")
            + record[1] + record[2] + record[3] + record[4] + record[5] + record[6] + record[7] + record[8] + record[9] + record[10]);
            recordCount++;

        }
        System.out.println("----------------------------------------------------------------------------------------------");
        System.out.printf("# of Gamers: %d%n",recordCount);
        System.out.println("Top Gamer: Adelie");
        System.out.println("----------------------------------------------------------------------------------------------");
            br.close();
        }

这是我的输出:

-----------------------------------------------------------------------------------------------
Games Report
-----------------------------------------------------------------------------------------------
Gamer    1       2       3       4       5       6       7       8       9       10       Total
-----------------------------------------------------------------------------------------------
Bob 167 123 159 102 102 189 183 173 197 148 167123159102102189183173197148
Sally   189 130 138 113 159 116 134 196 150 144 189130138113159116134196150144
Mario   104 106 120 188 143 189 149 174 163 100 104106120188143189149174163100
Lev 152 159 195 140 154 176 107 128 166 181 152159195140154176107128166181
Carden  158 200 175 114 117 150 176 181 131 132 158200175114117150176181131132
Adelie  175 199 122 104 198 182 175 153 120 165 175199122104198182175153120165
Lada    161 108 102 193 151 197 115 137 126 186 161108102193151197115137126186
Xavier  178 171 147 113 107 129 128 189 165 195 178171147113107129128189165195
Raffi   176 144 151 124 149 112 158 159 119 177 176144151124149112158159119177
Chang   135 144 177 153 143 125 145 140 117 158 135144177153143125145140117158
Mich    156 105 178 137 165 180 128 115 139 157 156105178137165180128115139157
Mason   162 185 108 106 113 135 139 135 197 160 162185108106113135139135197160
Cora    186 115 106 126 135 108 157 156 187 120 186115106126135108157156187120
Sergio  117 105 115 116 193 200 176 134 122 153 117105115116193200176134122153
Jonas   132 163 196 101 134 159 131 104 135 168 132163196101134159131104135168
----------------------------------------------------------------------------------------------
# of Gamers: 15
Top Gamer: Adelie
----------------------------------------------------------------------------------------------

请注意,当程序为 运行.

时,我的分数输出正确显示

此外,这里是文本文件中的信息:

鲍勃,167,123,159,102,102,189,183,173,197,148 莎莉,189,130​​,138,113,159,116,134,196,150,144 马里奥,104,106,120,188,143,189,149,174,163,100 Lev,152,159,195,140,​​154,176,107,128,166,181 卡登,158,200,175,114,117,150,176,181,131,132 阿德利,175,199,122,104,198,182,175,153,120,165 拉达161,108,102,193,151,197,115,137,126,186 泽维尔,178,171,147,113,107,129,128,189,165,195 拉菲,176,144,151,124,149,112,158,159,119,177 张,135,144,177,153,143,125,145,140,​​117,158 密歇根,156,105,178,137,165,180,128,115,139,157 梅森,162,185,108,106,113,135,139,135,197,160 科拉,186,115,106,126,135,108,157,156,187,120 塞尔吉奥,117,105,115,116,193,200,176,134,122,153 乔纳斯,132,163,196,101,134,159,131,104,135,168

如果您能提供任何见解,我们将不胜感激。

有一种更简单的方法可以做到这一点:

int result = (int) number

这种实现方式称为类型转换。

我从您的原始代码中假设您想要 parseInt,因为您希望最后一列正确显示总和。

为此,我从您的字符串数组记录中删除第一个元素(因为这是人名),然后我使用 Java 的流 API 以便将每个元素转换为整数并获得总和。在一些字符串整数周围也有一些空格,所以我在使用 parseInt 之前去掉了空格。

这是我的实现:

package com.sandbox;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ScoresDisplay {

    public static void main(String[] args) throws IOException
    {
        File fileName = new File("path/to/scores.txt");
        if (fileName.exists())
        {
            BufferedReader br = null;
            String line = "";
            String cvsSplitBy = ",";
            int recordCount = 0;
            String number ="167";
            int result = Integer.parseInt(number);
            
            br = new BufferedReader(new FileReader(fileName));
        
        System.out.println("-----------------------------------------------------------------------------------------------");
        System.out.println("Games Report");
        System.out.println("-----------------------------------------------------------------------------------------------");
        System.out.println("Gamer    1       2       3       4       5       6       7       8       9       10       Total");
        System.out.println("-----------------------------------------------------------------------------------------------");
        while ((line = br.readLine()) != null)
        {
        String[] record = line.split(cvsSplitBy);
        String[] ints_only = Arrays.copyOfRange(record, 1, record.length);
        List<Integer> recordAsInts = Arrays.stream(ints_only)
            .map(str -> str.strip())
            .map(Integer::parseInt)
            .collect(Collectors.toList());
        int sum = recordAsInts.stream().reduce(Integer::sum).orElse(0);
        System.out.println(record[0] + "\t"
            + record[1] + (record[2].length() > 7 ? "\t" : "\t")
            + record[2] + (record[2].length() > 7 ? "\t" : "\t")
            + record[3] + (record[3].length() > 7 ? "\t" : "\t")
            + record[4] + (record[4].length() > 7 ? "\t" : "\t")
            + record[5] + (record[5].length() > 7 ? "\t" : "\t")
            + record[6] + (record[6].length() > 7 ? "\t" : "\t")
            + record[7] + (record[7].length() > 7 ? "\t" : "\t")
            + record[8] + (record[8].length() > 7 ? "\t" : "\t")
            + record[9] + (record[9].length() > 7 ? "\t" : "\t")
            + record[10] + (record[10].length() > 7 ? "\t" : "\t")
            + sum);
            recordCount++;

        }
        System.out.println("----------------------------------------------------------------------------------------------");
        System.out.printf("# of Gamers: %d%n",recordCount);
        System.out.println("Top Gamer: Adelie");
        System.out.println("----------------------------------------------------------------------------------------------");
            br.close();
        }
    }

}

输出:

我在这里使用图片代替,因为格式在文本中看起来很奇怪。