| Author |
How Do I read Names in A text File In A reverse Order
|
Stanley Mungai
Ranch Hand
Joined: Dec 09, 2011
Posts: 155
|
|
HI guys, I have a text File that Contain Names Starting with the surname and then the First Name. I would Like to read them In a reverse order Starting with the First Name and the Second name. The code I have reads them in the order which they are printed on the text file. For Example
I have the Name:
Mungai Stanley
I want the output to be:
Stanley Mungai
The code I have is:
|
Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
Read the line.
Split the read values
Use as required.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Stanley Mungai
Ranch Hand
Joined: Dec 09, 2011
Posts: 155
|
|
|
Thank you Maneesh For that Quick Reply. I must say That Am very new to Java and I do not Know what to "Split the Read Values Mean".
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
1) When you execute
the name parameter contains the line text, e.g.Mungai Stanley
2) Look at the Java API to find method(s) in the String class which will split (hint) a given string and return you a String array
3) Once you have the elements in the array i.e. Mungai and Stanley, you can always refer to them by the index and use as you require it.
|
 |
Stanley Mungai
Ranch Hand
Joined: Dec 09, 2011
Posts: 155
|
|
|
Ok Thank you. I will look at that. It is the Re arranging bit that Am not sure what to do with it.
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
Its not quite difficult as it might sound.
As you already know, array elements can be accessed using the index like
obj[0], obj[1]....,obj[n]
So when you split the read line you get the last name and first name. Just access it as first name(index) and last name(index)
|
 |
Stanley Mungai
Ranch Hand
Joined: Dec 09, 2011
Posts: 155
|
|
|
OK I will try. I was just Hoping an example would help. Thank you.
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
|
Here you go http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
|
 |
Stanley Mungai
Ranch Hand
Joined: Dec 09, 2011
Posts: 155
|
|
Ok Here I what I now Have But I am Getting an error.
I am Getting An error
Please Someone help me out.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
An array is indexed from 0 to length - 1. Your loop starts at length.
|
Joanne
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32704
|
|
The continuation condition for your for loop looks incorrect.
[edit]I was mistaken, because you are iterating backwards. Joanne was correct. Sorry. You should write
for (int i = myArray.length - 1; i >= 0; i--) ...
[/edit]
|
 |
Stanley Mungai
Ranch Hand
Joined: Dec 09, 2011
Posts: 155
|
|
When I get a Solution, I always Like to post it for guys like me who might have the Same Problem. Here Is the Code guys:
If the Input is: Surname Firstname
Output is : Firstname Surname
Thanks to everyone Who helped Me to think Along!!!
|
 |
 |
|
|
subject: How Do I read Names in A text File In A reverse Order
|
|
|