簡體   English   中英

如何將字符串數字轉換為 Java 中的 integer?

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

我已將一個文本文件導入到我的程序中,其中包含來自游戲玩家的多個分數。 我需要添加每個玩家的分數 - 在嘗試這樣做時,我意識到這些分數來自文本文件,因此,它們實際上是字符串,而不是數字。 我想將字符串編號轉換為 integer 但無法通過使用 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();
        }

這是我的 output:

-----------------------------------------------------------------------------------------------
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
----------------------------------------------------------------------------------------------

請注意,運行程序時,我的分數的 output 正確顯示。

此外,這里是來自文本文件的信息:

Bob,167,123,159,102,102,189,183,173,197,148 Sally,189,130,138,113,159,116,134,196,150,144 Mario,104,106,120,188,143,189,149,174,163,100 Lev,152,159,195,140,154,176,107,128,166,181 Carden,158,200,175,114,117,150,176,181,131,132 Adelie,175,199,122,104,198,182,175,153,120,165 Lada,161,108,102,193,151,197,115,137,126,186 Xavier,178,171,147,113,107,129,128,189,165,195 Raffi,176,144,151,124,149,112,158,159,119,177 Chang,135,144,177,153,143,125,145,140,117,158 Mich,156,105,178,137,165,180,128,115,139,157 Mason,162,185,108,106,113,135,139,135,197,160 Cora,186,115,106,126,135,108,157,156,187,120 Sergio,117,105,115,116,193,200,176,134,122,153 Jonas,132,163,196,101,134,159,131,104,135,168

您可以提供的任何見解將不勝感激。

我從您的原始代碼中假設您想要 parseInt ,因為您希望最后一列正確顯示總和。

為此,我從您的 String 數組記錄中刪除第一個元素(因為這是人的名字),然后我使用 Java 的 Streams API 將每個元素轉換為 integer 並得到總和。 在一些作為字符串的整數周圍還有一些空格,所以我在使用 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();
        }
    }

}

Output:

我在這里使用圖片代替,因為格式在文本中看起來很奇怪。

在此處輸入圖像描述

有一種更簡單的方法可以做到這一點:

int result = (int) number

這種方法稱為類型轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM