简体   繁体   中英

How to read strings separated by return from a file

My task is to re-alphabetize a dictionary using 5 different sorting algorithms. I have the algorithms taken care of. The thing I am having trouble with is being able to read a string of text from a file and stop where the definition of the word ends without moving onto the next entry. This means I need each index of my array to be a word and its definition like so:

a[i] = "coronet: n. inferior crown denoting, according to its form, various degrees of noble rank less than sovereign.";

Where it is taken from a list arranged like so:

//Begin list

"orthogonal: adj. having or determined by right angles.

deviate: v. to take a different course.

coronet: n. inferior crown denoting, according to its form, various degrees of noble rank less than sovereign.

incite: v. to rouse to a particular action.

deface: v. to mar or disfigure the face or external surface of."

//End list

From there I can actually sort this array, but I'm currently having trouble using Scanner to properly distinguish where each definition ends.

I have no code for anybody to improve, I just need a method of solving this problem.

Any suggestions are appreciated!

Correct me if I'm wrong, but according to your example, each definition begins with a new line. In that case you can use this code to read one line at a time.

ArrayList<String> defs = new ArrayList<String>();
Scanner sc = new Scanner(new File(*path*));
while(sc.hasNextLine()){
     defs.add(sc.nextLine());
}

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