• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Save each line contacts in a file

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again everyone.
My problem there is i want to insert contacts by line. For en example:
todd fox 2222222
tod sss 35464564576
jeff star 45252
I used append and everything but is pointless



 
Bartender
Posts: 6123
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look like you'd get three identical lines in the file. You have a loop counter but you don't use 'k' anywhere.

What behavior are you seeing?
 
Ruben Ochoa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. The output is [ ],[ ].[ ] 3 times, filled by names. What loop i must use? While(true)? I tried
 
Carey Brown
Bartender
Posts: 6123
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ruben Ochoa wrote:Indeed. The output is [ ],[ ].[ ] 3 times, filled by names. What loop i must use? While(true)? I tried

You need to be clearer when responding. Do you get "[]" or do you get "somename"?

You are filling your array with three pieces of data. When you are printing the data you will get a single line of three pieces of data separated with commas. If you want multiple name entries you'll have to fill your list outside of this write method. You don't need any loop to write the three pieces of data. You will need a loop if you fill in the list outside of your method.
 
Ruben Ochoa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Ruben Ochoa wrote:Indeed. The output is [ ],[ ].[ ] 3 times, filled by names. What loop i must use? While(true)? I tried

You need to be clearer when responding. Do you get "[]" or do you get "somename"?

You are filling your array with three pieces of data. When you are printing the data you will get a single line of three pieces of data separated with commas. If you want multiple name entries you'll have to fill your list outside of this write method. You don't need any loop to write the three pieces of data. You will need a loop if you fill in the list outside of your method.



You have right.
The output is: [ somename ],[  somename ], [  somename ]
I used for or while but is pointless
 
Sheriff
Posts: 6173
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried a "for each" loop?  They work well with Lists:
 
Ruben Ochoa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Have you tried a "for each" loop?  They work well with Lists:



You mean:
The output is just []
 
Carey Brown
Bartender
Posts: 6123
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear what your requirements are. Do you want first, last, and phone on separate lines or all on one line separated by a space? Do you want multiple "persons" or just one?

It is also helpful to make a simple self contained compilable example to play with. Here is one with a variety of approaches...
Note that when you use write() you'll need to supply your own new-line.
 
Ruben Ochoa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a doctor for an example wants to creates contacts of his clients, inserts one by one with separate lines the contacts.
 
Carey Brown
Bartender
Posts: 6123
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What format do you want the line in? How are you going to deal with multiple contacts if you have only one text field for each contact field?
 
Knute Snortum
Sheriff
Posts: 6173
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ruben Ochoa wrote:You mean:
The output is just []


No, you don't need a loop for that (and notice you're not using the variable data).  If that's all you need just write:
 
Ruben Ochoa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What format do you want the line in? How are you going to deal with multiple contacts if you have only one text field for each contact field?



The program I want to create is about a menu that have submenus of Contacts, Dates and Help. At Contact menu I want: 1. Create new contact with name, surname and telephone. 2.Read the file with contacts. Contacts saves in a array. 3.Getting contacts in a file. 4.Delete contacts and 5.Update contacts.
I created 4 textfields. 1.Name 2.Surname.3Telephone4.Date
 
Knute Snortum
Sheriff
Posts: 6173
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...but weren't we talking about fixing this code?
Then you could do this with a "for each" loop:
That would give you a file with each field on a separate line.  That may not be what you want.

While I write this, I see you have posted how you want the data to be saved.  You probably don't want to use the method above.  Do you have a Contact class?  If so, use a list of contacts, List<Contact>, then use a loop like this:
If you write a toString() method correctly, this format will post to the text file.
 
Ruben Ochoa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:...but weren't we talking about fixing this code?
Then you could do this with a "for each" loop:
That would give you a file with each field on a separate line.  That may not be what you want.

While I write this, I see you have posted how you want the data to be saved.  You probably don't want to use the method above.  Do you have a Contact class?  If so, use a list of contacts, List<Contact>, then use a loop like this:
If you write a toString() method correctly, this format will post to the text file.




Indeed but i wanted to give more informations so will be more cleared.
 
Ruben Ochoa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried nane ArrayList into Contacts that i have create but not work.
 
Knute Snortum
Sheriff
Posts: 6173
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's good to remember that saying ItDoesntWorkIsUseless (that's a link).  Tell us what happened in detail.  We can't help it we don't know what's going on.
 
Ruben Ochoa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:It's good to remember that saying ItDoesntWorkIsUseless (that's a link).  Tell us what happened in detail.  We can't help it we don't know what's going on.



I understand
 
The moustache of a titan! The ad of a flea:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!