Convert string representation of array back to int array in java

前端 未结 2 658
予麋鹿
予麋鹿 2021-01-29 11:51

just started programming with Java. If I have an array as follows stored in a .txt file:

[10, 22, 30, 55, 10, 20, 19]

How do I convert it back

相关标签:
2条回答
  • 2021-01-29 12:39

    What you're talking about doing is parsing the text. A simple way to implement this yourself for a beginner if you know the structure of the text is to hard code the rules for reading in a function and use Scanner.

    I'm on my phone so I'll put details later but there's tons of info on how to do that if I'm not in time

    0 讨论(0)
  • 2021-01-29 12:55

    So if your line is

    [10, 22, 30, 55, 10, 20, 19]
    

    then you can do

    line = line.replace ("[", "");
    line = line.replace ("]", "");
    

    then you can use String.split

    String vals [] = line.split (",");
    

    then for each val you can use

    intVal [x] = Integer.valueOf (val[x].trim ());
    
    0 讨论(0)
提交回复
热议问题