Home » Java » java – reading text file and sorting each line into seperate values-Exceptionshub

java – reading text file and sorting each line into seperate values-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I do apologise in advance, I am a beginner in Java and this is only my second project.

I was given a text file and each line contains information for example

Name:Location:fees:description

and there is about 25 unique lines.

I do understand I would need to use some sorta of indexing and splitting each line with “:”.

I do not expect a full solution just how I would approach this assignment.

my expected output should look something like

ID - Name - Location - Description
------------------------------------
1     Bob      example   example
How to&Answers:

First you would need to read the file line by line and store them in an array. Then for each line you need to use split(':') to make them separated, it will give you another array. Then just loop through the new array and print them as you like.

If you want to sort them, then you have to design a class and create a list/array of that class, and store your data inside the list. Next decide which property you would like to sort (Name, Location, fees, description). Then implement some simple sort algorithm (Selection Sort, Bubble Sort) to sort them using your property of choice. Finally loop and output your sorted list.