【问题标题】:How to convert a string number into an integer in Java? [closed]如何在Java中将字符串数字转换为整数? [关闭]
【发布时间】:2022-02-01 01:42:43
【问题描述】:

我已将一个文本文件导入我的程序,其中包含来自游戏玩家的多个分数。我需要添加每个玩家的分数 - 在尝试这样做时,我意识到这些分数来自文本文件,因此,它们实际上是字符串,而不是数字。我想将字符串数字转换为整数,但无法通过使用 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 列弗,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

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

【问题讨论】:

  • 当您尝试使用 parseInt 时遇到什么问题?您还可以包含一个您从中获取信息的文本文件的示例吗?
  • 嘿@RichardKYu,谢谢你的回复。当我尝试使用 paseInt 时,我收到了来自 Eclipse 的一般性建议。代码本身运行,但我把它放在下面 String number="167";并且它不会改变输出本身的任何内容。我将使用文本文件对帖子进行编辑。

标签: java string integer bufferedreader filereader


【解决方案1】:

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

为此,我从您的 String 数组记录中删除第一个元素(因为这是人名),然后使用 Java 的 Streams 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();
        }
    }

}

输出:

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

【讨论】:

  • 嘿@RichardKYu,感谢您抽出宝贵的时间来做这件事。很抱歉我的回复延迟。我非常感谢您的洞察力,这非常有效。
【解决方案2】:

有一个更简单的方法来做到这一点:

int result = (int) number

这种方式称为类型转换。

【讨论】:

  • 这是无效的。您不能将 Java Object 转换为 primitive
  • 它在以前的项目中工作过,所以我不知道。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 2013-12-29
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 2015-07-27
  • 1970-01-01
相关资源
最近更新 更多