簡體   English   中英

如何將Stream轉換為Int?

[英]How can I convert a Stream into an Int?

我需要在文本文件中寫數字,然后必須閱讀文件,最后求和。 我可以讀寫文件,但是我不知道如何進行數學運算。

package clase.pkg13.de.septiembre;
import java.io.*;

public class FicheroTexto {
    public static void main (String args[]) {
        try {
            PrintWriter salida = new PrintWriter ( new BufferedWriter(new FileWriter("C:\\Users\\Santiago\\Desktop\\prueba.txt")));
            salida.println("1");
            salida.println("2");
            salida.close();

            BufferedReader entrada = new BufferedReader(new FileReader("C:\\Users\\Santiago\\Desktop\\prueba.txt"));
            String s, s2 = new String();
            while((s= entrada.readLine()) !=null)
                s2 = s2 + s + "\n";
            System.out.println("Numbers:"+"\n"+s2);
            entrada.close();
        } catch (java.io.IOException e) {
            // Do nothing
        }
    }
}

使用Integer.parseInt(string)將字符串轉換為整數。 然后,您可以對它們進行常規的數學運算。

parseInt

public static int parseInt(String s)
                    throws NumberFormatException

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

Parameters:
    s - a String containing the int representation to be parsed
Returns:
    the integer value represented by the argument in decimal.
Throws:
    NumberFormatException - if the string does not contain a parsable integer.

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29

我建議您使用掃描儀。

PrintWriter salida = new PrintWriter (new FileWriter("prueba.txt"));
salida.println(1);
salida.println(2);
salida.close();

Scanner scanner = new Scanner(new FileReader("prueba.txt"));
long total = 0;
List<Long> longs = new ArrayList<>();
while(scanner.hasNextLong()) {
    long l = scanner.nextLong();
    longs.add(l);
    total += l;
}
scanner.close();
System.out.println("Numbers:\n" + longs);
System.out.println("Sum: " + total);

版畫

Numbers:
[1, 2]
Sum: 3

嘗試使用此更新的代碼(假定每行包含一個數字)。 請參閱“ Java-parseInt()方法”( http://www.tutorialspoint.com/java/lang/integer_parseint.htm )上的有關從字符串解析整數的教程:

package clase.pkg13.de.septiembre;
import java.io.*;

public class FicheroTexto {
    public static void main (String args[]) throws Exception{
    try {
        PrintWriter salida = new PrintWriter ( new BufferedWriter(new FileWriter("C:\\Users\\Santiago\\Desktop\\prueba.txt")));
        salida.println("1");
        salida.println("2");
        salida.close();

        BufferedReader entrada = new BufferedReader(new FileReader("C:\\Users\\Santiago\\Desktop\\prueba.txt"));
        String s, s2 = new String();
        int sum = 0;
        while((s= entrada.readLine()) !=null)
        {
        s2 = s2 + s + "\n";
        sum += Integer.parseInt(s);

        }
        System.out.println("Numbers:"+"\n"+s2);
        System.out.println("Sum: " + sum);
        entrada.close();
    } catch (java.io.IOException e) {
        // Do nothing
    }
    }
}

暫無
暫無

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

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