Home > Back-end >  Learn Java read TXT file data, converted to an int array, a lot more at the back of the original dat
Learn Java read TXT file data, converted to an int array, a lot more at the back of the original dat

Time:09-27

Package QuickSort;

import java.io.BufferedReader;
import java.io.File;
Import the Java. IO. FileReader;

Public class getTxt {

Public static void main (String [] args) {
//TODO Auto - generated method stub


The File File=new File (" F:/1. TXT ");
GetTxt gt=new getTxt ();
Int [b]=gt. Txt (file);
for(int i=0; iSystem. The out. Print (b [I] + "");
}


}

Public static int [] Txt File (filePath) {
Try {
BufferedReader br=new BufferedReader (new FileReader filePath ());
String message="";
The String line=null;
While ((line=br. ReadLine ())!=null) {
Message=message + "" + line;
}
Message=message. The trim ();//remove end blank
String s=[] message. The split (" ");//data separated by a space between
Int a []=new int [message length ()];
for(int i=0; iA [I]=Integer. The valueOf (s [I]);
}
br.close();
return a;
}

The catch (Exception e) {
//TODO: handle the exception
e.printStackTrace();
return null;
}
}
}

CodePudding user response:

The
refer to the original poster qq_39322325 response:


Before you remove the blank added

Message=message + "" + line; This sentence, don't go to the blank!

CodePudding user response:

Error, the above is the following sentence is wrong

Int a []=new int [message length ()];
Instead of
Int a []=new int [s.l ength ()]

CodePudding user response:

Two possible a
Int a []=new int [message length ()]; A number of group leader spent long behind the cause of 0 you stating why array with the length of the message? Not the length of the s?
2
for(int i=0; iA [I]=Integer. The valueOf (s [I]);
}
You how big the s array to see your data might be
1, 2, 3, 4, 5 s.l enght for 5 but you declare an array length is 9 empty 4 you declare an array length should be s the length of the array

CodePudding user response:

S.l ength without parentheses,

CodePudding user response:

reference 4 floor qq_39936465 response:
s.l ength without parentheses,
 
String s=[] message. The split (" ");//data separated by a space between
Int a []=new int [message length ()];
for(int i=0; i s is an array,,, not a string
  • Related