简体   繁体   English

从.txt文件中读取字符串,然后将其转换为整数

[英]Read String from a .txt file and then convert it to integer

I am attempting to read some data from a text file, this goes in the format: 我正在尝试从文本文件中读取一些数据,格式如下:

10
1
s
q

here is the code: 这是代码:

public static void main(String[] args) {

        String line = null;

        int[] b= new int[10];
        int i = 0;

        try {
            BufferedReader bf = new BufferedReader(new FileReader("C:\\myFile.txt"));

            while ((line = bf.readLine()) != null) {

                b[i] = Integer.parseInt(line);
                i++;

            }

        } catch (IOException e) {
            System.out.println(e.getMessage());

        }
}

this b[i] = Integer.parseInt(line); 这个b[i] = Integer.parseInt(line); easily reads first line from the file that is '10' and same goes for the second line that is '1' but for the third line 's' compiler gives me following error 轻松地从文件'10'读取第一行,第二行'1'相同,但是对于第三行's'编译器给我以下错误

Exception in thread "main" java.lang.NumberFormatException: For input string: "s"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at test.Main.main(Main.java:41)

I need to convert String 's' and 'H' to an integer value and then store into my array b of integer type. 我需要将String的's'和'H'转换为整数值,然后存储到整数类型的数组b中。

Check first that if string contains alphabet or numbers 首先检查字符串是否包含字母或数字

if(line.matches("[a-zA-Z]+"))
    int a=(int)line.charAt(0);

else if(line.matches("[0-9]+"))
    int a=Integer.parseInt(line);

This should work if you have only one character: 如果您只有一个字符,这应该可以工作:

char c = b[i].charAt(0);
int x = c - 48;

you can convert your text file into byte array then convert this array into int array 您可以将文本文件转换为字节数组,然后将此数组转换为int数组

public static void Do()
{

    try {
        InputStream in;
        in = new FileInputStream("a.txt");
        byte[] array = new byte[in.available()];
        in.read(array);
        int[] array2 = new int[array.length];
        for (int i = 0 ; i<array.length ; i++)
        {
            array2[i] = array[i];
            System.out.println(array2[i]+"");
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

and the output will be like this 和输出将是这样的

13 10 99 13 10 100 13 10 100 102 104 102 103 104 103 102 104 13 10 99 13 10 100 13 10 100 102 104 102 103 104 103 102 104

its depending in your text file characters . 它取决于您的文本文件字符。 data in a.txt is (abcd dfhfghgfh ) a.txt中的数据是(abcd dfhfghgfh)

but for me ,,, I will give every char an integer value then use if statment to convert char to int 但对我来说,我会给每个char一个整数值,然后使用if语句将char转换为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM