This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How Do I read Names in A text File In A reverse Order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How Do I read Names in A text File In A reverse Order" Watch "How Do I read Names in A text File In A reverse Order" New topic
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: 10172
    
    8

Read the line.
Split the read values
Use as required.


[How to ask questions] [Donate a pint, save a life!] [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: 10172
    
    8

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: 10172
    
    8

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: 10172
    
    8

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: 3432
    
  12
An array is indexed from 0 to length - 1. Your loop starts at length.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
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!!!
 
Don't get me started about those stupid light bulbs.
 
subject: How Do I read Names in A text File In A reverse Order
 
Similar Threads
incompatible types: found double required float error;
Read n Modify Text file
Problem in file transfer
problem reading a string from a text file
compare two text files