简体   繁体   中英

How can I get integers from strings in my situation?

Short story: I generate random numbers and end symbol 0-9/ ( '/' is line end symbol, if I meet it, I go to write to next line in file.) When I generated my numbers and put in file, I want to get back those numbers from file but in not like strings, it should be Integers.

Assume my file looks like this:

846525451454341*
*
0067617354809629733035*
3313449117867514*
02337436891267261671546*
469980603887044*
7*
9*
642*
*
0617044835719095066*
5*
7175887168189821760*
581*
76300152922692817*

As you can noticed, line is able to hold only '*' in some cases (As I said it is generated random).

My purpose

I want to get back these lines like integers. For example I take 1 line until I meet end symbol ( '/' ) then I loop another line and so on.

Some snippet:

 public void readGeneratedFile() throws IOException {
        try(BufferedReader r= new BufferedReader(new FileReader("C:\\java\\numbers.txt"))){
            int ch;
            s = new String();
            while((ch=r.read())!=-1){
               s+=String.valueOf(Character.toChars(ch)).replace(" ",""); // Here I take all file into String, But this approach is leading to boilerplate code;
                }
                    // catch IOException , finally close the file.

My question

How can I get back those lines like integers? (Suppose I want to take some actions with those numbers) It's cool if you get an idea what I want to do.

Thanks.

EDITED:

Sorry for misunderstanding, It is not what I want. I want to get back separated values, For example I have 123456/564654/21 string, and my Integer array[1][index] should looks like 1,2,3,4,5,6 then I meet end line symbol '/' I jump to array[2][index] and fill it with next line in file.

Your Strings are crossing the integer limit , You need BigInteger

Ex

BigInteger b = new BigInteger("7175887168189821760");

And you cannot get it back like integers since they are crossing the limit.

You can try like this. also.

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
         BigInteger integer;
        while (line != null) {
            line = line.replace("*","");
            integer = new BigInteger(line);
            //Your stuf
            line = br.readLine();
        }

    } finally {
        br.close();
    }

This will help

Integer.parseInt(String)

Since you have bigger values greater than what an Integer can store, you can go for Long.parseLong(String)

The other way is to use BigInteger(String) for working on the big numbers

最后将StringBuilder用作字符累加器(StringBuilder.append())

int result = Integer.parseInt(builder.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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