• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read numbers and names from a text file

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, guys!
I have a text file containing numbers and names as shown in the example below:

61 13 31 28 80 52 41 Albert Rosenberg
64 76 36 45 54 35 09 Joel
79 25 63 37 75 42 19 Michael Burton


I'm making a program that will read the file and put the numbers in a list of int arrays and names in another list of strings. In my program i created two classes. One will receive the numbers and the other will receive the names. But i only can read the numbers! How can I read everything and separate into two different lists?
Here is what i did.
 
Bartender
Posts: 612
7
Mac OS X Python
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well to be more correct, you are reading in all the data - you are just ignoring the names.

What you may want to do in your for loop is to test the value of str to see if it is alpha or numeric (see the javadocs for String for some methods that may help).

Then you can build a non-numeric character string to rebuild the name and at end of the for loop add it to your Names.

-steve
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you create a class which encapsulates both an int[] and a name. No messing around with multiple arrays.

If you have a text file, you would find the methods of the Scanner class helpful; they can tell in advance whether you are going to read a number or not.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, i cannot use "split( "\\s+" ) )" anymore? This method take only one argument! I would like to use regular expressions.
Campbell Ritchie, i use two classes because i think it looks more organized. Don't you think? I would like to count how many times a name appears in this file, that's why i use Names class with a string and an integer value.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sure you can use split.

But in your current code you are simply ignoring the names.

But again (or but, but) Campbell has a great suggestion. Either way read the javadocs and decide for yourself.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell has a great suggestion.



What suggestion? Use scanner or create a class which encapsulates both an int[] and a name.?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robson Martinz wrote: . . . What suggestion? . . .

Both were great suggestions
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robson Martinz wrote:So, i cannot use "split( "\\s+" ) )" anymore? This method take only one argument! I would like to use regular expressions.

There are all sorts of ways to solve your problem; some of them can use regular expressions. The way you said that, it made it sound as if your priority was to use regular expressions rather than find an elegant solution to the name/number problem.

Campbell Ritchie, i use two classes because i think it looks more organized. Don't you think?

No.

I would like to count how many times a name appears in this file, that's why i use Names class with a string and an integer value.

That is different from what you told us earlier. Please explain.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That is different from what you told us earlier. Please explain.


I want to encapsulate each name with the number of times it appeared in the file, so i created a class just for the names.

I'm sorry! I don't know what is different from earlier. I told you guys i want to read the numbers and names. I would like to count how many times some name appears in the file. I almost solved my problem, but i have another one. When i read two names that are equal, my program say they are different! I tried to call "getBytes()" and he shows me two different things, but the names in the file are the same! If in this case i have to create another topic, please tell me.


Please, consider this class Names, instead of my first one, in my original post. I decided change her a little bit.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please, consider this example of my file.

61 13 31 28 80 52 41 Albert Rosenberg
64 76 36 45 54 35 09 Joel Whatever
23 25 54 45 75 01 69 Albert Rosenberg
79 25 63 37 75 42 19 Michael Burton


Thank you for your help!
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to say I don't understand the way you have designed your classes but I can tell you that you certainly shouldn't be comparing Strings using '=='. See AvoidTheEqualityOperator (← click)
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem solved. Thank you for your answer, guys!

Using equals or equalignirecase.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see this is already answered, but get to know how to use the comparable interface thoroughly. It is quite handy and worth studying further. You will use this one over and over again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic