如何从Java文件中读取数字? [英] How can I read numbers from a file in Java?

查看:346
本文介绍了如何从Java文件中读取数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从我的file.txt中读取输入(字母,数字),其中它可以无限次读取,但只有在遇到特殊符号时才停止输入(字母,数字)同时,它是数字,即

  123,345,abc 

它应该翻译ascii代码并且添加结果为123 + 345 = 468的2个值

strong>编辑问题



这里是我的代码;我真的有一个阅读我的file.txt中的这些字节的问题。我想将它的值转换成类似于我的file.txt文件中的值

  public class .... {

static char tmp = 0;

public static void main(String [] args){
try {
Reader myReader = new FileReader(MyFolder / myFile2.txt);

列表< Character> myList = new ArrayList< Character>();
$ b / * for(int myData = myInputStream.read();
myData!= -1;
myData = myInputStream.read()){
System。 out.print(+(char)myData);


(int myData = myReader.read();
myData!= -1;
myData = myReader.read()){$ ((char)myData!=','){
myList.add((char)myData);
}
else {
continue;


(字符i:myList)
{
tmp = 1;
}
String myString = String.valueOf(tmp);
int num1 = Integer.parseInt(myString);
int num2 = Integer.parseInt(myString);
int equal = num1 + num2;

System.out.print(equal);

myReader.close();

catch(FileNotFoundException e){

}
catch(IOException e){




$ / code $ / pre

解决方案

这是一些基本的代码,如果你想要的话,建立你已经有的。

 公共类.... {

private static final模式COMMA = Pattern.compile(,);

public static void main(String [] args){
try {
BufferedReader myReader =
BufferedReader(new FileReader(MyFolder / myFile2.txt) );

List< Integer> myList = new ArrayList< Integer>();
int total = 0;
字符串行; $())
while((line = myReader.readLine())!= null){
(String token:COMMA.split(line)){
try {
total + =的Integer.parseInt(令牌);
} catch(NumberFormatException ex){
System.err.println(token +不是数字);
}
}
}

System.out.print(total);

myReader.close();
} catch(FileNotFoundException e){

} catch(IOException e){

}
}
}

请注意,最好重构这个,所以它不是全部在 main() / code>,异常处理不是很好,但我只是在这里的基础知识。


How can I read inputs (letters, numbers) from my file.txt wherein it reads infinitely but only stops when it encounters special symbols? At the same time when it is numbers i.e

123,345,abc

it should translate the ascii code and add the 2 values that results as 123 + 345 = 468

EDITED QUESTION

Here's my code; I really had a problem with reading those bytes in my file.txt. I want to convert its value where Isimilarly added it in my file.txt

public class .... {

    static char tmp = 0;

    public static void main(String[] args) {
        try {
            Reader myReader = new FileReader("MyFolder/myFile2.txt");

            List<Character> myList = new ArrayList<Character>();

            /*for(int myData = myInputStream.read();
                  myData != -1;
                  myData = myInputStream.read()){
                System.out.print(" " + (char)myData);
            }*/

            for(int myData = myReader.read();
                myData != -1;
                myData = myReader.read()){
                if((char)myData != ','){
                    myList.add((char)myData);
                }
                else{
                    continue;
                }
            }
            for(Character i: myList)
            {
                tmp = 1;
            }
            String myString = String.valueOf(tmp);
            int num1 = Integer.parseInt(myString);
            int num2 = Integer.parseInt(myString);
            int equal = num1 + num2;

            System.out.print(equal);

            myReader.close();
        }
        catch(FileNotFoundException e){

        }
        catch(IOException e){

        }
    }
}

解决方案

Here's some basic code to do what I think you're asking for, building off of what you already have.

public class .... {

    private static final Pattern COMMA = Pattern.compile(",");

    public static void main(String[] args) {
        try {
            BufferedReader myReader =
                    new BufferedReader(new FileReader("MyFolder/myFile2.txt"));

            List<Integer> myList = new ArrayList<Integer>();
            int total = 0;
            String line;
            while ((line = myReader.readLine()) != null) {
                for (String token : COMMA.split(line)) {
                    try {
                        total += Integer.parseInt(token);
                    } catch (NumberFormatException ex) {
                        System.err.println(token + " is not a number");
                    }
                }
            } 

            System.out.print(total);

            myReader.close();
        } catch(FileNotFoundException e){

        } catch(IOException e){

        }
    }
}

Note that it would be better to restructure this so it isn't all in main(), and the exception handling isn't very good, but I'm just going for the basics here.

这篇关于如何从Java文件中读取数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆